@@ -52,61 +52,92 @@ jobs:
5252 docker compose -f docker/docker-compose.yml build --pull
5353
5454 - name : Tag & push built images
55- # This step:
56- # 1. lists services from docker compose
57- # 2. obtains the built image ID for each service
58- # 3. tags each image as: $REGISTRY/$OWNER/<service>:<sha>
59- # 4. pushes the tag, and also pushes :latest when on main or when the ref is a tag
6055 run : |
6156 set -euo pipefail
6257
6358 REGISTRY=${{ env.REGISTRY }}
6459 OWNER=${{ env.OWNER }}
6560 SHA=${{ env.COMMIT_SHORT }}
61+ REPO_NAME=${{ github.event.repository.name }}
6662
6763 echo "Registry: $REGISTRY"
6864 echo "Owner: $OWNER"
65+ echo "Repo: $REPO_NAME"
6966 echo "Tag: $SHA"
7067
71- # get list of services defined in compose
68+ # list services defined in compose
7269 services=$(docker compose -f docker/docker-compose.yml config --services)
7370 if [ -z "$services" ]; then
7471 echo "No services found in docker-compose.yml"
7572 exit 1
7673 fi
7774
7875 # decide whether to push 'latest' as well
79- ref="${GITHUB_REF:-}"
8076 push_latest=false
81- if [[ "${GITHUB_REF}" == "refs/heads/main" ]] || [[ "${GITHUB_REF}" == refs/tags/* ]]; then
77+ if [[ "${GITHUB_REF:- }" == "refs/heads/main" ]] || [[ "${GITHUB_REF:- }" == refs/tags/* ]]; then
8278 push_latest=true
8379 fi
8480
8581 for svc in $services; do
86- echo "Processing service: $svc"
82+ echo "Checking service: $svc"
83+
84+ # Extract IMAGE and whether a build: exists for this service from 'docker compose config'
85+ svc_info=$(docker compose -f docker/docker-compose.yml config | awk -v svc="$svc" '
86+ $0 ~ "services:" { in_services=1 }
87+ in_services && $0 ~ "^[[:space:]]*"svc":" { in_svc=1; next }
88+ in_svc && $0 ~ "^[[:space:]]*[^[:space:]]" { exit }
89+ in_svc {
90+ if ($1 == "image:") { gsub(/"/,"",$2); print "IMAGE="$2 }
91+ if ($1 == "build:") { print "HASBUILD=1" }
92+ }
93+ ')
94+
95+ # evaluate the small KEY=VAL output (safe in CI)
96+ unset IMAGE HASBUILD
97+ eval "$svc_info" || true
98+
99+ # only process services that have both build: and image:
100+ if [ -z "${IMAGE:-}" ] || [ -z "${HASBUILD:-}" ]; then
101+ echo "Skipping $svc (requires both build: and image:)."
102+ continue
103+ fi
104+
105+ echo "Service $svc has build and image -> $IMAGE"
87106
88- # image id produced by compose build for this service
107+ # determine source reference for tagging: prefer compose image id, then image:latest, then image (no tag)
108+ src_ref=""
89109 imgid=$(docker compose -f docker/docker-compose.yml images -q "$svc" 2>/dev/null || true)
90- if [ -z "$imgid" ]; then
91- echo "Failed to get image id for service $svc"
110+ if [ -n "$imgid" ]; then
111+ src_ref="$imgid"
112+ elif docker image inspect "$IMAGE:latest" >/dev/null 2>&1; then
113+ src_ref="$IMAGE:latest"
114+ elif docker image inspect "$IMAGE" >/dev/null 2>&1; then
115+ src_ref="$IMAGE"
116+ else
117+ echo "No built image found for $svc (expected $IMAGE). Did the build step succeed?"
92118 exit 1
93119 fi
94120
95- target="$REGISTRY/$OWNER/${{ github.event.repository.name } }-$svc:$SHA"
96- echo "Tagging $imgid -> $target"
97- docker tag "$imgid " "$target"
121+ target="$REGISTRY/$OWNER/${REPO_NAME }-$svc:$SHA"
122+ echo "Tagging $src_ref -> $target"
123+ docker tag "$src_ref " "$target"
98124
99125 echo "Pushing $target"
100126 docker push "$target"
101127
102128 if [ "$push_latest" = true ]; then
103- latest_tag="$REGISTRY/$OWNER/${{ github.event.repository.name } }-$svc:latest"
129+ latest_tag="$REGISTRY/$OWNER/${REPO_NAME }-$svc:latest"
104130 echo "Also tagging & pushing latest: $latest_tag"
105- docker tag "$imgid " "$latest_tag"
131+ docker tag "$src_ref " "$latest_tag"
106132 docker push "$latest_tag"
107133 fi
134+
135+ # clean up variables for next iteration
136+ unset IMAGE HASBUILD src_ref imgid
108137 done
109138
139+ echo "All done. Images pushed."
140+
110141 - name : Output pushed images
111142 run : |
112143 echo "Images pushed for commit $COMMIT_SHORT"
0 commit comments