|
| 1 | +name: CI (Docker) |
| 2 | +on: |
| 3 | + workflow_dispatch: |
| 4 | + pull_request: |
| 5 | + types: [opened, synchronize, reopened, labeled, unlabeled] |
| 6 | + push: |
| 7 | + # Ref: GHA Filter pattern syntax: https://docs.github.com/en/actions/reference/workflows-and-actions/workflow-syntax#filter-pattern-cheat-sheet |
| 8 | + # Run on pushes to main, release branches, and previous/future major version branches |
| 9 | + branches: |
| 10 | + - main |
| 11 | + - 'v[0-9]+.*' # Matches any release branch, e.g. v6.0.3, v12.1.0 |
| 12 | + - '[0-9]+.x' # Matches any major version branch, e.g. 5.x, 23.x |
| 13 | + |
| 14 | +jobs: |
| 15 | + build: |
| 16 | + name: Build & Push |
| 17 | + runs-on: ubuntu-latest-16-cores |
| 18 | + permissions: |
| 19 | + contents: read |
| 20 | + packages: write |
| 21 | + steps: |
| 22 | + - name: Checkout repository |
| 23 | + uses: actions/checkout@v4 |
| 24 | + with: |
| 25 | + submodules: true |
| 26 | + |
| 27 | + - name: Set up Docker Buildx |
| 28 | + uses: docker/setup-buildx-action@v3 |
| 29 | + |
| 30 | + - name: Log in to GitHub Container Registry |
| 31 | + uses: docker/login-action@v3 |
| 32 | + with: |
| 33 | + registry: ghcr.io |
| 34 | + username: ${{ github.actor }} |
| 35 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 36 | + |
| 37 | + # We can't access secrets from forks, so we can't push to the registry |
| 38 | + # Instead, we upload the image as an artifact, and download it in downstream jobs |
| 39 | + - name: Determine build strategy |
| 40 | + id: strategy |
| 41 | + run: | |
| 42 | + IS_FORK_PR="false" |
| 43 | + if [ "${{ github.event_name }}" = "pull_request" ] && [ "${{ github.event.pull_request.head.repo.full_name }}" != "${{ github.repository }}" ]; then |
| 44 | + IS_FORK_PR="true" |
| 45 | + fi |
| 46 | + IMAGE_NAME="ghcr.io/${{ github.repository_owner }}/ghost-development" |
| 47 | + if [ "$IS_FORK_PR" = "true" ]; then |
| 48 | + IMAGE_NAME="ghcr.io/${{ github.event.pull_request.head.repo.owner.login }}/ghost-development" |
| 49 | + fi |
| 50 | + CACHE_KEY=$(echo $IMAGE_NAME | tr '[:upper:]' '[:lower:]') |
| 51 | + echo "is-fork-pr=$IS_FORK_PR" >> $GITHUB_OUTPUT |
| 52 | + echo "should-push=$( [ "$IS_FORK_PR" = "false" ] && echo "true" || echo "false" )" >> $GITHUB_OUTPUT |
| 53 | + echo "should-load=$( [ "$IS_FORK_PR" = "true" ] && echo "true" || echo "false" )" >> $GITHUB_OUTPUT |
| 54 | + echo "image-name=$IMAGE_NAME" >> $GITHUB_OUTPUT |
| 55 | + echo "cache-key=$CACHE_KEY" >> $GITHUB_OUTPUT |
| 56 | + echo "Build Strategy: " |
| 57 | + echo " Is fork PR: $IS_FORK_PR" |
| 58 | + echo " Should push: $( [ "$IS_FORK_PR" = "false" ] && echo "true" || echo "false" )" |
| 59 | + echo " Should load: $( [ "$IS_FORK_PR" = "true" ] && echo "true" || echo "false" )" |
| 60 | + echo " Image name: $IMAGE_NAME" |
| 61 | + echo " Cache key: $CACHE_KEY" |
| 62 | +
|
| 63 | + - name: Docker meta |
| 64 | + id: meta |
| 65 | + uses: docker/metadata-action@v5 |
| 66 | + with: |
| 67 | + images: | |
| 68 | + ${{ steps.strategy.outputs.image-name }} |
| 69 | + tags: | |
| 70 | + type=ref,event=branch |
| 71 | + type=ref,event=pr |
| 72 | + type=sha |
| 73 | + type=raw,value=latest,enable={{is_default_branch}} |
| 74 | + labels: | |
| 75 | + org.opencontainers.image.title=Ghost Development |
| 76 | + org.opencontainers.image.description=Ghost development build |
| 77 | + org.opencontainers.image.vendor=TryGhost |
| 78 | + maintainer=TryGhost |
| 79 | +
|
| 80 | + - name: Build and push Docker image |
| 81 | + uses: docker/build-push-action@v6 |
| 82 | + id: build |
| 83 | + with: |
| 84 | + context: . |
| 85 | + file: .docker/Dockerfile |
| 86 | + push: ${{ steps.strategy.outputs.should-push }} |
| 87 | + load: ${{ steps.strategy.outputs.should-load }} |
| 88 | + tags: ${{ steps.meta.outputs.tags }} |
| 89 | + labels: ${{ steps.meta.outputs.labels }} |
| 90 | + # On PRs: use both main cache and PR-specific cache |
| 91 | + # On main: only use main cache |
| 92 | + cache-from: | |
| 93 | + type=registry,ref=${{ steps.strategy.outputs.cache-key }}:cache-main |
| 94 | + ${{ github.event_name == 'pull_request' && format('type=registry,ref={0}:cache-pr-{1}', steps.strategy.outputs.cache-key, github.event.pull_request.number) || '' }} |
| 95 | + # Only export cache if we can push (not on fork PRs) |
| 96 | + cache-to: ${{ steps.strategy.outputs.should-push == 'true' && format('type=registry,ref={0}:cache-{1},mode=max', steps.strategy.outputs.cache-key, github.event_name == 'pull_request' && format('pr-{0}', github.event.pull_request.number) || 'main') || '' }} |
| 97 | + |
| 98 | + - name: Save image as artifact (fork PR) |
| 99 | + if: steps.strategy.outputs.is-fork-pr == 'true' |
| 100 | + run: | |
| 101 | + # Get the first tag from the multi-line tags output |
| 102 | + IMAGE_TAG=$(echo "${{ steps.meta.outputs.tags }}" | head -n1) |
| 103 | + echo "Saving image: $IMAGE_TAG" |
| 104 | + docker save "$IMAGE_TAG" | gzip > docker-image.tar.gz |
| 105 | + echo "Image saved as docker-image.tar.gz" |
| 106 | + ls -lh docker-image.tar.gz |
| 107 | +
|
| 108 | + - name: Upload image artifact (fork PR) |
| 109 | + if: steps.strategy.outputs.is-fork-pr == 'true' |
| 110 | + uses: actions/upload-artifact@v4 |
| 111 | + with: |
| 112 | + name: docker-image |
| 113 | + path: docker-image.tar.gz |
| 114 | + retention-days: 1 |
| 115 | + |
| 116 | + outputs: |
| 117 | + image-tags: ${{ steps.meta.outputs.tags }} |
| 118 | + image-digest: ${{ steps.build.outputs.digest }} |
| 119 | + is-fork: ${{ steps.strategy.outputs.is-fork-pr }} |
| 120 | + |
| 121 | + inspect_image: |
| 122 | + name: Inspect Docker Image |
| 123 | + needs: build |
| 124 | + runs-on: ubuntu-latest |
| 125 | + |
| 126 | + steps: |
| 127 | + - name: Checkout repository |
| 128 | + uses: actions/checkout@v4 |
| 129 | + with: |
| 130 | + sparse-checkout: | |
| 131 | + .github/actions/load-docker-image |
| 132 | +
|
| 133 | + - name: Load Docker Image |
| 134 | + id: load |
| 135 | + uses: ./.github/actions/load-docker-image |
| 136 | + with: |
| 137 | + is-fork: ${{ needs.build.outputs.is-fork }} |
| 138 | + image-tags: ${{ needs.build.outputs.image-tags }} |
| 139 | + |
| 140 | + - name: Inspect image size and layers |
| 141 | + shell: bash |
| 142 | + run: | |
| 143 | + IMAGE_TAG="${{ steps.load.outputs.image-tag }}" |
| 144 | + echo "Analyzing Docker image: $IMAGE_TAG" |
| 145 | +
|
| 146 | + # Get the image size in bytes |
| 147 | + IMAGE_SIZE_BYTES=$(docker inspect "$IMAGE_TAG" --format='{{.Size}}') |
| 148 | +
|
| 149 | + # Convert to human readable format |
| 150 | + IMAGE_SIZE_MB=$(( IMAGE_SIZE_BYTES / 1024 / 1024 )) |
| 151 | + IMAGE_SIZE_GB=$(echo "scale=2; $IMAGE_SIZE_BYTES / 1024 / 1024 / 1024" | bc) |
| 152 | +
|
| 153 | + # Format size display based on magnitude |
| 154 | + if [ $IMAGE_SIZE_MB -ge 1024 ]; then |
| 155 | + IMAGE_SIZE_DISPLAY="${IMAGE_SIZE_GB} GB" |
| 156 | + else |
| 157 | + IMAGE_SIZE_DISPLAY="${IMAGE_SIZE_MB} MB" |
| 158 | + fi |
| 159 | +
|
| 160 | + echo "Image size: ${IMAGE_SIZE_DISPLAY}" |
| 161 | +
|
| 162 | + # Write to GitHub Step Summary |
| 163 | + { |
| 164 | + echo "# Docker Image Analysis" |
| 165 | + echo "" |
| 166 | + echo "**Image:** \`$IMAGE_TAG\`" |
| 167 | + echo "" |
| 168 | + echo "**Total Size:** ${IMAGE_SIZE_DISPLAY}" |
| 169 | + echo "" |
| 170 | + echo "## Image Layers" |
| 171 | + echo "" |
| 172 | + echo "| Size | Layer |" |
| 173 | + echo "|------|-------|" |
| 174 | +
|
| 175 | + # Get all layers (including 0B ones) |
| 176 | + docker history "$IMAGE_TAG" --format "{{.Size}}@@@{{.CreatedBy}}" --no-trunc | \ |
| 177 | + while IFS='@@@' read -r size cmd; do |
| 178 | + # Clean up the command for display |
| 179 | + cmd_clean=$(echo "$cmd" | sed 's/^\/bin\/sh -c //' | sed 's/^#(nop) //' | sed 's/^@@//' | sed 's/|/\\|/g' | cut -c1-80) |
| 180 | + if [ ${#cmd} -gt 80 ]; then |
| 181 | + cmd_clean="${cmd_clean}..." |
| 182 | + fi |
| 183 | + echo "| $size | \`${cmd_clean}\` |" |
| 184 | + done |
| 185 | +
|
| 186 | + } >> $GITHUB_STEP_SUMMARY |
0 commit comments