|
| 1 | +name: image-build-and-publish |
| 2 | + |
| 3 | +# This workflow uses actions that are not certified by GitHub. |
| 4 | +# They are provided by a third-party and are governed by |
| 5 | +# separate terms of service, privacy policy, and support |
| 6 | +# documentation. |
| 7 | + |
| 8 | +on: |
| 9 | + workflow_dispatch: |
| 10 | + |
| 11 | +env: |
| 12 | + # Use docker.io for Docker Hub if empty |
| 13 | + REGISTRY: ghcr.io |
| 14 | + # github.repository as <account>/<repo> |
| 15 | + IMAGE_NAME: ${{ github.repository }} |
| 16 | + |
| 17 | + |
| 18 | +jobs: |
| 19 | + build: |
| 20 | + |
| 21 | + runs-on: ubuntu-latest |
| 22 | + permissions: |
| 23 | + contents: read |
| 24 | + packages: write |
| 25 | + # This is used to complete the identity challenge |
| 26 | + # with sigstore/fulcio when running outside of PRs. |
| 27 | + id-token: write |
| 28 | + |
| 29 | + steps: |
| 30 | + - name: downcase REPO |
| 31 | + run: | |
| 32 | + echo "REPO=${GITHUB_REPOSITORY,,}" >>${GITHUB_ENV} |
| 33 | + - name: Checkout repository |
| 34 | + uses: actions/checkout@v4 |
| 35 | + |
| 36 | + # Install the cosign tool except on PR |
| 37 | + # https://github.com/sigstore/cosign-installer |
| 38 | + #- name: Install cosign |
| 39 | + # if: github.event_name != 'pull_request' |
| 40 | + # uses: sigstore/cosign-installer@f3c664df7af409cb4873aa5068053ba9d61a57b6 #v2.6.0 |
| 41 | + # with: |
| 42 | + # cosign-release: 'v1.13.1' |
| 43 | + |
| 44 | + |
| 45 | + # Workaround: https://github.com/docker/build-push-action/issues/461 |
| 46 | + - name: Setup Docker buildx |
| 47 | + uses: docker/setup-buildx-action@v3 |
| 48 | + |
| 49 | + # Login against a Docker registry except on PR |
| 50 | + # https://github.com/docker/login-action |
| 51 | + - name: Log into registry ${{ env.REGISTRY }} |
| 52 | + if: github.event_name != 'pull_request' |
| 53 | + uses: docker/login-action@v3 |
| 54 | + with: |
| 55 | + registry: ${{ env.REGISTRY }} |
| 56 | + username: ${{ github.actor }} |
| 57 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 58 | + |
| 59 | + # Extract metadata (tags, labels) for Docker |
| 60 | + # https://github.com/docker/metadata-action |
| 61 | + - name: Extract Docker metadata |
| 62 | + id: meta |
| 63 | + uses: docker/metadata-action@v5 |
| 64 | + with: |
| 65 | + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 66 | + |
| 67 | + - name: Create tags (version and latest) |
| 68 | + id: tag-master |
| 69 | + run: | |
| 70 | + VERSION=$(cat VERSION) |
| 71 | + echo "VERSION=$VERSION" >> $GITHUB_ENV |
| 72 | + TAGS=$(echo "ghcr.io/$REPO:$VERSION" , "ghcr.io/$REPO:latest") |
| 73 | + echo "TAGS=$TAGS" >> $GITHUB_ENV |
| 74 | + echo "Version: $VERSION " |
| 75 | + echo "Tags: $TAGS" |
| 76 | + |
| 77 | + - name: Show versions and tags |
| 78 | + run: | |
| 79 | + echo "Version: ${{ env.VERSION }}" |
| 80 | + echo "Tags: ${{ env.TAGS }}" |
| 81 | + |
| 82 | + # Build and push Docker image with Buildx (don't push on PR) |
| 83 | + # https://github.com/docker/build-push-action |
| 84 | + - name: Build and push Docker image |
| 85 | + id: build-and-push |
| 86 | + uses: docker/build-push-action@v5 |
| 87 | + with: |
| 88 | + context: . |
| 89 | + push: ${{ github.event_name != 'pull_request' }} |
| 90 | + tags: ${{ env.TAGS }} |
| 91 | + platforms: linux/amd64,linux/arm64 |
| 92 | + labels: ${{ steps.meta.outputs.labels }} |
| 93 | + cache-from: type=gha |
| 94 | + cache-to: type=gha,mode=max |
| 95 | + |
| 96 | + # Sign the resulting Docker image digest except on PRs. |
| 97 | + # This will only write to the public Rekor transparency log when the Docker |
| 98 | + # repository is public to avoid leaking data. If you would like to publish |
| 99 | + # transparency data even for private images, pass --force to cosign below. |
| 100 | + # https://github.com/sigstore/cosign |
| 101 | + #- name: Sign the published Docker image |
| 102 | + # if: ${{ github.event_name != 'pull_request' }} |
| 103 | + # env: |
| 104 | + # COSIGN_EXPERIMENTAL: "true" |
| 105 | + # # This step uses the identity token to provision an ephemeral certificate |
| 106 | + # # against the sigstore community Fulcio instance. |
| 107 | + # run: echo "${{ steps.meta.outputs.tags }}" | xargs -I {} cosign sign {}@${{ steps.build-and-push.outputs.digest }} |
0 commit comments