Skip to content

Commit d29a2c9

Browse files
committed
feat: reworked tag in docker build pipeline
1 parent 56893bf commit d29a2c9

File tree

3 files changed

+84
-6
lines changed

3 files changed

+84
-6
lines changed

.github/actions/svu/action.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ runs:
9595
fi
9696
9797
echo -e "${YELLOW}[ EXECUTING ] > Calculating current and next tag${BLANK}"
98-
CURRENT_TAG=$(svu current --tag.prefix="${COMPONENT}v" --tag.pattern="${COMPONENT}v[0-9]*.[0-9]*.[0-9]*${PRERELEASE_PATTERN}")
98+
CURRENT_TAG=$(svu current --tag.prefix="${COMPONENT}v" --tag.pattern="${COMPONENT}v[0-9]*.[0-9]*.[0-9]*${PRERELEASE_PATTERN}*")
9999
if [ $? -ne 0 ]
100100
then
101101
CURRENT_TAG=""

.github/workflows/__docker-build__.yaml

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,51 @@ jobs:
169169
password: ${{ secrets.registry-password }}
170170
logout: false
171171

172+
- name: Define Docker image tag
173+
id: define-tag
174+
run: |
175+
echo -e "${BLUE}[ STEP - DEFINE TAG ] > Defining target tag${BLANK}"
176+
if [[ "${{ inputs.component }}" == '' ]]
177+
then
178+
TARGET_TAG="sha-${GITHUB_SHA:0:7}"
179+
else
180+
TARGET_TAG="${{ inputs.component }}-sha-${GITHUB_SHA:0:7}"
181+
fi
182+
echo -e "${YELLOW}[ INFO ] > Target tag is ${PURPLE}${TARGET_TAG}${BLANK}"
183+
echo "TARGET_TAG=${TARGET_TAG}" >> "$GITHUB_OUTPUT"
184+
shell: bash
185+
env:
186+
RED: \033[1;31m
187+
GREEN: \033[1;32m
188+
YELLOW: \033[1;33m
189+
BLUE: \033[1;34m
190+
PURPLE: \033[1;35m
191+
CYAN: \033[1;36m
192+
BLANK: \033[0m
193+
194+
- name: Define Docker image PR tag
195+
id: define-pr-tag
196+
if: github.event_name == 'pull_request'
197+
run: |
198+
echo -e "${BLUE}[ STEP - DEFINE TAG ] > Defining target PR tag${BLANK}"
199+
if [[ "${{ inputs.component }}" == '' ]]
200+
then
201+
TARGET_PR_TAG="pr-${{ github.event.pull_request.number }}"
202+
else
203+
TARGET_PR_TAG="${{ inputs.component }}-pr-${{ github.event.pull_request.number }}"
204+
fi
205+
echo -e "${YELLOW}[ INFO ] > Target PR tag is ${PURPLE}${TARGET_PR_TAG}${BLANK}"
206+
echo "TARGET_PR_TAG=${TARGET_PR_TAG}" >> "$GITHUB_OUTPUT"
207+
shell: bash
208+
env:
209+
RED: \033[1;31m
210+
GREEN: \033[1;32m
211+
YELLOW: \033[1;33m
212+
BLUE: \033[1;34m
213+
PURPLE: \033[1;35m
214+
CYAN: \033[1;36m
215+
BLANK: \033[0m
216+
172217
- name: Build Docker img [PR]
173218
uses: docker/build-push-action@v5
174219
if: github.event_name == 'pull_request'
@@ -184,7 +229,7 @@ jobs:
184229
build-args: |
185230
${{ inputs.build-arg }}
186231
tags: |
187-
${{ inputs.cache-registry }}/${{ inputs.img-name }}:pr-${{ github.event.pull_request.number }}
232+
${{ inputs.cache-registry }}/${{ inputs.img-name }}:${{ steps.define-pr-tag.outputs.TARGET_PR_TAG }}
188233
189234
- name: Calculate current and next tag
190235
id: svu
@@ -211,5 +256,5 @@ jobs:
211256
build-args: |
212257
${{ inputs.build-arg }}
213258
tags: |
214-
${{ inputs.cache-registry }}/${{ inputs.img-name }}:sha-${{ github.sha }}
259+
${{ inputs.cache-registry }}/${{ inputs.img-name }}:${{ steps.define-tag.outputs.TARGET_TAG }}
215260
${{ inputs.cache-registry }}/${{ inputs.img-name }}:${{ steps.svu.outputs.NEW_TAG }}

.github/workflows/__docker-release__.yaml

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,39 @@ jobs:
132132
pattern: "${{ inputs.component }}-v?[0-9]{1,2}\\.[0-9]{1,3}\\.[0-9]{1,3}$"
133133
starting-tag: ""
134134

135+
- name: Define Last built SHA
136+
id: define-sha
137+
run: |
138+
echo -e "${BLUE}[ STEP - DEFINE LAST BUILT SHA ] > Defining last SHA${BLANK}"
139+
if [[ "${{ inputs.component }}" == '' ]]
140+
then
141+
git log -n 1 -- ${{ inputs.workingdir }}
142+
SHA=$(git log -n 1 --pretty=format:"%H" -- ${{ inputs.workingdir }})
143+
LAST_TAG_SHA="sha-${GITHUB_SHA:0:7}"
144+
else
145+
git log -n 1 -- ${{ inputs.workingdir }}
146+
SHA=$(git log -n 1 --pretty=format:"%H" -- ${{ inputs.workingdir }})
147+
LAST_TAG_SHA="${{ inputs.component }}-sha-${SHA:0:7}"
148+
fi
149+
if [[ "${SHA}" != ${{ github.sha }} ]]
150+
then
151+
echo -e "${YELLOW}[ INFO ] > Last sha is different from current sha ${PURPLE}${LAST_SHA}${BLANK}"
152+
echo "LAST_SHA=${SHA}" >> "$GITHUB_OUTPUT"
153+
else
154+
echo -e "${YELLOW}[ INFO ] > Last sha is same as current sha, using github.ref_name${BLANK}"
155+
fi
156+
echo -e "${YELLOW}[ INFO ] > Last sha tag is ${PURPLE}${LAST_TAG_SHA}${BLANK}"
157+
echo "LAST_TAG_SHA=${LAST_TAG_SHA}" >> "$GITHUB_OUTPUT"
158+
shell: bash
159+
env:
160+
RED: \033[1;31m
161+
GREEN: \033[1;32m
162+
YELLOW: \033[1;33m
163+
BLUE: \033[1;34m
164+
PURPLE: \033[1;35m
165+
CYAN: \033[1;36m
166+
BLANK: \033[0m
167+
135168
- name: Skopeo retag [ Artefact promotion ]
136169
uses: ixxeL-DevOps/gha-templates/.github/actions/skopeo-retag@main
137170
id: skopeo-retag
@@ -140,7 +173,7 @@ jobs:
140173
registry-url: ${{ inputs.registry-url }}
141174
registry-username: ${{ inputs.registry-username }}
142175
registry-password: ${{ secrets.registry-password }}
143-
src-img: ${{ inputs.cache-registry }}/${{ inputs.img-name }}:sha-${{ github.sha }}
176+
src-img: ${{ inputs.cache-registry }}/${{ inputs.img-name }}:${{ steps.define-sha.outputs.LAST_TAG_SHA }}
144177
dst-img: ${{ inputs.cache-registry }}/${{ inputs.img-name }}:${{ steps.svu.outputs.NEW_TAG }}
145178

146179
- name: Crane retag [ Artefact promotion ]
@@ -151,15 +184,15 @@ jobs:
151184
registry-url: ${{ inputs.registry-url }}
152185
registry-username: ${{ inputs.registry-username }}
153186
registry-password: ${{ secrets.registry-password }}
154-
src-img: ${{ inputs.cache-registry }}/${{ inputs.img-name }}:sha-${{ github.sha }}
187+
src-img: ${{ inputs.cache-registry }}/${{ inputs.img-name }}:${{ steps.define-sha.outputs.LAST_TAG_SHA }}
155188
dst-tag: ${{ steps.svu.outputs.NEW_TAG }}
156189

157190
- name: Git tag
158191
id: git-tag
159192
uses: ixxeL-DevOps/gha-templates/.github/actions/git-tag@main
160193
with:
161194
tag-value: ${{ steps.svu.outputs.NEW_TAG }}
162-
commit-sha: ${{ github.ref_name }}
195+
commit-sha: ${{ steps.define-sha.outputs.LAST_SHA || github.ref_name }}
163196
git-workdir: ${{ inputs.git-workdir }}
164197
force-tag: "false"
165198

0 commit comments

Comments
 (0)