|
9 | 9 | jobs: |
10 | 10 | build: |
11 | 11 | runs-on: ubuntu-latest |
| 12 | + permissions: |
| 13 | + contents: read |
| 14 | + packages: write |
12 | 15 |
|
13 | 16 | steps: |
14 | 17 | - name: Checkout |
15 | 18 | uses: actions/checkout@v4 |
16 | 19 |
|
17 | 20 | - name: Set up Docker Buildx |
18 | 21 | uses: docker/setup-buildx-action@v3 |
| 22 | + |
| 23 | + - name: Login to GHCR |
| 24 | + uses: docker/login-action@v3 |
19 | 25 | with: |
20 | | - driver: docker |
21 | | - # Use the plain 'docker' driver (instead of the default 'docker-container' driver |
22 | | - # that is selected when 'platforms:' is specified). The docker driver makes local |
23 | | - # image tags (canis-base:pr) from the previous step immediately visible to |
24 | | - # subsequent FROM instructions in the same job. The container driver keeps |
25 | | - # the image only in its internal cache, causing the next build to try to |
26 | | - # pull "canis-base:pr" from Docker Hub (library/canis-base:pr) and fail with |
27 | | - # "pull access denied". |
28 | | - |
29 | | - # Build slim base (verification only — no push) |
30 | | - - name: Build slim base |
| 26 | + registry: ghcr.io |
| 27 | + username: ${{ github.actor }} |
| 28 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 29 | + |
| 30 | + - name: Compute temporary tag (for PR verification only) |
| 31 | + id: temp |
| 32 | + run: | |
| 33 | + if [ "${{ github.event_name }}" = "pull_request" ]; then |
| 34 | + TEMP_TAG="pr-${{ github.event.pull_request.number }}-${{ github.sha }}" |
| 35 | + else |
| 36 | + TEMP_TAG="main-${{ github.sha }}" |
| 37 | + fi |
| 38 | + echo "temp_tag=$TEMP_TAG" >> $GITHUB_OUTPUT |
| 39 | +
|
| 40 | + # Build and push the slim base with a temporary tag. |
| 41 | + # This makes the base image available in GHCR so the tools step can pull it |
| 42 | + # via the build-arg (avoids local driver / local-tag visibility problems |
| 43 | + # and the cache-export limitation of the docker driver). |
| 44 | + - name: Build and push slim base (temporary tag) |
31 | 45 | uses: docker/build-push-action@v6 |
32 | 46 | with: |
33 | 47 | context: . |
34 | 48 | file: Dockerfile |
35 | | - push: false |
36 | | - load: true |
37 | | - tags: canis-base:pr |
| 49 | + platforms: linux/amd64 |
| 50 | + push: true |
| 51 | + tags: | |
| 52 | + ghcr.io/datacite/canis-base:${{ steps.temp.outputs.temp_tag }} |
| 53 | + ghcr.io/datacite/canis-base:${{ github.sha }} |
38 | 54 | cache-from: type=gha |
39 | 55 | cache-to: type=gha,mode=max |
40 | 56 |
|
41 | | - # Build tools variant on top of the local base tag (verification only — no push) |
| 57 | + # Build the tools variant. It will resolve BASE_IMAGE from GHCR using the temp tag. |
| 58 | + # We do not push the tools image here (verification only). |
42 | 59 | - name: Build tools variant |
43 | 60 | uses: docker/build-push-action@v6 |
44 | 61 | with: |
45 | 62 | context: . |
46 | 63 | file: Dockerfile.tools |
| 64 | + platforms: linux/amd64 |
47 | 65 | push: false |
48 | 66 | build-args: | |
49 | | - BASE_IMAGE=canis-base:pr |
| 67 | + BASE_IMAGE=ghcr.io/datacite/canis-base:${{ steps.temp.outputs.temp_tag }} |
50 | 68 | cache-from: type=gha |
51 | 69 | cache-to: type=gha,mode=max |
| 70 | + |
| 71 | + # Always attempt to delete the temporary base image after the build/checks |
| 72 | + # (whether the job passed or failed). |
| 73 | + - name: Cleanup temporary base image |
| 74 | + if: always() |
| 75 | + run: | |
| 76 | + TEMP_TAG="${{ steps.temp.outputs.temp_tag }}" |
| 77 | + echo "Attempting to delete temporary tag: $TEMP_TAG" |
| 78 | + # Find the version ID for this exact tag |
| 79 | + VERSION_ID=$(gh api \ |
| 80 | + "/users/${{ github.repository_owner }}/packages/container/canis-base/versions" \ |
| 81 | + --jq ".[] | select(.metadata.container.tags | index(\"$TEMP_TAG\")) | .id" 2>/dev/null || echo "") |
| 82 | + if [ -n "$VERSION_ID" ]; then |
| 83 | + echo "Deleting version ID $VERSION_ID" |
| 84 | + gh api -X DELETE \ |
| 85 | + "/users/${{ github.repository_owner }}/packages/container/canis-base/versions/$VERSION_ID" \ |
| 86 | + || echo "Warning: delete may have failed or insufficient permissions (manual cleanup may be needed in the GitHub UI)" |
| 87 | + else |
| 88 | + echo "No matching version found for tag $TEMP_TAG" |
| 89 | + fi |
0 commit comments