|
| 1 | +name: build-and-publish-docker |
| 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 | + push: |
| 10 | + branches: ["master", "sho/docker"] |
| 11 | + |
| 12 | + pull_request: |
| 13 | + branches: ["master"] |
| 14 | + |
| 15 | +env: |
| 16 | + # Use docker.io for Docker Hub if empty |
| 17 | + REGISTRY: ghcr.io |
| 18 | + # github.repository as <account>/<repo> |
| 19 | + IMAGE_NAME: ${{ github.repository }} |
| 20 | + |
| 21 | +jobs: |
| 22 | + |
| 23 | + build: |
| 24 | + runs-on: ubuntu-latest |
| 25 | + permissions: |
| 26 | + contents: read |
| 27 | + packages: write |
| 28 | + # This is used to complete the identity challenge |
| 29 | + # with sigstore/fulcio when running outside of PRs. |
| 30 | + id-token: write |
| 31 | + |
| 32 | + steps: |
| 33 | + - name: Checkout repository |
| 34 | + uses: actions/checkout@v4 |
| 35 | + |
| 36 | + # Workaround: https://github.com/docker/build-push-action/issues/461 |
| 37 | + - name: Setup Docker buildx |
| 38 | + uses: docker/setup-buildx-action@v3 |
| 39 | + |
| 40 | + # Login against a Docker registry except on PR |
| 41 | + # https://github.com/docker/login-action |
| 42 | + - name: Log into registry ${{ env.REGISTRY }} |
| 43 | + if: github.event_name != 'pull_request' |
| 44 | + uses: docker/login-action@v3 |
| 45 | + with: |
| 46 | + registry: ${{ env.REGISTRY }} |
| 47 | + username: ${{ github.actor }} |
| 48 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 49 | + |
| 50 | + # Extract metadata (tags, labels) for Docker |
| 51 | + # https://github.com/docker/metadata-action |
| 52 | + - name: Extract Docker metadata |
| 53 | + id: meta |
| 54 | + uses: docker/metadata-action@v5 |
| 55 | + with: |
| 56 | + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 57 | + |
| 58 | + # Build and push Docker image with Buildx (don't push on PR) |
| 59 | + # https://github.com/docker/build-push-action |
| 60 | + - name: Build and push Docker image |
| 61 | + id: build-and-push |
| 62 | + uses: docker/build-push-action@v5 |
| 63 | + with: |
| 64 | + push: ${{ github.event_name != 'pull_request' }} |
| 65 | + tags: ${{ steps.meta.outputs.tags }} |
| 66 | + labels: ${{ steps.meta.outputs.labels }} |
| 67 | + cache-from: type=gha |
| 68 | + cache-to: type=gha,mode=max |
| 69 | + |
| 70 | + - name: Clean container registry |
| 71 | + uses: actions/delete-package-versions@v5 |
| 72 | + with: |
| 73 | + package-name: "elk-live" |
| 74 | + package-type: "container" |
| 75 | + min-versions-to-keep: 3 |
| 76 | + token: ${{ secrets.GITHUB_TOKEN }} |
0 commit comments