|
7 | 7 |
|
8 | 8 | jobs: |
9 | 9 | build-and-push-docker-image: |
| 10 | + permissions: |
| 11 | + contents: read |
| 12 | + packages: write |
| 13 | + id-token: write |
| 14 | + attestations: write |
| 15 | + |
10 | 16 | name: Build Docker image and push to repositories with version tag |
11 | 17 | runs-on: ubuntu-latest |
12 | 18 | steps: |
13 | 19 | - name: Checkout code |
14 | | - uses: actions/checkout@v2 |
15 | | - |
16 | | - - name: Set up QEMU |
17 | | - uses: docker/setup-qemu-action@v1 |
18 | | - |
19 | | - - name: Set up Docker Buildx |
20 | | - id: buildx |
21 | | - uses: docker/setup-buildx-action@v1 |
22 | | - |
23 | | - - name: Docker meta |
24 | | - id: meta |
25 | | - uses: docker/metadata-action@v3 |
26 | | - with: |
27 | | - images: | |
28 | | - lablabs/cloudflare_exporter |
29 | | - ghcr.io/lablabs/cloudflare_exporter |
30 | | - # generate Docker tags based on the following events/attributes |
31 | | - tags: type=ref,event=tag |
32 | | - |
33 | | - - name: Login to DockerHub |
34 | | - uses: docker/login-action@v1 |
35 | | - with: |
36 | | - username: ${{ secrets.DOCKERHUB_USERNAME }} |
37 | | - password: ${{ secrets.DOCKERHUB_TOKEN }} |
| 20 | + uses: actions/checkout@v5 |
38 | 21 |
|
39 | 22 | - name: Login to Github Packages |
40 | | - uses: docker/login-action@v1 |
| 23 | + uses: docker/login-action@v3 |
41 | 24 | with: |
42 | 25 | registry: ghcr.io |
43 | 26 | username: ${{ github.repository_owner }} |
44 | 27 | password: ${{ secrets.GITHUB_TOKEN }} |
45 | 28 |
|
46 | | - - name: Build image and push to Docker Hub and GitHub Container Registry |
47 | | - uses: docker/build-push-action@v2 |
| 29 | + |
| 30 | + - uses: actions/setup-go@44694675825211faa026b3c33043df3e48a5fa00 # v6 |
| 31 | + with: |
| 32 | + go-version-file: "go.mod" |
| 33 | + cache: false |
| 34 | + |
| 35 | + |
| 36 | + - uses: ko-build/setup-ko@d006021bd0c28d1ce33a07e7943d48b079944c8d # v0.9 |
48 | 37 | with: |
49 | | - context: . |
50 | | - file: ./Dockerfile |
51 | | - platforms: linux/amd64,linux/arm64 |
52 | | - tags: ${{ steps.meta.outputs.tags }} |
53 | | - labels: ${{ steps.meta.outputs.labels }} |
54 | | - push: true |
55 | | - |
56 | | - - name: Image digest |
57 | | - run: echo ${{ steps.docker_build.outputs.digest }} |
58 | | - |
| 38 | + version: v0.18.0 |
| 39 | + - name: Build and push |
| 40 | + id: publish-image |
| 41 | + env: |
| 42 | + IMAGE_VERSION: ${{ github.ref_name }} |
| 43 | + KO_DOCKER_REPO: "ghcr.io/${{ github.repository }}" |
| 44 | + run: | |
| 45 | + ko build . --sbom=none --image-refs ./image-digest --bare --platform linux/arm64,linux/amd64 -t ${IMAGE_VERSION} \ |
| 46 | + --image-label org.opencontainers.image.title=cloudflare-exporter \ |
| 47 | + --image-label org.opencontainers.image.description="Prometheus CloudFlare Exporter" \ |
| 48 | + --image-label org.opencontainers.image.url=${{ github.server_url }}/${{ github.repository }} \ |
| 49 | + --image-label org.opencontainers.image.revision=${{ github.sha }} \ |
| 50 | + --image-label org.opencontainers.image.source=${{ github.server_url }}/${{ github.repository }} \ |
| 51 | + --image-label org.opencontainers.image.licenses=Apache-2.0 \ |
| 52 | + --image-label org.opencontainers.image.version=${IMAGE_VERSION} \ |
| 53 | + --image-label org.opencontainers.image.created="$(date -u +'%Y-%m-%dT%H:%M:%SZ')" |
| 54 | +
|
| 55 | + # Extract image digest from ko output |
| 56 | + # The file contains multiple lines (one per platform + manifest list at the end) |
| 57 | + # We need only the last line (manifest list) for attestation |
| 58 | + cat ./image-digest |
| 59 | + IMAGE_REF=$(head -n 1 ./image-digest) |
| 60 | + IMAGE_DIGEST=$(echo "$IMAGE_REF" | cut -d'@' -f2) |
| 61 | +
|
| 62 | + echo "Image reference: $IMAGE_REF" |
| 63 | + echo "Image digest: $IMAGE_DIGEST" |
| 64 | +
|
| 65 | + echo "digest=$IMAGE_DIGEST" >> "$GITHUB_OUTPUT" |
| 66 | + echo "image-ref=$IMAGE_REF" >> "$GITHUB_OUTPUT" |
| 67 | +
|
| 68 | + - name: Attest |
| 69 | + uses: actions/attest-build-provenance@v3 |
| 70 | + id: attest |
| 71 | + with: |
| 72 | + subject-name: ghcr.io/${{ github.repository }} |
| 73 | + subject-digest: ${{ steps.publish-image.outputs.digest }} |
| 74 | + push-to-registry: true |
| 75 | + |
| 76 | + distribute-to-dockerhub: |
| 77 | + runs-on: ubuntu-latest |
| 78 | + needs: |
| 79 | + - build-and-push-docker-image |
| 80 | + permissions: |
| 81 | + packages: read |
| 82 | + steps: |
| 83 | + - name: Copy image to dockerhub |
| 84 | + env: |
| 85 | + IMAGE_VERSION: ${{ github.ref_name }} |
| 86 | + GHCR_REPO: "ghcr.io/${{ github.repository }}" |
| 87 | + GHCR_USERNAME: ${{ github.actor }} |
| 88 | + GHCR_PASSWORD: ${{ secrets.GITHUB_TOKEN }} |
| 89 | + DOCKER_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }} |
| 90 | + DOCKER_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }} |
| 91 | + run: | |
| 92 | + docker run --rm quay.io/containers/skopeo:v1.18.0 \ |
| 93 | + copy --multi-arch all \ |
| 94 | + --src-creds "$GHCR_USERNAME:$GHCR_PASSWORD" \ |
| 95 | + --dest-creds "$DOCKER_USERNAME:$DOCKER_TOKEN" \ |
| 96 | + docker://${GHCR_REPO}:${IMAGE_VERSION} \ |
| 97 | + docker://lablabs/cloudflare_exporter:${IMAGE_VERSION} |
0 commit comments