11name : Build Docker image
22
33on :
4- pull_request :
5- types : [opened, synchronize, reopened, ready_for_review]
6- paths :
7- - .github/workflows/build-docker.yml
8- - docker/**
9- - src/**
10- - .dockerignore
11- - LICENSE
12- - README.md
13- - pyproject.toml
14- - uv.lock
15- push :
16- branches : [main]
17- paths :
18- - .github/workflows/build-docker.yml
19- - docker/**
20- - src/**
21- - .dockerignore
22- - LICENSE
23- - README.md
24- - pyproject.toml
25- - uv.lock
26- release :
27- types : [published]
284 workflow_dispatch :
295
306permissions :
317 contents : read
328
339concurrency :
34- group : docker-${{ github.event.release.tag_name || github.head_ref || github. ref }}
35- cancel-in-progress : ${{ github.event_name != 'release' }}
10+ group : docker-${{ github.ref }}
11+ cancel-in-progress : true
3612
3713env :
3814 IMAGE_NAME : powercontext-server
5531
5632 - name : Resolve image metadata
5733 id : metadata
58- env :
59- RELEASE_TAG : ${{ github.event.release.tag_name }}
6034 run : |
6135 python - <<'PY'
6236 import os
6842 project = tomllib.loads(Path("pyproject.toml").read_text())
6943 package_version = project["project"]["version"]
7044 source_sha = subprocess.check_output(["git", "rev-parse", "HEAD"], text=True).strip()
71- release_tag = os.environ.get("RELEASE_TAG", "")
72-
73- if release_tag:
74- release_version = release_tag.removeprefix("v")
75- if not re.fullmatch(r"[0-9]+\.[0-9]+\.[0-9]+(?:[-+][0-9A-Za-z.-]+)?", release_version):
76- raise SystemExit("Release tag must use vX.Y.Z or X.Y.Z semantic versioning")
77- package_version = release_version
78-
79- pyproject_path = Path("pyproject.toml")
80- pyproject_text = pyproject_path.read_text()
81- updated = re.sub(
82- r'^version = ".*"$',
83- f'version = "{release_version}"',
84- pyproject_text,
85- count=1,
86- flags=re.MULTILINE,
87- )
88- if updated == pyproject_text:
89- raise SystemExit("Could not update the project version for the Release image")
90- pyproject_path.write_text(updated)
9145
9246 safe_version = re.sub(r"[^A-Za-z0-9_.-]+", "-", package_version).strip("-.")
9347 if not safe_version:
10256 PY
10357
10458 - name : Build Docker image archives
105- if : github.event_name != 'release'
10659 env :
10760 IMAGE_TAG : ${{ steps.metadata.outputs.image_tag }}
10861 run : |
12376 done
12477
12578 - name : Smoke test Linux amd64 image
126- if : github.event_name != 'release'
12779 env :
12880 IMAGE_TAG : ${{ steps.metadata.outputs.image_tag }}
12981 run : |
@@ -154,7 +106,6 @@ jobs:
154106 exit 1
155107
156108 - name : Upload Docker image archives
157- if : github.event_name != 'release'
158109 uses : actions/upload-artifact@v7
159110 with :
160111 name : ${{ steps.metadata.outputs.artifact_name }}
@@ -163,64 +114,15 @@ jobs:
163114 if-no-files-found : error
164115 retention-days : 30
165116
166- - name : Validate Docker Hub configuration
167- if : github.event_name == 'release'
168- env :
169- DOCKER_PUSH_BASE : ${{ vars.DOCKER_PUSH_BASE }}
170- run : |
171- if [ -z "$DOCKER_PUSH_BASE" ]; then
172- echo "Repository variable DOCKER_PUSH_BASE is required for Release publishing" >&2
173- exit 1
174- fi
175-
176- - name : Resolve Release image tags
177- if : github.event_name == 'release'
178- id : release-tags
179- uses : docker/metadata-action@v5
180- with :
181- images : ${{ vars.DOCKER_PUSH_BASE }}/powercontext-server
182- tags : |
183- type=semver,pattern={{version}},value=${{ github.event.release.tag_name }}
184- type=raw,value=latest,enable=${{ github.event.release.prerelease == false }}
185-
186- - name : Log in to Docker Hub
187- if : github.event_name == 'release'
188- uses : docker/login-action@v3
189- with :
190- username : ${{ secrets.DOCKER_USERNAME }}
191- password : ${{ secrets.DOCKER_PASSWORD }}
192-
193- - name : Build and push Release image
194- if : github.event_name == 'release'
195- uses : docker/build-push-action@v6
196- with :
197- context : .
198- file : docker/Dockerfile
199- platforms : linux/amd64,linux/arm64
200- push : true
201- tags : ${{ steps.release-tags.outputs.tags }}
202- labels : ${{ steps.release-tags.outputs.labels }}
203- cache-from : type=gha,scope=docker-release
204- cache-to : type=gha,mode=max,scope=docker-release
205-
206117 - name : Summarize build
207118 env :
208119 ARTIFACT_NAME : ${{ steps.metadata.outputs.artifact_name }}
209- IMAGE_TAGS : ${{ steps.release-tags.outputs.tags }}
210120 SOURCE_SHA : ${{ steps.metadata.outputs.source_sha }}
211121 run : |
212122 {
213123 echo "## Docker image"
214124 echo
215125 echo "- Source SHA: \`$SOURCE_SHA\`"
216- if [ "$GITHUB_EVENT_NAME" = "release" ]; then
217- echo "- Published tags:"
218- while IFS= read -r tag; do
219- echo " - \`$tag\`"
220- done <<< "$IMAGE_TAGS"
221- echo "- Platforms: \`linux/amd64\`, \`linux/arm64\`"
222- else
223- echo "- Artifact: \`$ARTIFACT_NAME\`"
224- echo "- Retention: 30 days"
225- fi
126+ echo "- Artifact: \`$ARTIFACT_NAME\`"
127+ echo "- Retention: 30 days"
226128 } >> "$GITHUB_STEP_SUMMARY"
0 commit comments