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