|
1 | | -name: Docker |
| 1 | +name: Build and publish a Docker image |
2 | 2 |
|
3 | | -on: [push] |
| 3 | +on: [push, pull_request, workflow_dispatch] |
4 | 4 |
|
5 | 5 | env: |
6 | | - IMAGE_NAME: chiya |
7 | | - REPOSITORY: ghcr.io |
| 6 | + REGISTRY: ghcr.io |
| 7 | + IMAGE_NAME: ${{ github.repository }} |
| 8 | + FORCE_COLOR: 1 |
| 9 | + # This environment variable determines whether to push the Docker image to the registry. |
| 10 | + # It evaluates to 'true' if the workflow is NOT triggered by a pull request, |
| 11 | + # or if the pull request originates from the 'Snaacky/chiya' repository. |
| 12 | + # This prevents pushing images from forks or untrusted sources. |
| 13 | + PUSH_TO_REGISTRY: ${{ github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == 'Snaacky/chiya' }} |
8 | 14 |
|
9 | 15 | jobs: |
10 | | - push: |
| 16 | + build-and-push-image: |
11 | 17 | runs-on: ubuntu-latest |
| 18 | + permissions: |
| 19 | + contents: read |
| 20 | + packages: write |
| 21 | + id-token: write |
| 22 | + attestations: write |
12 | 23 | steps: |
13 | | - - name: Checkout repository |
14 | | - uses: actions/checkout@v3 |
15 | | - |
16 | | - - name: Prepare metadata for build |
17 | | - id: prep |
18 | | - run: | |
19 | | - VERSION=edge |
20 | | - if [[ $GITHUB_REF == refs/tags/* ]]; then |
21 | | - VERSION=${GITHUB_REF#refs/tags/} |
22 | | - elif [[ $GITHUB_REF == refs/heads/* ]]; then |
23 | | - VERSION=$(echo ${GITHUB_REF#refs/heads/} | sed -r 's#/+#-#g') |
24 | | - elif [[ $GITHUB_REF == refs/pull/* ]]; then |
25 | | - VERSION=pr-${{ github.event.number }} |
26 | | - fi |
27 | | -
|
28 | | - # Use Docker `latest` tag convention |
29 | | - [ "$VERSION" == "master" ] && VERSION=latest |
30 | | -
|
31 | | - echo ::set-output name=version::${VERSION} |
32 | | -
|
33 | | - - name: Build and publish Docker image (using cache) |
34 | | - uses: whoan/docker-build-with-cache-action@v5 |
| 24 | + - name: Checkout |
| 25 | + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 |
35 | 26 | with: |
36 | | - registry: ${{ env.REPOSITORY }} |
37 | | - username: ${{ github.repository_owner }} |
| 27 | + persist-credentials: false |
| 28 | + |
| 29 | + - name: Log in to the Container registry |
| 30 | + if: ${{ env.PUSH_TO_REGISTRY == 'true' }} |
| 31 | + uses: docker/login-action@74a5d142397b4f367a81961eba4e8cd7edddf772 # v3.4.0 |
| 32 | + with: |
| 33 | + registry: ${{ env.REGISTRY }} |
| 34 | + username: ${{ github.actor }} |
38 | 35 | password: ${{ secrets.GITHUB_TOKEN }} |
39 | | - image_name: ${{ env.IMAGE_NAME }} |
40 | | - image_tag: ${{ steps.prep.outputs.version }} |
| 36 | + |
| 37 | + - name: Extract metadata (tags, labels) for Docker |
| 38 | + id: meta |
| 39 | + uses: docker/metadata-action@902fa8ec7d6ecbf8d84d538b9b233a880e428804 # v5.7.0 |
| 40 | + with: |
| 41 | + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 42 | + tags: | |
| 43 | + type=ref,event=branch |
| 44 | + type=ref,event=tag |
| 45 | + type=ref,event=pr |
| 46 | + type=sha |
| 47 | + type=raw,value=latest,enable={{is_default_branch}} |
| 48 | +
|
| 49 | + - name: Set up Docker Buildx |
| 50 | + uses: docker/setup-buildx-action@e468171a9de216ec08956ac3ada2f0791b6bd435 # v3.11.1 |
| 51 | + |
| 52 | + - name: Build and push Docker image |
| 53 | + id: push |
| 54 | + uses: docker/build-push-action@263435318d21b8e681c14492fe198d362a7d2c83 # v6.18.0 |
| 55 | + with: |
| 56 | + context: . |
| 57 | + push: ${{ env.PUSH_TO_REGISTRY == 'true' }} |
| 58 | + tags: ${{ steps.meta.outputs.tags }} |
| 59 | + labels: ${{ steps.meta.outputs.labels }} |
| 60 | + cache-from: type=gha |
| 61 | + cache-to: type=gha,mode=max |
| 62 | + |
| 63 | + - name: Generate artifact attestation |
| 64 | + if: ${{ env.PUSH_TO_REGISTRY == 'true' }} |
| 65 | + uses: actions/attest-build-provenance@e8998f949152b193b063cb0ec769d69d929409be # v2.4.0 |
| 66 | + with: |
| 67 | + subject-name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME}} |
| 68 | + subject-digest: ${{ steps.push.outputs.digest }} |
| 69 | + push-to-registry: true |
0 commit comments