Build and Push Docker Image #1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Build and Push Docker Image | |
| on: | |
| push: | |
| branches: [ "main" ] | |
| # Publish semver tags as releases. | |
| tags: [ 'v*.*.*' ] | |
| pull_request: | |
| branches: [ "main" ] | |
| workflow_dispatch: | |
| env: | |
| # Use docker.io for Docker Hub if needed | |
| REGISTRY: ghcr.io | |
| # github.repository as <account>/<repo> | |
| IMAGE_NAME: ${{ github.repository }} | |
| jobs: | |
| build-and-push: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: read | |
| packages: write | |
| # Optional: write permission to deploy metadata to GHCR. | |
| # id-token: write | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| - name: Set up Go | |
| uses: actions/setup-go@v5 | |
| with: | |
| go-version: '1.21' # Specify your Go version | |
| - name: Build Go application (optional, if not done in Dockerfile) | |
| run: go build -v ./... | |
| - name: Run Go tests (optional) | |
| run: go test -v ./... | |
| # Install the cosign tool except on PR | |
| # https://github.com/sigstore/cosign-installer | |
| # - name: Install Cosign | |
| # if: github.event_name != 'pull_request' | |
| # uses: sigstore/cosign-installer@v3.5.0 | |
| # with: | |
| # cosign-release: 'v2.2.4' # optional | |
| # Set up BuildKit Docker builder to be able to build | |
| # multi-platform images and export cache | |
| # https://github.com/docker/setup-buildx-action | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| # Login against a Docker registry except on PR | |
| # https://github.com/docker/login-action | |
| - name: Log into registry ${{ env.REGISTRY }} | |
| if: github.event_name != 'pull_request' | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ${{ env.REGISTRY }} | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| # Extract metadata (tags, labels) for Docker | |
| # https://github.com/docker/metadata-action | |
| - name: Extract Docker metadata | |
| id: meta | |
| uses: docker/metadata-action@v5 | |
| with: | |
| images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} | |
| tags: | | |
| # set latest tag for default branch | |
| type=raw,value=latest,enable={{is_default_branch}} | |
| # tag event 'v1.2.3' becomes '1.2.3' | |
| type=semver,pattern={{version}} | |
| # tag event 'v1.2.3' becomes '1.2' | |
| type=semver,pattern={{major}}.{{minor}} | |
| # tag event 'v1.2.3' becomes '1' | |
| type=semver,pattern={{major}} | |
| # sha-<short SHA> | |
| type=sha,prefix=sha- | |
| # Build and push Docker image with Buildx (don't push on PR) | |
| # https://github.com/docker/build-push-action | |
| - name: Build and push Docker image | |
| id: build-push | |
| uses: docker/build-push-action@v6 | |
| with: | |
| context: . | |
| push: ${{ github.event_name != 'pull_request' }} | |
| tags: ${{ steps.meta.outputs.tags }} | |
| labels: ${{ steps.meta.outputs.labels }} | |
| cache-from: type=gha | |
| cache-to: type=gha,mode=max | |
| # Sign the resulting Docker image digest except on PRs. | |
| # This will only write to the workflow logs if the Docker repository is not | |
| # protected by signed interactions. | |
| # https://github.com/sigstore/cosign | |
| # https://docs.github.com/en/actions/security-guides/security-hardening-for-github-actions#using-an-intermediate-repository | |
| # - name: Sign the published Docker image | |
| # if: ${{ github.event_name != 'pull_request' }} | |
| # env: | |
| # # https://docs.github.com/en/actions/security-guides/security-hardening-for-github-actions#using-an-intermediate-repository | |
| # COSIGN_EXPERIMENTAL: "true" | |
| # # This step uses the identity token to provision an ephemeral certificate | |
| # # against the sigstore community Fulcio instance. | |
| # run: echo "${{ steps.meta.outputs.tags }}" | xargs -I {} cosign sign --yes {}@${{ steps.build-push.outputs.digest }} |