3535 id : changes
3636 run : echo "files=$(git diff --name-only ${{ github.event.before }} ${{ github.sha }} | tr '\n' ' ')" >> $GITHUB_ENV
3737
38- - name : Write Docker versions to file
39- # This step writes the Docker and Docker Compose versions to a file
40- run : |
41- echo "- $(docker --version)" > docker-versions.txt
42- echo "- $(docker compose version)" >> docker-versions.txt
43-
4438 - name : Authenticate GH CLI
4539 # This step authenticates the GitHub CLI
4640 run : gh auth login --with-token <<< "${{ secrets.GITHUB_TOKEN }}"
@@ -50,28 +44,65 @@ jobs:
5044 run : |
5145 git config --global user.name 'GitHub Action'
5246 git config --global user.email '[email protected] ' 53- git checkout -b docker-versions-update
47+
48+ # Ensure we start from a clean main branch
49+ git fetch origin main
50+ git checkout main
51+ git reset --hard origin/main
52+
53+ # Create unique branch name to avoid conflicts
54+ BRANCH_NAME="docker-versions-update-$(date +%s)"
55+ git checkout -b "$BRANCH_NAME"
56+
57+ # Write Docker versions to file
58+ echo "- $(docker --version)" > docker-versions.txt
59+ echo "- $(docker compose version)" >> docker-versions.txt
60+
61+ # Only add the specific file we want
5462 git add docker-versions.txt
55- if git diff-index --quiet HEAD --; then
56- echo "No changes to commit"
63+
64+ # Check if there are actually changes to commit
65+ if git diff --cached --quiet; then
66+ echo "No changes to docker-versions.txt, skipping PR creation"
5767 else
58- git commit -m "Update Docker versions"
59- git push origin docker-versions-update
60- echo 'y' | gh pr create --fill
68+ # Check if an open PR already exists
69+ EXISTING_PR=$(gh pr list --base main --state open --search "chore: update Docker versions in:title" --json number --jq '.[0].number // empty')
70+ if [ -n "$EXISTING_PR" ]; then
71+ echo "Open PR #$EXISTING_PR already exists for Docker version updates, skipping PR creation"
72+ echo "You can view it at: $(gh pr view $EXISTING_PR --json url --jq '.url')"
73+ else
74+ git commit -m "chore: update Docker versions"
75+ git push origin "$BRANCH_NAME"
76+ # Create PR with explicit title and body instead of --fill
77+ gh pr create \
78+ --title "chore: update Docker versions" \
79+ --body "Automated update of docker-versions.txt with current Docker and Docker Compose versions.
80+
81+ **Changes:**
82+ - Updated Docker version information
83+ - Updated Docker Compose version information
84+
85+ This PR only contains changes to \`docker-versions.txt\` and no other files." \
86+ --base main \
87+ --head "$BRANCH_NAME"
88+ fi
6189 fi
6290
6391 - name : Check for Dockerfile and context changes
6492 # This step checks for changes in Dockerfile and context
6593 run : |
94+ HAS_DOCKER_CHANGES=false
6695 for file in ${{ env.files }}; do
67- if [[ $file =~ (^|/)Dockerfile($|/)|(^|/)dockerfiles/ ]]; then
68- echo "Dockerfile or dockerfiles directory has changed. "
69- echo "Changed file: $file"
96+ if [[ " $file" =~ (^|/)Dockerfile($|/)|(^|/)dockerfiles/ ]]; then
97+ echo "Dockerfile or dockerfiles directory has changed: $file "
98+ HAS_DOCKER_CHANGES=true
7099 break
71100 fi
72101 done
73-
74- if (( $? == 0 )); then
102+
103+ echo "HAS_DOCKER_CHANGES=$HAS_DOCKER_CHANGES" >> $GITHUB_ENV
104+
105+ if [[ "$HAS_DOCKER_CHANGES" == "false" ]]; then
75106 echo "No Dockerfile or context directory changes. Skipping Docker image build and push steps."
76107 echo "Changed files: ${{ env.files }}"
77108 fi
87118 -
88119 name : Login to GitHub Container Registry
89120 # This step logs in to GHCR
90- if : contains( env.files, 'Dockerfile')
121+ if : env.HAS_DOCKER_CHANGES == 'true' && env.IS_FORK != 'true'
91122 uses : docker/login-action@v3
92123 with :
93124 registry : ghcr.io
@@ -103,91 +134,83 @@ jobs:
103134 echo "BRANCH=$BRANCH_NAME" >> $GITHUB_ENV
104135 id : extract_branch
105136
106- - name : Extract branch name and set BRANCH environment variable
107- # This step extracts the branch name and sets the BRANCH environment variable
108- shell : bash
109- run : |
110- BRANCH_NAME=$(echo ${GITHUB_REF#refs/heads/} | sed -e 's#/#-#g')
111- if [[ "$BRANCH_NAME" == "main" ]]; then BRANCH_NAME=""; fi
112- echo "BRANCH=$BRANCH_NAME" >> $GITHUB_ENV
113-
114137 - name : Set repository name to lowercase
115138 # This step sets the repository name to lowercase
116139 run : echo "REPO_NAME=$(echo ${{ github.repository }} | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV
117140
118141 - name : Build and push a simple jenkins controller
119142 # This step builds and pushes a simple Jenkins controller
120- if : contains(env.files, 'dockerfiles/Dockerfile') || contains(env.files, 'dockerfiles/')
143+ if : ( contains(env.files, 'dockerfiles/Dockerfile') || contains(env.files, 'dockerfiles/')) && env.IS_FORK != 'true'
121144 uses : docker/build-push-action@v6
122145 with :
123146 context : ./dockerfiles
124- platforms : linux/amd64, linux/aarch64
147+ platforms : linux/amd64, linux/arm64
125148 push : true
126149 tags : ghcr.io/${{ env.REPO_NAME }}/jenkinsci-tutorials:simple_controller_${{ env.BRANCH }}
127150
128151 - name : Build and push the jenkins agent for maven tutorial
129152 # This step builds and pushes the Jenkins agent for the Maven tutorial
130- if : contains(env.files, 'dockerfiles/maven/Dockerfile')
153+ if : contains(env.files, 'dockerfiles/maven/') && env.IS_FORK != 'true'
131154 uses : docker/build-push-action@v6
132155 with :
133156 context : ./dockerfiles/maven
134- platforms : linux/amd64, linux/aarch64
157+ platforms : linux/amd64, linux/arm64
135158 push : true
136159 tags : ghcr.io/${{ env.REPO_NAME }}/jenkinsci-tutorials:maven_agent_${{ env.BRANCH }}
137160
138161 - name : Build and push the jenkins agent for python tutorial
139162 # This step builds and pushes the Jenkins agent for the Python tutorial
140- if : contains(env.files, 'dockerfiles/python/Dockerfile')
163+ if : contains(env.files, 'dockerfiles/python/') && env.IS_FORK != 'true'
141164 uses : docker/build-push-action@v6
142165 with :
143166 context : ./dockerfiles/python
144- platforms : linux/amd64, linux/aarch64
167+ platforms : linux/amd64, linux/arm64
145168 push : true
146169 tags : ghcr.io/${{ env.REPO_NAME }}/jenkinsci-tutorials:python_agent_${{ env.BRANCH }}
147170
148171 - name : Build and push the jenkins agent for node tutorial
149172 # This step builds and pushes the Jenkins agent for the Node.js tutorial
150- if : contains(env.files, 'dockerfiles/node/Dockerfile')
173+ if : contains(env.files, 'dockerfiles/node/') && env.IS_FORK != 'true'
151174 uses : docker/build-push-action@v6
152175 with :
153176 context : ./dockerfiles/node
154- platforms : linux/amd64, linux/aarch64
177+ platforms : linux/amd64, linux/arm64
155178 push : true
156179 tags : ghcr.io/${{ env.REPO_NAME }}/jenkinsci-tutorials:node_agent_${{ env.BRANCH }}
157180
158181 - name : Build and push the jenkins agent for the sidekick container
159182 # This step builds and pushes the Jenkins agent for the sidekick container
160- if : contains(env.files, 'dockerfiles/sidekick/Dockerfile')
183+ if : contains(env.files, 'dockerfiles/sidekick/') && env.IS_FORK != 'true'
161184 uses : docker/build-push-action@v6
162185 with :
163186 context : ./dockerfiles/sidekick
164- platforms : linux/amd64, linux/aarch64
187+ platforms : linux/amd64, linux/arm64
165188 push : true
166189 tags : ghcr.io/${{ env.REPO_NAME }}/jenkinsci-tutorials:sidekick_${{ env.BRANCH }}
167190
168191 - name : Build and push the jenkins agent for the agent-finding container
169192 # This step builds and pushes the Jenkins agent for the agent-finding container
170- if : contains(env.files, 'dockerfiles/agent-discovery/Dockerfile')
193+ if : contains(env.files, 'dockerfiles/agent-discovery/') && env.IS_FORK != 'true'
171194 uses : docker/build-push-action@v6
172195 with :
173196 context : ./dockerfiles/agent-discovery/
174- platforms : linux/amd64, linux/aarch64
197+ platforms : linux/amd64, linux/arm64
175198 push : true
176199 tags : ghcr.io/${{ env.REPO_NAME }}/jenkinsci-tutorials:agent_discovery_${{ env.BRANCH }}
177200
178201 - name : Build and push the jenkins agent for multi-branch controller
179202 # This step builds and pushes the Jenkins agent for the multi-branch controller
180- if : contains(env.files, 'dockerfiles/multi/Dockerfile')
203+ if : contains(env.files, 'dockerfiles/multi/') && env.IS_FORK != 'true'
181204 uses : docker/build-push-action@v6
182205 with :
183206 context : ./dockerfiles/multi
184- platforms : linux/amd64, linux/aarch64
207+ platforms : linux/amd64, linux/arm64
185208 push : true
186209 tags : ghcr.io/${{ env.REPO_NAME }}/jenkinsci-tutorials:multi_controller_${{ env.BRANCH }}
187210
188211 - name : Build and push the jenkins agent for Android
189212 # This step builds and pushes the Jenkins agent for Android
190- if : contains(env.files, 'dockerfiles/android/Dockerfile')
213+ if : contains(env.files, 'dockerfiles/android/') && env.IS_FORK != 'true'
191214 uses : docker/build-push-action@v6
192215 with :
193216 context : ./dockerfiles/android
@@ -197,7 +220,7 @@ jobs:
197220
198221 - name : Build and push the jenkins agent for golang tutorial
199222 # This step builds and pushes the Jenkins agent for the Golang tutorial
200- if : contains(env.files, 'dockerfiles/golang/Dockerfile')
223+ if : contains(env.files, 'dockerfiles/golang/') && env.IS_FORK != 'true'
201224 uses : docker/build-push-action@v6
202225 with :
203226 context : ./dockerfiles/golang
@@ -207,20 +230,20 @@ jobs:
207230
208231 - name : Build and push the jenkins agent for cpp tutorial
209232 # This step builds and pushes the Jenkins agent for the C++ tutorial
210- if : contains(env.files, 'dockerfiles/cpp/Dockerfile')
233+ if : contains(env.files, 'dockerfiles/cpp/') && env.IS_FORK != 'true'
211234 uses : docker/build-push-action@v6
212235 with :
213236 context : ./dockerfiles/cpp
214- platforms : linux/amd64, linux/aarch64
237+ platforms : linux/amd64, linux/arm64
215238 push : true
216239 tags : ghcr.io/${{ env.REPO_NAME }}/jenkinsci-tutorials:cpp_${{ env.BRANCH }}
217-
240+
218241 - name : Build and push the jenkins agent for dotnet tutorial
219- # This step builds and pushes the Jenkins agent for the C++ tutorial
220- if : contains(env.files, 'dockerfiles/dotnet/Dockerfile')
242+ # This step builds and pushes the Jenkins agent for the .NET tutorial
243+ if : contains(env.files, 'dockerfiles/dotnet/') && env.IS_FORK != 'true'
221244 uses : docker/build-push-action@v6
222245 with :
223246 context : ./dockerfiles/dotnet
224- platforms : linux/amd64, linux/aarch64
247+ platforms : linux/amd64, linux/arm64
225248 push : true
226249 tags : ghcr.io/${{ env.REPO_NAME }}/jenkinsci-tutorials:dotnet_${{ env.BRANCH }}
0 commit comments