11name : CI/CD
22
33on :
4- # push:
5- # branches-ignore:
6- # - master
74 workflow_call :
5+ inputs :
6+ publish :
7+ description : " Publish images to DockerHub and send the release Slack notification (set by the Combined workflow on master merge)."
8+ type : boolean
9+ default : false
10+ release_sha :
11+ description : " Commit SHA to reference in the notification (the merge commit on master)."
12+ type : string
13+ default : " "
814
915concurrency :
1016 group : ${{ github.workflow }}-${{ github.ref }}
7480 run : |
7581 IMAGE_TAG="${{ matrix.tags[0] }}"
7682 HAS_PREVIOUS=false
77- if [ "${{ github.ref }}" = "refs/heads/master" ]; then
83+ PUBLISH=false
84+ RELEASE_SHA="${{ inputs.release_sha }}"
85+ if [ -z "$RELEASE_SHA" ]; then RELEASE_SHA="${{ github.sha }}"; fi
86+ if [ "${{ github.ref }}" = "refs/heads/master" ] || [ "${{ inputs.publish }}" = "true" ]; then
87+ PUBLISH=true
7888 PREV_COMMIT_HASH=$(git rev-parse HEAD^1)
7989 if docker pull "$IMAGE_TAG" >/dev/null 2>&1; then HAS_PREVIOUS=true; fi
8090 else
8898 {
8999 echo "PREV_COMMIT_HASH=$PREV_COMMIT_HASH"
90100 echo "HAS_PREVIOUS=$HAS_PREVIOUS"
101+ echo "PUBLISH=$PUBLISH"
102+ echo "RELEASE_SHA=$RELEASE_SHA"
91103 } >> "$GITHUB_ENV"
92104
93105 - name : Set up QEMU
@@ -103,7 +115,7 @@ jobs:
103115 password : ${{ secrets.DOCKER_PASSWORD }}
104116
105117 - name : Re-tag images with previous commit hash
106- if : ${{ github.ref == 'refs/heads/master ' && env.HAS_PREVIOUS == 'true' }}
118+ if : ${{ env.PUBLISH == 'true ' && env.HAS_PREVIOUS == 'true' }}
107119 run : |
108120 PREV_HASH=${{ env.PREV_COMMIT_HASH }}
109121 if [ -z "$PREV_HASH" ]; then
@@ -123,14 +135,14 @@ jobs:
123135 id : docker_build
124136 uses : docker/build-push-action@v2
125137 with :
126- push : ${{ github.ref == 'refs/heads/master ' }}
127- load : ${{ github.ref != 'refs/heads/master ' }}
138+ push : ${{ env.PUBLISH == 'true ' }}
139+ load : ${{ env.PUBLISH != 'true ' }}
128140 file : ${{ matrix.image }}
129141 tags : ${{ join(matrix.tags) }}
130- platforms : ${{ github.ref == 'refs/heads/master ' && join(matrix.platforms) || 'linux/amd64' }}
142+ platforms : ${{ env.PUBLISH == 'true ' && join(matrix.platforms) || 'linux/amd64' }}
131143
132144 - name : Pull image for master branch
133- if : github.ref == 'refs/heads/master '
145+ if : env.PUBLISH == 'true '
134146 run : |
135147 echo "Pulling image for master branch"
136148 docker pull ${{ matrix.tags[0] }}
@@ -156,21 +168,26 @@ jobs:
156168
157169 - name : Fetch Job ID
158170 id : fetch_job_id
159- if : ${{ github.ref == 'refs/heads/master ' && env.FORMATTED_DIFF != '' }}
171+ if : ${{ env.PUBLISH == 'true ' && env.FORMATTED_DIFF != '' && steps.docker_build.outcome == 'success ' }}
160172 env :
161173 GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
162174 run : |
163175 JOBS_JSON=$(curl -s -H "Authorization: Bearer $GITHUB_TOKEN" \
164- "https://api.github.com/repos/${{ github.repository }}/actions/runs/${{ github.run_id }}/jobs")
165- echo "$JOBS_JSON" > jobs-response.json
166- cat jobs-response.json
167- JOB_ID=$(echo "$JOBS_JSON" | jq -r --arg name "${{ matrix.image }}" \
168- '.jobs[] | select(.name | contains($name)) | .id')
169- echo "Extracted Job ID: $JOB_ID"
170- echo "::set-output name=job_id::$JOB_ID"
176+ "https://api.github.com/repos/${{ github.repository }}/actions/runs/${{ github.run_id }}/jobs?per_page=100")
177+ JOB=$(echo "$JOBS_JSON" | jq -r --arg name "${{ matrix.image }}" \
178+ '.jobs[] | select(.name | contains($name))')
179+ JOB_ID=$(echo "$JOB" | jq -r '.id')
180+ MANIFEST_STEP=$(echo "$JOB" | jq -r '.steps[] | select(.name=="Current image report") | .number')
181+ DIFF_STEP=$(echo "$JOB" | jq -r '.steps[] | select(.name=="Run the diff and format output") | .number')
182+ echo "Extracted Job ID: $JOB_ID (manifest step $MANIFEST_STEP, diff step $DIFF_STEP)"
183+ {
184+ echo "job_id=$JOB_ID"
185+ echo "manifest_step=$MANIFEST_STEP"
186+ echo "diff_step=$DIFF_STEP"
187+ } >> "$GITHUB_OUTPUT"
171188
172189 - name : Send Slack Notification
173- if : ${{ github.ref == 'refs/heads/master ' && env.FORMATTED_DIFF != '' }}
190+ if : ${{ env.PUBLISH == 'true ' && env.FORMATTED_DIFF != '' && steps.docker_build.outcome == 'success ' }}
174191 uses : slackapi/slack-github-action@v1.24.0
175192 with :
176193 payload : |
@@ -181,7 +198,7 @@ jobs:
181198 "fields": [
182199 {
183200 "title": "New version of ${{ matrix.tags[0] }} has been published",
184- "value": "You can check the:\n- *Manifest*: <https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}/job/${{ steps.fetch_job_id.outputs.job_id }}#step:9 :1|View Manifest>\n- *Diff*: <https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}/job/${{ steps.fetch_job_id.outputs.job_id }}#step:11:7 |View Diff>\n\nThis version was built out of <https://github.com/${{ github.repository }}/commit/${{ github.sha }}|${{ github.sha }}>.",
201+ "value": "You can check the:\n- *Manifest*: <https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}/job/${{ steps.fetch_job_id.outputs.job_id }}#step:${{ steps.fetch_job_id.outputs.manifest_step }} :1|View Manifest>\n- *Diff*: <https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}/job/${{ steps.fetch_job_id.outputs.job_id }}#step:${{ steps.fetch_job_id.outputs.diff_step }}:1 |View Diff>\n\nThis version was built out of <https://github.com/${{ github.repository }}/commit/${{ env.RELEASE_SHA }}|${{ env.RELEASE_SHA }}>.",
185202 "short": false
186203 }
187204 ]
0 commit comments