|
| 1 | +name: docker |
| 2 | +on: |
| 3 | + push: |
| 4 | + # TODO: Build on main? |
| 5 | + branches: |
| 6 | + - main |
| 7 | + tags: |
| 8 | + - '**' |
| 9 | + # TODO: Remove pull_request before merging |
| 10 | + pull_request: |
| 11 | + branches: |
| 12 | + - main |
| 13 | + |
| 14 | +permissions: |
| 15 | + contents: read |
| 16 | + packages: write |
| 17 | + # Ensure cosign can request for Github's OIDC JWT ID token |
| 18 | + # See: https://docs.github.com/en/actions/deployment/security-hardening-your-deployments/configuring-openid-connect-in-cloud-providers#adding-permissions-settings |
| 19 | + id-token: write |
| 20 | + |
| 21 | +jobs: |
| 22 | + # This job builds the binaries and uploads it as github artifacts. |
| 23 | + # This allows us to use the same binaries for any release CI jobs. |
| 24 | + build: |
| 25 | + name: Build all linux architectures |
| 26 | + runs-on: ubuntu-latest |
| 27 | + steps: |
| 28 | + - name: setup go |
| 29 | + uses: actions/setup-go@v3 |
| 30 | + with: |
| 31 | + go-version: ${{ env.GO_VERSION }} |
| 32 | + |
| 33 | + - uses: actions/checkout@v3 |
| 34 | + with: |
| 35 | + # Fetch all tags |
| 36 | + fetch-depth: 0 |
| 37 | + |
| 38 | + - name: Build on all supported architectures |
| 39 | + run: | |
| 40 | + set -e |
| 41 | + ./scripts/release.sh |
| 42 | +
|
| 43 | + - uses: actions/upload-artifact@v3 |
| 44 | + with: |
| 45 | + name: binaries-${{ github.sha }} |
| 46 | + path: | |
| 47 | + release*/* |
| 48 | +
|
| 49 | + # This job downloads the binaries previously uploaded as artifacts, and builds multi-arch images |
| 50 | + build-docker-image: |
| 51 | + needs: [build] |
| 52 | + runs-on: ubuntu-latest |
| 53 | + steps: |
| 54 | + - name: Checkout |
| 55 | + uses: actions/checkout@v3 |
| 56 | + with: |
| 57 | + # Fetch all tags |
| 58 | + fetch-depth: 0 |
| 59 | + |
| 60 | + - name: Prepare |
| 61 | + id: prep |
| 62 | + run: | |
| 63 | + set -e |
| 64 | + TAG=$( git describe --tags --dirty ) # E.g. v1.2.0-23-g60ee190 |
| 65 | + echo "TAG=$TAG" >> $GITHUB_ENV |
| 66 | +
|
| 67 | + # This step generates the docker tags |
| 68 | + - name: Docker meta |
| 69 | + id: meta |
| 70 | + uses: docker/metadata-action@v4 |
| 71 | + with: |
| 72 | + images: | |
| 73 | + ${{ github.repository }} |
| 74 | + ghcr.io/${{ github.repository }} |
| 75 | + # type=ref,event=pr generates tag(s) on PRs only. E.g. 'pr-123', 'pr-123-abc0123' |
| 76 | + # type=ref,event=branch generates tag(s) on branch only. E.g. 'main-abc0123' |
| 77 | + # type=semver generates tag(s) on tags only. E.g. 'v0', 'v0.0', 'v0.0.0', and 'latest' |
| 78 | + tags: | |
| 79 | + type=ref,event=pr |
| 80 | + type=ref,suffix=-{{sha}},event=branch |
| 81 | + type=semver,pattern=v{{major}} |
| 82 | + type=semver,pattern=v{{major}}.{{minor}} |
| 83 | + type=semver,pattern=v{{major}}.{{minor}}.{{patch}} |
| 84 | + # The rest of the org.opencontainers.image.xxx labels are dynamically generated |
| 85 | + labels: | |
| 86 | + org.opencontainers.image.description=CNI Plugins |
| 87 | + org.opencontainers.image.licenses=Apache License 2.0 |
| 88 | +
|
| 89 | + # See: https://github.com/docker/build-push-action/blob/v2.6.1/docs/advanced/cache.md#github-cache |
| 90 | + - name: Set up QEMU |
| 91 | + uses: docker/setup-qemu-action@v2 |
| 92 | + |
| 93 | + - name: Set up Docker Buildx |
| 94 | + id: buildx |
| 95 | + uses: docker/setup-buildx-action@v2 |
| 96 | + |
| 97 | + - name: Cache Docker layers |
| 98 | + uses: actions/cache@v3 |
| 99 | + with: |
| 100 | + path: /tmp/.buildx-cache |
| 101 | + key: ${{ runner.os }}-buildx-${{ github.sha }} |
| 102 | + restore-keys: | |
| 103 | + ${{ runner.os }}-buildx- |
| 104 | +
|
| 105 | + - name: Login to Docker Hub registry |
| 106 | + uses: docker/login-action@v2 |
| 107 | + with: |
| 108 | + username: ${{ secrets.DOCKERHUB_USERNAME }} |
| 109 | + password: ${{ secrets.DOCKERHUB_TOKEN }} |
| 110 | + |
| 111 | + - name: Login to GitHub Container Registry |
| 112 | + uses: docker/login-action@v2 |
| 113 | + with: |
| 114 | + registry: ghcr.io |
| 115 | + username: ${{ github.actor }} |
| 116 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 117 | + |
| 118 | + - uses: actions/download-artifact@v3 |
| 119 | + with: |
| 120 | + name: binaries-${{ github.sha }} |
| 121 | + |
| 122 | + - run: | |
| 123 | + ls -al release*/ |
| 124 | +
|
| 125 | + - name: Build and push |
| 126 | + id: build-and-push |
| 127 | + # TODO: Remove pull_request before merging |
| 128 | + if: github.event_name == 'pull_request' || github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/') |
| 129 | + uses: docker/build-push-action@v3 |
| 130 | + with: |
| 131 | + build-args: | |
| 132 | + TAG=${{ env.TAG }} |
| 133 | + context: '.' |
| 134 | + file: Dockerfile |
| 135 | + platforms: linux/amd64,linux/arm,linux/arm64,linux/mips64le,linux/ppc64le,linux/riscv64,linux/s390x |
| 136 | + push: true |
| 137 | + tags: ${{ steps.meta.outputs.tags }} |
| 138 | + labels: ${{ steps.meta.outputs.labels }} |
| 139 | + cache-from: type=local,src=/tmp/.buildx-cache |
| 140 | + cache-to: type=local,dest=/tmp/.buildx-cache-new,mode=max |
| 141 | + |
| 142 | + - name: Install cosign |
| 143 | + uses: sigstore/cosign-installer@c3667d99424e7e6047999fb6246c0da843953c65 # v3.0.1 |
| 144 | + |
| 145 | + # This signs the image using ACTIONS_ID_TOKEN_REQUEST_TOKEN |
| 146 | + - name: Sign the published Docker image |
| 147 | + run: echo "${{ steps.meta.outputs.tags }}" | xargs -I {} cosign sign --yes {}@${{ steps.build-and-push.outputs.digest }} |
| 148 | + |
| 149 | + # Temp fix |
| 150 | + # https://github.com/docker/build-push-action/issues/252 |
| 151 | + # https://github.com/moby/buildkit/issues/1896 |
| 152 | + - name: Move cache |
| 153 | + run: | |
| 154 | + rm -rf /tmp/.buildx-cache |
| 155 | + mv /tmp/.buildx-cache-new /tmp/.buildx-cache |
0 commit comments