@@ -217,9 +217,19 @@ jobs:
217217 # done
218218
219219 push-docker-images :
220- runs-on : ubuntu-22.04
220+ runs-on : ubuntu-latest
221221 name : Build and Push Docker Images
222- needs : build-and-test
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"
223233
224234 steps :
225235 - name : Checkout code
@@ -234,41 +244,23 @@ jobs:
234244 username : ${{ secrets.DOCKERHUB_USERNAME }}
235245 password : ${{ secrets.DOCKERHUB_PASSWORD }}
236246
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
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
0 commit comments