|
| 1 | +name: CI Pipeline |
| 2 | + |
| 3 | +# This workflow build and run tests inside the dev container |
| 4 | +# only after the pull request is merged to main branch. |
| 5 | +# Once tests are successful it builds and pushes the |
| 6 | +# smaller size production image with multiarch suppport. |
| 7 | + |
| 8 | +on: |
| 9 | + push: |
| 10 | + branches: [ "main" ] |
| 11 | + |
| 12 | +env: |
| 13 | + # Use docker.io for Docker Hub if empty |
| 14 | + REGISTRY: ghcr.io |
| 15 | + # github.repository as <account>/<repo> |
| 16 | + IMAGE_NAME: ${{ github.repository }} |
| 17 | + TEST_TAG: user/app:test |
| 18 | + |
| 19 | +jobs: |
| 20 | + build: |
| 21 | + runs-on: ubuntu-latest |
| 22 | + permissions: |
| 23 | + contents: read |
| 24 | + packages: write |
| 25 | + # This is used to complete the identity challenge |
| 26 | + # with sigstore/fulcio when running outside of PRs. |
| 27 | + id-token: write |
| 28 | + |
| 29 | + steps: |
| 30 | + - name: Checkout repository |
| 31 | + uses: actions/checkout@v3 |
| 32 | + |
| 33 | + - name: Set up QEMU |
| 34 | + uses: docker/setup-qemu-action@v2 |
| 35 | + |
| 36 | + # Install the cosign tool except on PR |
| 37 | + # https://github.com/sigstore/cosign-installer |
| 38 | + - name: Install cosign |
| 39 | + uses: sigstore/cosign-installer@6e04d228eb30da1757ee4e1dd75a0ec73a653e06 #v3.1.1 |
| 40 | + with: |
| 41 | + cosign-release: 'v2.1.1' |
| 42 | + |
| 43 | + # Workaround: https://github.com/docker/build-push-action/issues/461 |
| 44 | + - name: Setup Docker buildx |
| 45 | + uses: docker/setup-buildx-action@79abd3f86f79a9d68a23c75a09a9a85889262adf |
| 46 | + |
| 47 | + # Login against a Docker registry except on PR |
| 48 | + # https://github.com/docker/login-action |
| 49 | + - name: Log into registry ${{ env.REGISTRY }} |
| 50 | + uses: docker/login-action@28218f9b04b4f3f62068d7b6ce6ca5b26e35336c |
| 51 | + with: |
| 52 | + registry: ${{ env.REGISTRY }} |
| 53 | + username: ${{ github.actor }} |
| 54 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 55 | + |
| 56 | + # Build and push Docker image with Buildx |
| 57 | + # https://github.com/docker/build-push-action |
| 58 | + - name: Build test image |
| 59 | + id: build-test |
| 60 | + uses: docker/build-push-action@ac9327eae2b366085ac7f6a2d02df8aa8ead720a |
| 61 | + with: |
| 62 | + context: . |
| 63 | + load: true |
| 64 | + target: dev |
| 65 | + tags: ${{ env.TEST_TAG }} |
| 66 | + cache-from: type=gha |
| 67 | + cache-to: type=gha,mode=max |
| 68 | + |
| 69 | + - name: Test |
| 70 | + run: | |
| 71 | + docker run --rm ${{ env.TEST_TAG }} make ci-test |
| 72 | +
|
| 73 | + # Extract metadata (tags, labels) for Docker |
| 74 | + # https://github.com/docker/metadata-action |
| 75 | + - name: Extract Docker metadata |
| 76 | + id: meta |
| 77 | + uses: docker/metadata-action@v4 |
| 78 | + with: |
| 79 | + # list of Docker images to use as base name for tags |
| 80 | + images: | |
| 81 | + ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 82 | + # generate Docker tags based on the following events/attributes |
| 83 | + tags: | |
| 84 | + type=sha |
| 85 | + type=raw,value={{branch}}-latest |
| 86 | + type=raw,value={{branch}}-{{date 'YYYYMMDDHHmmss'}} |
| 87 | +
|
| 88 | + # Build and push Docker image with Buildx |
| 89 | + # https://github.com/docker/build-push-action |
| 90 | + - name: Build and push production image |
| 91 | + id: build-and-push |
| 92 | + uses: docker/build-push-action@ac9327eae2b366085ac7f6a2d02df8aa8ead720a |
| 93 | + with: |
| 94 | + context: . |
| 95 | + target: http_app |
| 96 | + platforms: linux/amd64,linux/arm64 |
| 97 | + push: true |
| 98 | + tags: ${{ steps.meta.outputs.tags }} |
| 99 | + labels: ${{ steps.meta.outputs.labels }} |
| 100 | + cache-from: type=gha |
| 101 | + cache-to: type=gha,mode=max |
| 102 | + |
| 103 | + # Sign the resulting Docker image digest except on PRs. |
| 104 | + # This will only write to the public Rekor transparency log when the Docker |
| 105 | + # repository is public to avoid leaking data. If you would like to publish |
| 106 | + # transparency data even for private images, pass --force to cosign below. |
| 107 | + # https://github.com/sigstore/cosign |
| 108 | + - name: Sign the published Docker image |
| 109 | + env: |
| 110 | + # https://docs.github.com/en/actions/security-guides/security-hardening-for-github-actions#using-an-intermediate-environment-variable |
| 111 | + TAGS: ${{ steps.meta.outputs.tags }} |
| 112 | + DIGEST: ${{ steps.build-and-push.outputs.digest }} |
| 113 | + # This step uses the identity token to provision an ephemeral certificate |
| 114 | + # against the sigstore community Fulcio instance. |
| 115 | + run: echo "${TAGS}" | xargs -I {} cosign sign --yes {}@${DIGEST} |
0 commit comments