|
| 1 | +name: Build and Push to Docker Hub |
| 2 | + |
| 3 | +on: |
| 4 | + release: |
| 5 | + types: [published] |
| 6 | + |
| 7 | +env: |
| 8 | + # Use docker.io for Docker Hub if empty |
| 9 | + REGISTRY: docker.io |
| 10 | + # github.repository as <account>/<repo> |
| 11 | + IMAGE_NAME: ${{ github.repository }} |
| 12 | + DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }} |
| 13 | + COSIGN_EXPERIMENTAL: 1 |
| 14 | + DOCKER_CONTENT_TRUST: 1 |
| 15 | + DOCKER_CONTENT_TRUST_SERVER: https://notary.docker.io |
| 16 | + |
| 17 | +jobs: |
| 18 | + docker: |
| 19 | + runs-on: ubuntu-latest |
| 20 | + strategy: |
| 21 | + matrix: |
| 22 | + include: |
| 23 | + - dockerfile: kubernetes/dockerfiles/spike.Dockerfile |
| 24 | + image: spike/spike |
| 25 | + - dockerfile: kubernetes/dockerfiles/keeper.Dockerfile |
| 26 | + image: spike/keeper |
| 27 | + - dockerfile: kubernetes/dockerfiles/nexus.Dockerfile |
| 28 | + image: spike/nexus |
| 29 | + permissions: |
| 30 | + contents: read |
| 31 | + packages: write |
| 32 | + id-token: write # needed for signing the images with GitHub OIDC Token |
| 33 | + |
| 34 | + steps: |
| 35 | + - name: Checkout repository |
| 36 | + uses: actions/checkout@v4 |
| 37 | + with: |
| 38 | + ref: ${{ github.event.release.tag_name }} |
| 39 | + |
| 40 | + - name: Set up QEMU |
| 41 | + uses: docker/setup-qemu-action@v3 |
| 42 | + |
| 43 | + - name: Set up Docker Buildx |
| 44 | + uses: docker/setup-buildx-action@v3 |
| 45 | + |
| 46 | + - name: Install cosign |
| 47 | + uses: sigstore/cosign-installer@v3.3.0 |
| 48 | + |
| 49 | + # Setup Docker Content Trust keys |
| 50 | + - name: Setup DCT |
| 51 | + if: github.event_name == 'release' |
| 52 | + env: |
| 53 | + DCT_DELEGATION_KEY: ${{ secrets.DCT_DELEGATION_KEY }} |
| 54 | + DCT_ROOT_KEY: ${{ secrets.DCT_ROOT_KEY }} |
| 55 | + run: | |
| 56 | + mkdir -p ~/.docker/trust/private |
| 57 | + echo "$DCT_DELEGATION_KEY" > ~/.docker/trust/private/$(echo -n "${{ env.REGISTRY }}/${{ matrix.image }}" | sha256sum | cut -d' ' -f1).key |
| 58 | + echo "$DCT_ROOT_KEY" > ~/.docker/trust/private/root_keys |
| 59 | +
|
| 60 | + # Login to Docker Hub |
| 61 | + - name: Log in to Docker Hub |
| 62 | + uses: docker/login-action@v3 |
| 63 | + with: |
| 64 | + username: ${{ secrets.DOCKERHUB_USERNAME }} |
| 65 | + password: ${{ secrets.DOCKERHUB_TOKEN }} |
| 66 | + |
| 67 | + # Extract metadata (tags, labels) for Docker |
| 68 | + - name: Extract metadata |
| 69 | + id: meta |
| 70 | + uses: docker/metadata-action@v5 |
| 71 | + with: |
| 72 | + images: ${{ env.REGISTRY }}/${{ matrix.image }} |
| 73 | + tags: | |
| 74 | + type=semver,pattern={{version}},value=${{ github.event.release.tag_name }} |
| 75 | + type=semver,pattern={{major}}.{{minor}},value=${{ github.event.release.tag_name }} |
| 76 | + type=raw,value=latest |
| 77 | + type=sha |
| 78 | + # example tags in order: 1.2.3, 1.2, latest, sha-1234567890(git commit sha) |
| 79 | + |
| 80 | + # Build and push Docker image |
| 81 | + - name: Build and push |
| 82 | + uses: docker/build-push-action@v5 |
| 83 | + id: build-and-push |
| 84 | + with: |
| 85 | + context: . |
| 86 | + file: ${{ matrix.dockerfile }} |
| 87 | + platforms: linux/amd64,linux/arm64 |
| 88 | + push: ${{ github.event_name == 'release' }} |
| 89 | + tags: ${{ steps.meta.outputs.tags }} |
| 90 | + labels: ${{ steps.meta.outputs.labels }} |
| 91 | + cache-from: type=registry,ref=${{ env.REGISTRY }}/${{ matrix.image }}:buildcache |
| 92 | + cache-to: type=registry,ref=${{ env.REGISTRY }}/${{ matrix.image }}:buildcache,mode=max |
| 93 | + provenance: mode=max |
| 94 | + |
| 95 | + - name: Sign the images with GitHub OIDC (Cosign) |
| 96 | + if: github.event_name == 'release' |
| 97 | + env: |
| 98 | + DIGEST: ${{ steps.build-and-push.outputs.digest }} |
| 99 | + TAGS: ${{ steps.meta.outputs.tags }} |
| 100 | + run: | |
| 101 | + echo "${TAGS}" | tr ',' '\n' | while read -r tag; do |
| 102 | + cosign sign --yes "${tag}@${DIGEST}" |
| 103 | + done |
| 104 | +
|
| 105 | + - name: Sign the images with DCT |
| 106 | + if: github.event_name == 'release' |
| 107 | + env: |
| 108 | + DOCKER_CONTENT_TRUST_REPOSITORY_PASSPHRASE: ${{ secrets.DCT_REPOSITORY_PASSPHRASE }} |
| 109 | + TAGS: ${{ steps.meta.outputs.tags }} |
| 110 | + run: | |
| 111 | + echo "${TAGS}" | tr ',' '\n' | while read -r tag; do |
| 112 | + docker trust sign "$tag" |
| 113 | + done |
0 commit comments