Skip to content

Commit b76d16d

Browse files
authored
Chore(ci): Proper tag for docker img (#182)
* proper tag * add build on commit to main and add logic for tag latest & numeric tag
1 parent 15430b0 commit b76d16d

File tree

1 file changed

+52
-43
lines changed

1 file changed

+52
-43
lines changed

.github/workflows/build_and_push.yml

Lines changed: 52 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@ name: Build and Push Multi-Arch Images
22

33
on:
44
push:
5+
branches:
6+
- main
57
tags:
68
- 'v[0-9]+.[0-9]+.[0-9]+'
79

810
env:
911
REGISTRY_IMAGE: ghcr.io/lay3rlabs/layer-sdk
10-
IMAGE_TAG: ${{ github.sha }}
1112

1213
jobs:
1314
build_amd64:
1415
name: Build and Push AMD64 Image
1516
runs-on: linux-8-core
17+
if: ${{ github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v') }}
1618
permissions:
1719
contents: read
1820
packages: write
@@ -21,6 +23,20 @@ jobs:
2123
- name: Checkout the repo
2224
uses: actions/checkout@v4
2325

26+
- name: Set IMAGE_TAG
27+
shell: bash
28+
run: |
29+
if [[ "${GITHUB_REF_TYPE}" == "tag" ]]; then
30+
TAG="${GITHUB_REF_NAME#v}"
31+
IMAGE_TAG="${TAG//[^a-zA-Z0-9_.-]/-}"
32+
elif [[ "${GITHUB_REF_NAME}" == "main" ]]; then
33+
IMAGE_TAG="latest"
34+
else
35+
echo "Not on main branch or tag, skipping build."
36+
exit 1
37+
fi
38+
echo "IMAGE_TAG=${IMAGE_TAG}" >> $GITHUB_ENV
39+
2440
- name: Log in to the Container registry
2541
uses: docker/login-action@v3
2642
with:
@@ -33,14 +49,15 @@ jobs:
3349
with:
3450
context: .
3551
file: ./docker/Dockerfile.slay3rd
36-
push: ${{ github.event_name != 'pull_request' }}
52+
push: true
3753
tags: ${{ env.REGISTRY_IMAGE }}:${{ env.IMAGE_TAG }}-amd64
3854
platforms: linux/amd64
3955
provenance: false
4056

4157
build_arm64:
4258
name: Build and Push ARM64 Image
4359
runs-on: arm64-builder
60+
if: ${{ github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v') }}
4461
permissions:
4562
contents: read
4663
packages: write
@@ -49,6 +66,20 @@ jobs:
4966
- name: Checkout the repo
5067
uses: actions/checkout@v4
5168

69+
- name: Set IMAGE_TAG
70+
shell: bash
71+
run: |
72+
if [[ "${GITHUB_REF_TYPE}" == "tag" ]]; then
73+
TAG="${GITHUB_REF_NAME#v}"
74+
IMAGE_TAG="${TAG//[^a-zA-Z0-9_.-]/-}"
75+
elif [[ "${GITHUB_REF_NAME}" == "main" ]]; then
76+
IMAGE_TAG="latest"
77+
else
78+
echo "Not on main branch or tag, skipping build."
79+
exit 1
80+
fi
81+
echo "IMAGE_TAG=${IMAGE_TAG}" >> $GITHUB_ENV
82+
5283
- name: Log in to the Container registry
5384
uses: docker/login-action@v3
5485
with:
@@ -61,7 +92,7 @@ jobs:
6192
with:
6293
context: .
6394
file: ./docker/Dockerfile.slay3rd
64-
push: ${{ github.event_name != 'pull_request' }}
95+
push: true
6596
tags: ${{ env.REGISTRY_IMAGE }}:${{ env.IMAGE_TAG }}-arm64
6697
platforms: linux/arm64
6798
provenance: false
@@ -70,7 +101,7 @@ jobs:
70101
name: Create and Push Multi-Arch Manifest
71102
runs-on: ubuntu-latest
72103
needs: [build_amd64, build_arm64]
73-
if: ${{ github.event_name != 'pull_request' }}
104+
if: ${{ github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v') }}
74105
permissions:
75106
contents: read
76107
packages: write
@@ -79,14 +110,19 @@ jobs:
79110
- name: Checkout the repo
80111
uses: actions/checkout@v4
81112

82-
- name: Docker metadata
83-
id: meta
84-
uses: docker/metadata-action@v5
85-
with:
86-
images: ${{ env.REGISTRY_IMAGE }}
87-
tags: |
88-
type=ref,event=branch
89-
type=sha
113+
- name: Set IMAGE_TAG
114+
shell: bash
115+
run: |
116+
if [[ "${GITHUB_REF_TYPE}" == "tag" ]]; then
117+
TAG="${GITHUB_REF_NAME#v}"
118+
IMAGE_TAG="${TAG//[^a-zA-Z0-9_.-]/-}"
119+
elif [[ "${GITHUB_REF_NAME}" == "main" ]]; then
120+
IMAGE_TAG="latest"
121+
else
122+
echo "Not on main branch or tag, skipping manifest creation."
123+
exit 1
124+
fi
125+
echo "IMAGE_TAG=${IMAGE_TAG}" >> $GITHUB_ENV
90126
91127
- name: Set up Docker Buildx
92128
uses: docker/setup-buildx-action@v3
@@ -99,35 +135,8 @@ jobs:
99135
password: ${{ secrets.GITHUB_TOKEN }}
100136

101137
- name: Create and push multi-arch manifest
102-
env:
103-
REGISTRY_IMAGE: ${{ env.REGISTRY_IMAGE }}
104-
IMAGE_TAG: ${{ env.IMAGE_TAG }}
105-
TAGS: ${{ steps.meta.outputs.tags }}
106138
run: |
107-
echo "REGISTRY_IMAGE: $REGISTRY_IMAGE"
108-
echo "IMAGE_TAG: $IMAGE_TAG"
109-
echo "TAGS: $TAGS"
110-
111-
# Initialize TAG_ARGS with the primary tag
112-
TAG_ARGS="-t ${REGISTRY_IMAGE}:${IMAGE_TAG}"
113-
114-
# Check if TAGS is not empty
115-
if [ -n "$TAGS" ]; then
116-
# Split TAGS into an array
117-
IFS=',' read -ra TAG_ARRAY <<< "$TAGS"
118-
for FULL_TAG in "${TAG_ARRAY[@]}"; do
119-
# Extract the tag name from the full image reference
120-
TAG=${FULL_TAG##*:}
121-
# Append to TAG_ARGS if TAG is not empty
122-
if [ -n "$TAG" ]; then
123-
TAG_ARGS="$TAG_ARGS -t ${REGISTRY_IMAGE}:$TAG"
124-
fi
125-
done
126-
fi
127-
128-
echo "TAG_ARGS: $TAG_ARGS"
129-
130-
# Create and push the multi-arch manifest with all tags
131-
docker buildx imagetools create $TAG_ARGS \
132-
${REGISTRY_IMAGE}:${IMAGE_TAG}-amd64 \
133-
${REGISTRY_IMAGE}:${IMAGE_TAG}-arm64
139+
docker buildx imagetools create \
140+
--tag ${{ env.REGISTRY_IMAGE }}:${{ env.IMAGE_TAG }} \
141+
${{ env.REGISTRY_IMAGE }}:${{ env.IMAGE_TAG }}-amd64 \
142+
${{ env.REGISTRY_IMAGE }}:${{ env.IMAGE_TAG }}-arm64

0 commit comments

Comments
 (0)