-
Notifications
You must be signed in to change notification settings - Fork 177
138 lines (116 loc) · 4.31 KB
/
Copy pathbuild-docker.yml
File metadata and controls
138 lines (116 loc) · 4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
name: Build Docker image
on:
workflow_dispatch:
permissions:
contents: read
concurrency:
group: docker-${{ github.ref }}
cancel-in-progress: true
env:
IMAGE_NAME: powercontext-server
jobs:
build:
name: Build Linux amd64 and arm64 images
runs-on: ubuntu-24.04
timeout-minutes: 45
steps:
- name: Check out
uses: actions/checkout@v7
with:
fetch-depth: 0
- name: Set up uv
uses: astral-sh/setup-uv@v10.0.0
with:
version: "0.10.12"
enable-cache: true
- name: Set up QEMU
uses: docker/setup-qemu-action@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v4
- name: Resolve image metadata
id: metadata
run: |
python - <<'PY'
import os
import re
import subprocess
from pathlib import Path
package_version = subprocess.check_output(
["uvx", "--from", "hatchling", "--with", "hatch-vcs", "hatchling", "version"],
text=True,
).strip()
source_sha = subprocess.check_output(["git", "rev-parse", "HEAD"], text=True).strip()
safe_version = re.sub(r"[^A-Za-z0-9_.-]+", "-", package_version).strip("-.")
if not safe_version:
raise SystemExit("Project version does not produce a valid Docker tag")
image_tag = f"{safe_version}-{source_sha[:12]}"
artifact_name = f"powercontext-server-{image_tag}-docker-images"
with Path(os.environ["GITHUB_OUTPUT"]).open("a") as output:
output.write(f"package_version={package_version}\n")
output.write(f"source_sha={source_sha}\n")
output.write(f"image_tag={image_tag}\n")
output.write(f"artifact_name={artifact_name}\n")
PY
- name: Build Docker image archives
env:
IMAGE_TAG: ${{ steps.metadata.outputs.image_tag }}
run: |
mkdir -p docker-images
for platform in linux/amd64 linux/arm64; do
platform_suffix="${platform//\//-}"
output_file="docker-images/${IMAGE_NAME}-${IMAGE_TAG}-${platform_suffix}.tar"
docker buildx build \
--file docker/Dockerfile \
--platform "$platform" \
--tag "${IMAGE_NAME}:${IMAGE_TAG}" \
--build-arg "POWERCONTEXT_VERSION=${{ steps.metadata.outputs.package_version }}" \
--cache-from "type=gha,scope=docker-${platform_suffix}" \
--cache-to "type=gha,mode=max,scope=docker-${platform_suffix}" \
--output "type=docker,dest=${output_file}" \
.
done
- name: Smoke test Linux amd64 image
env:
IMAGE_TAG: ${{ steps.metadata.outputs.image_tag }}
run: |
image_archive="docker-images/${IMAGE_NAME}-${IMAGE_TAG}-linux-amd64.tar"
container_name="powercontext-smoke-${GITHUB_RUN_ID}-${GITHUB_RUN_ATTEMPT}"
cleanup() {
docker rm --force "$container_name" >/dev/null 2>&1 || true
}
trap cleanup EXIT
docker load --input "$image_archive"
docker run \
--detach \
--name "$container_name" \
--publish 127.0.0.1::8000 \
"${IMAGE_NAME}:${IMAGE_TAG}"
endpoint="$(docker port "$container_name" 8000/tcp)"
for attempt in $(seq 1 30); do
if curl --fail --silent --show-error "http://${endpoint}/health/ready"; then
exit 0
fi
sleep 1
done
docker logs "$container_name"
exit 1
- name: Upload Docker image archives
uses: actions/upload-artifact@v7
with:
name: ${{ steps.metadata.outputs.artifact_name }}
path: docker-images/*.tar
compression-level: 0
if-no-files-found: error
retention-days: 30
- name: Summarize build
env:
ARTIFACT_NAME: ${{ steps.metadata.outputs.artifact_name }}
SOURCE_SHA: ${{ steps.metadata.outputs.source_sha }}
run: |
{
echo "## Docker image"
echo
echo "- Source SHA: \`$SOURCE_SHA\`"
echo "- Artifact: \`$ARTIFACT_NAME\`"
echo "- Retention: 30 days"
} >> "$GITHUB_STEP_SUMMARY"