Skip to content

Commit 680ec10

Browse files
committed
Add GitHub workflow for blog image
1 parent cdecf5c commit 680ec10

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
name: Build and Publish Blog Image
2+
3+
concurrency:
4+
group: blog-build-${{ github.ref }}
5+
cancel-in-progress: true
6+
7+
on:
8+
push:
9+
branches: [master]
10+
tags: ["v*"]
11+
pull_request:
12+
workflow_dispatch:
13+
14+
env:
15+
REGISTRY: ghcr.io
16+
IMAGE_NAME: "www-blog"
17+
18+
jobs:
19+
build:
20+
runs-on: ubuntu-latest
21+
permissions:
22+
contents: read
23+
packages: write
24+
25+
steps:
26+
- name: Checkout
27+
uses: actions/checkout@v4
28+
29+
- name: Compute image name (lowercase owner/repo)
30+
id: img
31+
run: |
32+
OWNER="$(echo "${GITHUB_REPOSITORY_OWNER}" | tr '[:upper:]' '[:lower:]')"
33+
REPO_NAME="${GITHUB_REPOSITORY##*/}"
34+
IMAGE_NAME="${IMAGE_NAME:-$REPO_NAME}"
35+
IMAGE_NAME="$(echo "${IMAGE_NAME}" | tr '[:upper:]' '[:lower:]')"
36+
echo "IMAGE=${REGISTRY}/${OWNER}/${IMAGE_NAME}" >> "$GITHUB_ENV"
37+
38+
- name: Docker metadata
39+
id: meta
40+
uses: docker/metadata-action@v5
41+
with:
42+
images: ${{ env.IMAGE }}
43+
tags: |
44+
type=sha
45+
type=ref,event=tag
46+
type=ref,event=branch,enable=${{ github.ref == 'refs/heads/master' }}
47+
type=raw,value=latest,enable=${{ github.ref == 'refs/heads/master' }}
48+
49+
- name: Set up Buildx
50+
uses: docker/setup-buildx-action@v3
51+
52+
- name: Build image for tests
53+
uses: docker/build-push-action@v6
54+
with:
55+
context: .
56+
file: ./Dockerfile
57+
push: false
58+
load: true
59+
tags: ${{ env.IMAGE }}:ci
60+
cache-from: type=gha
61+
cache-to: type=gha,mode=max
62+
63+
- name: Run smoke test (curl /)
64+
env:
65+
IMAGE_UNDER_TEST: ${{ env.IMAGE }}:ci
66+
run: |
67+
set -euo pipefail
68+
cid=$(docker run -d -p 0:3000 "$IMAGE_UNDER_TEST")
69+
trap "docker rm -f $cid >/dev/null 2>&1" EXIT
70+
port=$(docker inspect -f '{{ (index (index .NetworkSettings.Ports "3000/tcp") 0).HostPort }}' "$cid")
71+
if [ -z "$port" ]; then
72+
echo "Failed to resolve mapped port for container $cid" >&2
73+
docker logs "$cid" || true
74+
exit 1
75+
fi
76+
for i in {1..20}; do
77+
if curl -fsS "http://127.0.0.1:${port}/" > /dev/null; then
78+
exit 0
79+
fi
80+
sleep 1
81+
done
82+
echo "Service did not respond on / after 20s" >&2
83+
docker logs "$cid" || true
84+
exit 1
85+
86+
- name: Log in to GHCR
87+
if: github.event_name == 'push' && (startsWith(github.ref, 'refs/heads/master') || startsWith(github.ref, 'refs/tags/'))
88+
uses: docker/login-action@v3
89+
with:
90+
registry: ${{ env.REGISTRY }}
91+
username: ${{ github.actor }}
92+
password: ${{ secrets.CR_PAT != '' && secrets.CR_PAT || secrets.GITHUB_TOKEN }}
93+
94+
- name: Build and push
95+
if: github.event_name == 'push' && (startsWith(github.ref, 'refs/heads/master') || startsWith(github.ref, 'refs/tags/'))
96+
uses: docker/build-push-action@v6
97+
with:
98+
context: .
99+
file: ./Dockerfile
100+
push: true
101+
tags: ${{ steps.meta.outputs.tags }}
102+
labels: ${{ steps.meta.outputs.labels }}
103+
cache-from: type=gha
104+
cache-to: type=gha,mode=max

0 commit comments

Comments
 (0)