11name : docker
22
3- # run on pushes to main and on tags; adjust to taste
43on :
54 push :
65 branches : [ main ]
1615 build-and-push :
1716 runs-on : ubuntu-latest
1817 env :
19- # registry to push to. Change to docker.io if you prefer.
20- REGISTRY : ghcr.io
21- # owner that will own the pushed images. Defaults to repository owner.
22- OWNER : ${{ github.repository_owner }}
23- # short commit SHA tag
18+ REGISTRY : docker.io
19+ OWNER : mrminede
2420 COMMIT_SHORT : ${{ github.sha }}
2521 steps :
2622 - name : Shorten commit SHA
3228 with :
3329 fetch-depth : 0
3430
35- - name : Set up QEMU (optional for multi-arch builds)
31+ - name : Set up QEMU
3632 uses : docker/setup-qemu-action@v2
3733
3834 - name : Set up Docker Buildx
@@ -41,103 +37,11 @@ jobs:
4137 - name : Login to container registry
4238 uses : docker/login-action@v2
4339 with :
44- registry : ${{ env.REGISTRY }}
45- username : ${{ github.actor }}
46- # For GHCR the GITHUB_TOKEN usually works. For Docker Hub use a secret (DOCKERHUB_USERNAME/DOCKERHUB_TOKEN).
47- password : ${{ secrets.GITHUB_TOKEN }}
40+ username : ${{ secrets.DOCKERHUB_USERNAME }}
41+ password : ${{ secrets.DOCKERHUB_TOKEN }}
4842
49- - name : Build all compose services
50- # uses the Docker Compose CLI shipped with Docker; this runs 'docker compose build'
51- run : |
52- docker compose -f docker/docker-compose.yml build --pull
43+ - name : Build images
44+ run : docker compose -f docker/docker-compose.yml build --pull
5345
54- - name : Tag & push built images
55- run : |
56- set -euo pipefail
57-
58- REGISTRY=${{ env.REGISTRY }}
59- OWNER=${{ env.OWNER }}
60- SHA=${{ env.COMMIT_SHORT }}
61- REPO_NAME=${{ github.event.repository.name }}
62-
63- echo "Registry: $REGISTRY"
64- echo "Owner: $OWNER"
65- echo "Repo: $REPO_NAME"
66- echo "Tag: $SHA"
67-
68- # list services defined in compose
69- services=$(docker compose -f docker/docker-compose.yml config --services)
70- if [ -z "$services" ]; then
71- echo "No services found in docker-compose.yml"
72- exit 1
73- fi
74-
75- # decide whether to push 'latest' as well
76- push_latest=false
77- if [[ "${GITHUB_REF:-}" == "refs/heads/main" ]] || [[ "${GITHUB_REF:-}" == refs/tags/* ]]; then
78- push_latest=true
79- fi
80-
81- for svc in $services; do
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"
106-
107- # determine source reference for tagging: prefer compose image id, then image:latest, then image (no tag)
108- src_ref=""
109- imgid=$(docker compose -f docker/docker-compose.yml images -q "$svc" 2>/dev/null || true)
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?"
118- exit 1
119- fi
120-
121- target="$REGISTRY/$OWNER/${REPO_NAME}-$svc:$SHA"
122- echo "Tagging $src_ref -> $target"
123- docker tag "$src_ref" "$target"
124-
125- echo "Pushing $target"
126- docker push "$target"
127-
128- if [ "$push_latest" = true ]; then
129- latest_tag="$REGISTRY/$OWNER/${REPO_NAME}-$svc:latest"
130- echo "Also tagging & pushing latest: $latest_tag"
131- docker tag "$src_ref" "$latest_tag"
132- docker push "$latest_tag"
133- fi
134-
135- # clean up variables for next iteration
136- unset IMAGE HASBUILD src_ref imgid
137- done
138-
139- echo "All done. Images pushed."
140-
141- - name : Output pushed images
142- run : |
143- echo "Images pushed for commit $COMMIT_SHORT"
46+ - name : Push images
47+ run : docker compose -f docker/docker-compose.yml push
0 commit comments