-
Notifications
You must be signed in to change notification settings - Fork 344
Expand file tree
/
Copy pathgithub-docker-registry-push.yml
More file actions
248 lines (217 loc) · 10.7 KB
/
github-docker-registry-push.yml
File metadata and controls
248 lines (217 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# This workflow is used for building and pushing Docker images to GitHub Packages.
# It is triggered on push, pull request events and also runs on a schedule (at 4:30 and 16:30).
# It uses the GitHub Container Registry (GHCR) for storing the Docker images.
name: docker image building and pushing to GitHub Packages
on:
push:
branches:
- '*' # Triggered on push to any branch
pull_request:
branches: [ '*' ] # Triggered on pull request to any branch
schedule:
- cron: '30 4,16 * * *' # Runs on a schedule at 4:30 and 16:30
env:
GHCR_USERNAME: ${{ github.repository_owner }} # The owner of the repository
GHCR_TOKEN: ${{ secrets.GHCR_TOKEN }} # The token for GHCR
jobs:
docker:
runs-on: ubuntu-latest # The type of machine to run the job on
steps:
- name: Check if PR is from a fork
# This step checks if the pull request is from a fork
run: |
if [ "${{ github.event_name }}" = "pull_request" ]; then
if [ '${{ github.event.pull_request.head.repo.full_name }}' != '${{ github.repository }}' ]; then
echo "IS_FORK=true" >> $GITHUB_ENV
else
echo "IS_FORK=false" >> $GITHUB_ENV
fi
else
echo "IS_FORK=false" >> $GITHUB_ENV
fi
- name: Checkout
# This step checks out the repository
uses: actions/checkout@v6
with:
fetch-depth: 0
- name: Retrieve modified files
# This step retrieves the files that were modified in the commit
id: changes
run: echo "files=$(git diff --name-only ${{ github.event.before }} ${{ github.sha }} | tr '\n' ' ')" >> $GITHUB_ENV
- name: Authenticate GH CLI
# This step authenticates the GitHub CLI
run: gh auth login --with-token <<< "${{ secrets.GITHUB_TOKEN }}"
- name: Commit and create PR for Docker versions
if: github.event_name != 'pull_request'
run: |
git config --global user.name 'GitHub Action'
git config --global user.email 'action@github.com'
# Ensure we start from a clean main branch
git fetch origin main
git checkout main
git reset --hard origin/main
# Create unique branch name to avoid conflicts
BRANCH_NAME="docker-versions-update-$(date +%s)"
git checkout -b "$BRANCH_NAME"
# Write Docker versions to file
echo "- $(docker --version)" > docker-versions.txt
echo "- $(docker compose version)" >> docker-versions.txt
# Only add the specific file we want
git add docker-versions.txt
# Check if there are actually changes to commit
if git diff --cached --quiet; then
echo "No changes to docker-versions.txt, skipping PR creation"
else
# Check if an open PR already exists
EXISTING_PR=$(gh pr list --base main --state open --search "chore: update Docker versions in:title" --json number --jq '.[0].number // empty')
if [ -n "$EXISTING_PR" ]; then
echo "Open PR #$EXISTING_PR already exists for Docker version updates, skipping PR creation"
echo "You can view it at: $(gh pr view $EXISTING_PR --json url --jq '.url')"
else
git commit -m "chore: update Docker versions"
git push origin "$BRANCH_NAME"
# Create PR with explicit title and body instead of --fill
gh pr create --title "chore: update Docker versions" --body "Automated update of docker-versions.txt with current Docker and Docker Compose versions." --base main --head "$BRANCH_NAME" --base main --head "$BRANCH_NAME"
fi
fi
- name: Check for Dockerfile and context changes
# This step checks for changes in Dockerfile and context
run: |
HAS_DOCKER_CHANGES=false
for file in ${{ env.files }}; do
if [[ "$file" =~ (^|/)Dockerfile($|/)|(^|/)dockerfiles/ ]]; then
echo "Dockerfile or dockerfiles directory has changed: $file"
HAS_DOCKER_CHANGES=true
break
fi
done
echo "HAS_DOCKER_CHANGES=$HAS_DOCKER_CHANGES" >> $GITHUB_ENV
if [[ "$HAS_DOCKER_CHANGES" == "false" ]]; then
echo "No Dockerfile or context directory changes. Skipping Docker image build and push steps."
echo "Changed files: ${{ env.files }}"
fi
- name: Set up QEMU
# This step sets up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
# This step sets up Docker Buildx
uses: docker/setup-buildx-action@v3
-
name: Login to GitHub Container Registry
# This step logs in to GHCR
if: env.HAS_DOCKER_CHANGES == 'true' && env.IS_FORK != 'true'
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ env.GHCR_USERNAME }}
password: ${{ env.GHCR_TOKEN }}
- name: Extract branch name
# This step extracts the branch name
shell: bash
run: |
BRANCH_NAME=$(echo ${GITHUB_REF#refs/heads/} | sed -e 's#/#-#g')
if [[ "$BRANCH_NAME" == "main" ]]; then BRANCH_NAME=""; fi
echo "BRANCH=$BRANCH_NAME" >> $GITHUB_ENV
id: extract_branch
- name: Set repository name to lowercase
# This step sets the repository name to lowercase
run: echo "REPO_NAME=$(echo ${{ github.repository }} | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV
- name: Build and push a simple jenkins controller
# This step builds and pushes a simple Jenkins controller
if: (contains(env.files, 'dockerfiles/Dockerfile') || contains(env.files, 'dockerfiles/')) && env.IS_FORK != 'true'
uses: docker/build-push-action@v6
with:
context: ./dockerfiles
platforms: linux/amd64, linux/arm64
push: true
tags: ghcr.io/${{ env.REPO_NAME }}/jenkinsci-tutorials:simple_controller_${{ env.BRANCH }}
- name: Build and push the jenkins agent for maven tutorial
# This step builds and pushes the Jenkins agent for the Maven tutorial
if: contains(env.files, 'dockerfiles/maven/') && env.IS_FORK != 'true'
uses: docker/build-push-action@v6
with:
context: ./dockerfiles/maven
platforms: linux/amd64, linux/arm64
push: true
tags: ghcr.io/${{ env.REPO_NAME }}/jenkinsci-tutorials:maven_agent_${{ env.BRANCH }}
- name: Build and push the jenkins agent for python tutorial
# This step builds and pushes the Jenkins agent for the Python tutorial
if: contains(env.files, 'dockerfiles/python/') && env.IS_FORK != 'true'
uses: docker/build-push-action@v6
with:
context: ./dockerfiles/python
platforms: linux/amd64, linux/arm64
push: true
tags: ghcr.io/${{ env.REPO_NAME }}/jenkinsci-tutorials:python_agent_${{ env.BRANCH }}
- name: Build and push the jenkins agent for node tutorial
# This step builds and pushes the Jenkins agent for the Node.js tutorial
if: contains(env.files, 'dockerfiles/node/') && env.IS_FORK != 'true'
uses: docker/build-push-action@v6
with:
context: ./dockerfiles/node
platforms: linux/amd64, linux/arm64
push: true
tags: ghcr.io/${{ env.REPO_NAME }}/jenkinsci-tutorials:node_agent_${{ env.BRANCH }}
- name: Build and push the jenkins agent for the sidekick container
# This step builds and pushes the Jenkins agent for the sidekick container
if: contains(env.files, 'dockerfiles/sidekick/') && env.IS_FORK != 'true'
uses: docker/build-push-action@v6
with:
context: ./dockerfiles/sidekick
platforms: linux/amd64, linux/arm64
push: true
tags: ghcr.io/${{ env.REPO_NAME }}/jenkinsci-tutorials:sidekick_${{ env.BRANCH }}
- name: Build and push the jenkins agent for the agent-finding container
# This step builds and pushes the Jenkins agent for the agent-finding container
if: contains(env.files, 'dockerfiles/agent-discovery/') && env.IS_FORK != 'true'
uses: docker/build-push-action@v6
with:
context: ./dockerfiles/agent-discovery/
platforms: linux/amd64, linux/arm64
push: true
tags: ghcr.io/${{ env.REPO_NAME }}/jenkinsci-tutorials:agent_discovery_${{ env.BRANCH }}
- name: Build and push the jenkins agent for multi-branch controller
# This step builds and pushes the Jenkins agent for the multi-branch controller
if: contains(env.files, 'dockerfiles/multi/') && env.IS_FORK != 'true'
uses: docker/build-push-action@v6
with:
context: ./dockerfiles/multi
platforms: linux/amd64, linux/arm64
push: true
tags: ghcr.io/${{ env.REPO_NAME }}/jenkinsci-tutorials:multi_controller_${{ env.BRANCH }}
- name: Build and push the jenkins agent for Android
# This step builds and pushes the Jenkins agent for Android
if: contains(env.files, 'dockerfiles/android/') && env.IS_FORK != 'true'
uses: docker/build-push-action@v6
with:
context: ./dockerfiles/android
platforms: linux/amd64
push: true
tags: ghcr.io/${{ env.REPO_NAME }}/jenkinsci-tutorials:android_${{ env.BRANCH }}
- name: Build and push the jenkins agent for golang tutorial
# This step builds and pushes the Jenkins agent for the Golang tutorial
if: contains(env.files, 'dockerfiles/golang/') && env.IS_FORK != 'true'
uses: docker/build-push-action@v6
with:
context: ./dockerfiles/golang
platforms: linux/amd64, linux/arm64
push: true
tags: ghcr.io/${{ env.REPO_NAME }}/jenkinsci-tutorials:golang_${{ env.BRANCH }}
- name: Build and push the jenkins agent for cpp tutorial
# This step builds and pushes the Jenkins agent for the C++ tutorial
if: contains(env.files, 'dockerfiles/cpp/') && env.IS_FORK != 'true'
uses: docker/build-push-action@v6
with:
context: ./dockerfiles/cpp
platforms: linux/amd64, linux/arm64
push: true
tags: ghcr.io/${{ env.REPO_NAME }}/jenkinsci-tutorials:cpp_${{ env.BRANCH }}
- name: Build and push the jenkins agent for dotnet tutorial
# This step builds and pushes the Jenkins agent for the .NET tutorial
if: contains(env.files, 'dockerfiles/dotnet/') && env.IS_FORK != 'true'
uses: docker/build-push-action@v6
with:
context: ./dockerfiles/dotnet
platforms: linux/amd64, linux/arm64
push: true
tags: ghcr.io/${{ env.REPO_NAME }}/jenkinsci-tutorials:dotnet_${{ env.BRANCH }}