|
| 1 | +name: Image build |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + inputs: |
| 6 | + publish: |
| 7 | + description: Publish artifacts to ghcr.io |
| 8 | + default: false |
| 9 | + required: false |
| 10 | + type: boolean |
| 11 | + release: |
| 12 | + description: Whether this is a release build |
| 13 | + default: false |
| 14 | + required: false |
| 15 | + type: boolean |
| 16 | + outputs: |
| 17 | + container-image-name: |
| 18 | + description: Container image name |
| 19 | + value: ${{ jobs.container-image.outputs.name }} |
| 20 | + container-image-digest: |
| 21 | + description: Container image digest |
| 22 | + value: ${{ jobs.container-image.outputs.digest }} |
| 23 | + container-image-tag: |
| 24 | + description: Container image tag |
| 25 | + value: ${{ jobs.container-image.outputs.tag }} |
| 26 | + |
| 27 | +permissions: |
| 28 | + contents: read |
| 29 | + |
| 30 | +jobs: |
| 31 | + container-image: |
| 32 | + name: Container image |
| 33 | + runs-on: ubuntu-latest |
| 34 | + |
| 35 | + permissions: |
| 36 | + contents: read |
| 37 | + packages: write |
| 38 | + id-token: write |
| 39 | + |
| 40 | + outputs: |
| 41 | + name: ${{ steps.image-name.outputs.value }} |
| 42 | + digest: ${{ steps.build.outputs.digest }} |
| 43 | + tag: ${{ steps.meta.outputs.version }} |
| 44 | + |
| 45 | + steps: |
| 46 | + - name: Checkout repository |
| 47 | + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 |
| 48 | + |
| 49 | + - name: Set up QEMU |
| 50 | + uses: docker/setup-qemu-action@53851d14592bedcffcf25ea515637cff71ef929a # v3.3.0 |
| 51 | + |
| 52 | + - name: Set up Docker Buildx |
| 53 | + uses: docker/setup-buildx-action@6524bf65af31da8d45b59e8c27de4bd072b392f5 # v3.8.0 |
| 54 | + |
| 55 | + - name: Set up Cosign |
| 56 | + uses: sigstore/cosign-installer@dc72c7d5c4d10cd6bcb8cf6e3fd625a9e5e537da # v3.7.0 |
| 57 | + if: inputs.publish |
| 58 | + |
| 59 | + - name: Set image name |
| 60 | + id: image-name |
| 61 | + run: echo "value=ghcr.io/axoflow/cloudconnectors" >> "$GITHUB_OUTPUT" |
| 62 | + |
| 63 | + - name: Gather build metadata |
| 64 | + id: meta |
| 65 | + uses: docker/metadata-action@369eb591f429131d6889c46b94e711f089e6ca96 # v5.6.1 |
| 66 | + with: |
| 67 | + images: ${{ steps.image-name.outputs.value }} |
| 68 | + flavor: | |
| 69 | + latest = false |
| 70 | + tags: | |
| 71 | + type=ref,event=branch |
| 72 | + type=ref,event=pr,prefix=pr- |
| 73 | + type=semver,pattern={{raw}} |
| 74 | + type=raw,value=latest,enable={{is_default_branch}} |
| 75 | + labels: | |
| 76 | + org.opencontainers.image.description=Axoflow cloudconnectors helps collecting logs from various cloud-providers. |
| 77 | + org.opencontainers.image.title=Axoflow cloudconnectors |
| 78 | + org.opencontainers.image.authors=Axoflow |
| 79 | +
|
| 80 | + - name: Login to GitHub Container Registry |
| 81 | + uses: docker/login-action@9780b0c442fbb1117ed29e0efdff1e18412f7567 # v3.3.0 |
| 82 | + with: |
| 83 | + registry: ghcr.io |
| 84 | + username: ${{ github.actor }} |
| 85 | + password: ${{ github.token }} |
| 86 | + if: inputs.publish |
| 87 | + |
| 88 | + - name: Build and push image |
| 89 | + id: build |
| 90 | + uses: docker/build-push-action@ca877d9245402d1537745e0e356eab47c3520991 # v6.13.0 |
| 91 | + with: |
| 92 | + context: . |
| 93 | + platforms: linux/amd64,linux/arm64 |
| 94 | + tags: ${{ steps.meta.outputs.tags }} |
| 95 | + labels: ${{ steps.meta.outputs.labels }} |
| 96 | + cache-from: type=gha |
| 97 | + cache-to: type=gha,mode=max |
| 98 | + outputs: | |
| 99 | + type=image,push=${{ inputs.publish }},name=target,annotation-index.org.opencontainers.image.description=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.description'] }} |
| 100 | + type=oci,dest=image.tar,name=target,annotation-index.org.opencontainers.image.description=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.description'] }} |
| 101 | +
|
| 102 | + - name: Sign image with GitHub OIDC Token |
| 103 | + if: ${{ inputs.publish && github.repository_owner == 'axoflow' }} # Check if the workflow is called by the same GitHub organization |
| 104 | + env: |
| 105 | + DIGEST: ${{ steps.build.outputs.digest }} |
| 106 | + TAGS: ${{ steps.meta.outputs.tags }} |
| 107 | + run: | |
| 108 | + readarray -t tag_array <<< "$TAGS" |
| 109 | + for tag in "${tag_array[@]}"; do |
| 110 | + full_image="${tag}@${DIGEST}" |
| 111 | + cosign sign --yes --rekor-url "https://rekor.sigstore.dev/" "${full_image}" |
| 112 | + done |
| 113 | +
|
| 114 | + - name: Verify signed image with cosign |
| 115 | + if: ${{ inputs.publish && github.repository_owner == 'axoflow' }} # Check if the workflow is called by the same GitHub organization |
| 116 | + env: |
| 117 | + DIGEST: ${{ steps.build.outputs.digest }} |
| 118 | + TAGS: ${{ steps.meta.outputs.tags }} |
| 119 | + run: | |
| 120 | + for tag in $TAGS; do |
| 121 | + cosign verify "${tag}@${DIGEST}" \ |
| 122 | + --rekor-url "https://rekor.sigstore.dev/" \ |
| 123 | + --certificate-identity "https://github.com/${{ github.repository }}/.github/workflows/image-build.yaml@${{ github.ref }}" \ |
| 124 | + --certificate-oidc-issuer "https://token.actions.githubusercontent.com" | jq |
| 125 | + done |
| 126 | +
|
| 127 | + - name: Extract OCI tarball |
| 128 | + run: | |
| 129 | + mkdir -p image |
| 130 | + tar -xf image.tar -C image |
| 131 | +
|
| 132 | + - name: Run Trivy vulnerability scanner |
| 133 | + uses: aquasecurity/trivy-action@18f2510ee396bbf400402947b394f2dd8c87dbb0 # 0.29.0 |
| 134 | + env: |
| 135 | + TRIVY_DB_REPOSITORY: public.ecr.aws/aquasecurity/trivy-db:2 |
| 136 | + TRIVY_JAVA_DB_REPOSITORY: public.ecr.aws/aquasecurity/trivy-java-db:1 |
| 137 | + with: |
| 138 | + input: image |
| 139 | + format: sarif |
| 140 | + output: trivy-results.sarif |
| 141 | + |
| 142 | + - name: Upload Trivy scan results as artifact |
| 143 | + uses: actions/upload-artifact@65c4c4a1ddee5b72f698fdd19549f0f0fb45cf08 # v4.6.0 |
| 144 | + with: |
| 145 | + name: "[${{ github.job }}] Trivy scan results" |
| 146 | + path: trivy-results.sarif |
| 147 | + retention-days: 5 |
0 commit comments