|
| 1 | +name: Build and publish Heka SSO Service Docker image |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + push: |
| 6 | + branches: |
| 7 | + - main |
| 8 | + paths: |
| 9 | + - 'heka-sso-service/**' |
| 10 | + - '!heka-sso-service/docs/**' |
| 11 | + - '!heka-sso-service/README.md' |
| 12 | + pull_request: |
| 13 | + branches: |
| 14 | + - main |
| 15 | + paths: |
| 16 | + - 'heka-sso-service/**' |
| 17 | + - '!heka-sso-service/docs/**' |
| 18 | + - '!heka-sso-service/README.md' |
| 19 | + |
| 20 | +env: |
| 21 | + REGISTRY: ghcr.io |
| 22 | + # github.repository as <account>/<repo> |
| 23 | + IMAGE_NAME: ${{ github.repository_owner }}/heka-sso-service |
| 24 | + |
| 25 | +permissions: |
| 26 | + contents: read |
| 27 | + |
| 28 | +jobs: |
| 29 | + build: |
| 30 | + defaults: |
| 31 | + run: |
| 32 | + working-directory: ./heka-sso-service |
| 33 | + runs-on: ubuntu-latest |
| 34 | + permissions: |
| 35 | + contents: read |
| 36 | + packages: write |
| 37 | + # This is used to complete the identity challenge |
| 38 | + # with sigstore/fulcio when running outside of PRs. |
| 39 | + id-token: write |
| 40 | + |
| 41 | + steps: |
| 42 | + - name: Harden the runner (Audit all outbound calls) |
| 43 | + uses: step-security/harden-runner@ec9f2d5744a09debf3a187a3f4f675c53b671911 # v2.13.0 |
| 44 | + with: |
| 45 | + egress-policy: audit |
| 46 | + |
| 47 | + - name: Checkout repository |
| 48 | + uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.1 |
| 49 | + |
| 50 | + # Extract version from package.json for tagging |
| 51 | + - name: Extract version |
| 52 | + id: version |
| 53 | + run: | |
| 54 | + APP_VERSION=$(cat package.json | grep -o '"version": "[^"]*"' | grep -o '[0-9][^"]*') |
| 55 | + COMMIT_SHA=$(git rev-parse --short HEAD) |
| 56 | + echo "commit_sha=${COMMIT_SHA}" >> $GITHUB_OUTPUT |
| 57 | + echo "unique_version=${APP_VERSION}-${COMMIT_SHA}" >> $GITHUB_OUTPUT |
| 58 | + echo "Extracted version: ${APP_VERSION}" |
| 59 | + echo "Commit SHA: ${COMMIT_SHA}" |
| 60 | + echo "Unique version: ${APP_VERSION}-${COMMIT_SHA}" |
| 61 | +
|
| 62 | + # Install the cosign tool except on PR |
| 63 | + # https://github.com/sigstore/cosign-installer |
| 64 | + - name: Install cosign |
| 65 | + if: github.event_name != 'pull_request' |
| 66 | + uses: sigstore/cosign-installer@59acb6260d9c0ba8f4a2f9d9b48431a222b68e20 #v3.5.0 |
| 67 | + with: |
| 68 | + cosign-release: 'v2.2.4' |
| 69 | + |
| 70 | + # Set up BuildKit Docker container builder to be able to build |
| 71 | + # multi-platform images and export cache |
| 72 | + # https://github.com/docker/setup-buildx-action |
| 73 | + - name: Set up Docker Buildx |
| 74 | + uses: docker/setup-buildx-action@f95db51fddba0c2d1ec667646a06c2ce06100226 # v3.0.0 |
| 75 | + |
| 76 | + # Login against a Docker registry except on PR |
| 77 | + # https://github.com/docker/login-action |
| 78 | + - name: Log into registry ${{ env.REGISTRY }} |
| 79 | + if: github.event_name != 'pull_request' |
| 80 | + uses: docker/login-action@343f7c4344506bcbf9b4de18042ae17996df046d # v3.0.0 |
| 81 | + with: |
| 82 | + registry: ${{ env.REGISTRY }} |
| 83 | + username: ${{ github.actor }} |
| 84 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 85 | + |
| 86 | + # Extract metadata (tags, labels) for Docker |
| 87 | + # https://github.com/docker/metadata-action |
| 88 | + - name: Extract Docker metadata |
| 89 | + id: meta |
| 90 | + uses: docker/metadata-action@96383f45573cb7f253c731d3b3ab81c87ef81934 # v5.0.0 |
| 91 | + with: |
| 92 | + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 93 | + tags: | |
| 94 | + type=ref,event=branch |
| 95 | + type=ref,event=pr |
| 96 | + type=raw,value=v${{ steps.version.outputs.unique_version }} |
| 97 | + type=raw,value=latest,enable={{is_default_branch}} |
| 98 | +
|
| 99 | + # Build and push Docker image with Buildx (don't push on PR) |
| 100 | + # https://github.com/docker/build-push-action |
| 101 | + - name: Build and push Docker image |
| 102 | + id: build-and-push |
| 103 | + uses: docker/build-push-action@0565240e2d4ab88bba5387d719585280857ece09 # v5.0.0 |
| 104 | + with: |
| 105 | + context: ./heka-sso-service |
| 106 | + push: ${{ github.event_name != 'pull_request' }} |
| 107 | + tags: ${{ steps.meta.outputs.tags }} |
| 108 | + labels: ${{ steps.meta.outputs.labels }} |
| 109 | + cache-from: type=gha |
| 110 | + cache-to: type=gha,mode=max |
| 111 | + platforms: linux/amd64,linux/arm64 |
| 112 | + |
| 113 | + # Output the image tags for easy copying |
| 114 | + - name: Output image tags |
| 115 | + if: github.event_name != 'pull_request' |
| 116 | + run: | |
| 117 | + echo "🐳 Docker image built and pushed!" |
| 118 | + echo "📋 Image tags:" |
| 119 | + echo "${{ steps.meta.outputs.tags }}" | while read -r tag; do |
| 120 | + echo " 📌 $tag" |
| 121 | + done |
| 122 | + echo "" |
| 123 | + echo "🏷️ Versioned tag:" |
| 124 | + echo " 📌 ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:v${{ steps.version.outputs.unique_version }}" |
| 125 | +
|
| 126 | + # Sign the resulting Docker image digest except on PRs. |
| 127 | + # This will only write to the public Rekor transparency log when the Docker |
| 128 | + # repository is public to avoid leaking data. If you would like to publish |
| 129 | + # transparency data even for private images, pass --force to cosign below. |
| 130 | + # https://github.com/sigstore/cosign |
| 131 | + - name: Sign the published Docker image |
| 132 | + if: ${{ github.event_name != 'pull_request' }} |
| 133 | + env: |
| 134 | + # https://docs.github.com/en/actions/security-guides/security-hardening-for-github-actions#using-an-intermediate-environment-variable |
| 135 | + TAGS: ${{ steps.meta.outputs.tags }} |
| 136 | + DIGEST: ${{ steps.build-and-push.outputs.digest }} |
| 137 | + # This step uses the identity token to provision an ephemeral certificate |
| 138 | + # against the sigstore community Fulcio instance. |
| 139 | + run: echo "${TAGS}" | xargs -I {} cosign sign --yes {}@${DIGEST} |
0 commit comments