Skip to content

Commit 34ee0fd

Browse files
feat(ci-cd): update Docker image build process to handle multiple services in a single step
1 parent 364d4da commit 34ee0fd

1 file changed

Lines changed: 41 additions & 33 deletions

File tree

.github/workflows/ci-cd.yml

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ env:
1515

1616
jobs:
1717
build-and-test:
18-
name: Run Tests
18+
name: build and test
1919
runs-on: ${{ matrix.os }}
2020

2121
defaults:
@@ -217,19 +217,9 @@ jobs:
217217
# done
218218

219219
push-docker-images:
220-
runs-on: ubuntu-latest
220+
runs-on: ubuntu-22.04
221221
name: Build and Push Docker Images
222-
needs: build-and-test # This ensures tests must pass before building
223-
224-
strategy:
225-
matrix:
226-
service:
227-
- name: "api_gateway"
228-
dockerfile: "./api_gateway/Dockerfile"
229-
suffix: "api_gateway"
230-
- name: "service"
231-
dockerfile: "./service/Dockerfile"
232-
suffix: "service"
222+
needs: build-and-test
233223

234224
steps:
235225
- name: Checkout code
@@ -244,23 +234,41 @@ jobs:
244234
username: ${{ secrets.DOCKERHUB_USERNAME }}
245235
password: ${{ secrets.DOCKERHUB_PASSWORD }}
246236

247-
- name: Extract metadata for ${{ matrix.service.name }}
248-
id: meta
249-
uses: docker/metadata-action@v5
250-
with:
251-
images: ${{ env.DOCKER_REPOSITORY }}
252-
tags: |
253-
type=raw,value=${{ matrix.service.suffix }}-latest,enable=${{ github.ref == 'refs/heads/master' }}
254-
type=raw,value=${{ matrix.service.suffix }}-dev-latest,enable=${{ github.ref == 'refs/heads/dev' }}
255-
type=sha,prefix=${{ github.ref_name }}-,suffix=-${{ matrix.service.suffix }}
256-
257-
- name: Build and push ${{ matrix.service.name }}
258-
uses: docker/build-push-action@v5
259-
with:
260-
context: .
261-
file: ${{ matrix.service.dockerfile }}
262-
push: true
263-
tags: ${{ steps.meta.outputs.tags }}
264-
labels: ${{ steps.meta.outputs.labels }}
265-
cache-from: type=gha
266-
cache-to: type=gha,mode=max
237+
- name: Build and push Docker images for all services
238+
env:
239+
DOCKER_REPOSITORY: ${{ env.DOCKER_REPOSITORY }}
240+
run: |
241+
services=(
242+
"api_gateway ./api_gateway/Dockerfile api_gateway"
243+
"service ./service/Dockerfile service"
244+
)
245+
246+
for service in "${services[@]}"; do
247+
set -- $service
248+
name=$1
249+
dockerfile=$2
250+
suffix=$3
251+
252+
echo "Building and pushing $name..."
253+
254+
# Extract tags
255+
tags=""
256+
if [ "${GITHUB_REF}" = "refs/heads/master" ]; then
257+
tags="${suffix}-latest"
258+
elif [ "${GITHUB_REF}" = "refs/heads/dev" ]; then
259+
tags="${suffix}-dev-latest"
260+
fi
261+
tags="${tags} ${GITHUB_REF_NAME}-${GITHUB_SHA:0:7}-${suffix}"
262+
263+
# Build and push
264+
docker buildx build \
265+
--file "$dockerfile" \
266+
--push \
267+
--tag "${DOCKER_REPOSITORY}:${tags// / --tag ${DOCKER_REPOSITORY}:}" \
268+
--label "org.opencontainers.image.source=${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}" \
269+
--cache-from type=gha \
270+
--cache-to type=gha,mode=max \
271+
.
272+
273+
echo "$name image pushed with tags: $tags"
274+
done

0 commit comments

Comments
 (0)