|
| 1 | +name: Build |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + push: |
| 6 | + branches: |
| 7 | + - "main" |
| 8 | + workflow_dispatch: |
| 9 | + |
| 10 | +permissions: {} |
| 11 | + |
| 12 | +env: |
| 13 | + _AZURE_REGISTRY: bitwardenprod.azurecr.io |
| 14 | + _IMAGE_NAME: TEMPLATE_IMAGE_NAME # UPDATE: TEMPLATE VALUE TO UPDATE WHEN COPIED |
| 15 | + |
| 16 | +jobs: |
| 17 | + build: |
| 18 | + name: Build |
| 19 | + runs-on: ubuntu-24.04 |
| 20 | + permissions: |
| 21 | + contents: read |
| 22 | + security-events: write |
| 23 | + packages: write |
| 24 | + id-token: write |
| 25 | + |
| 26 | + steps: |
| 27 | + - name: Check if image should be published |
| 28 | + env: |
| 29 | + PUBLISH_BRANCHES: "main" |
| 30 | + PUBLISH_TAGS: "false" |
| 31 | + PUBLISH_PR: "true" |
| 32 | + run: | |
| 33 | + BRANCH_REF=${GITHUB_REF#refs/heads/} |
| 34 | + IFS="," read -a publish_branches <<< "$PUBLISH_BRANCHES" |
| 35 | +
|
| 36 | + if [[ "${PUBLISH_TAGS}" == "true" && "${GITHUB_REF}" == refs/tags/* ]]; then |
| 37 | + echo "push_image=true" >> "$GITHUB_ENV" |
| 38 | + elif [[ "${publish_branches[*]}" =~ "${BRANCH_REF}" ]]; then |
| 39 | + echo "push_image=true" >> "$GITHUB_ENV" |
| 40 | + else |
| 41 | + echo "push_image=false" >> "$GITHUB_ENV" |
| 42 | + fi |
| 43 | +
|
| 44 | + - name: Check out repo |
| 45 | + uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0 |
| 46 | + with: |
| 47 | + persist-credentials: false |
| 48 | + |
| 49 | + ########## Set up Docker ########## |
| 50 | + - name: Set up QEMU emulators |
| 51 | + uses: docker/setup-qemu-action@29109295f81e9208d7d86ff1c6c12d2833863392 # v3.6.0 |
| 52 | + |
| 53 | + - name: Set up Docker Buildx |
| 54 | + uses: docker/setup-buildx-action@e468171a9de216ec08956ac3ada2f0791b6bd435 # v3.11.1 |
| 55 | + |
| 56 | + ########## Login to Docker registries ########## |
| 57 | + - name: Log in to Azure |
| 58 | + uses: bitwarden/gh-actions/azure-login@main |
| 59 | + with: |
| 60 | + subscription_id: ${{ secrets.AZURE_SUBSCRIPTION_ID }} |
| 61 | + tenant_id: ${{ secrets.AZURE_TENANT_ID }} |
| 62 | + client_id: ${{ secrets.AZURE_CLIENT_ID }} |
| 63 | + |
| 64 | + - name: Login to Azure ACR |
| 65 | + run: az acr login -n "${_AZURE_REGISTRY%%.*}" |
| 66 | + |
| 67 | + ########## Generate image tag and build Docker image ########## |
| 68 | + - name: Generate Docker image tag |
| 69 | + id: tag |
| 70 | + env: |
| 71 | + EVENT_TYPE: ${{ contains(github.event_name, 'pull_request') && 'pull_request' || '' }} |
| 72 | + run: | |
| 73 | + if [[ "$EVENT_TYPE" == "pull_request" ]]; then |
| 74 | + IMAGE_TAG="pr-${{ github.event.pull_request.number }}" |
| 75 | + else |
| 76 | + IMAGE_TAG=$(echo "${GITHUB_REF:11}" | sed "s#/#-#g") # slash safe branch name |
| 77 | + if [[ "$IMAGE_TAG" == "main" ]]; then |
| 78 | + IMAGE_TAG=dev |
| 79 | + fi |
| 80 | + fi |
| 81 | + echo "image_tag=$IMAGE_TAG" >> "$GITHUB_OUTPUT" |
| 82 | +
|
| 83 | + - name: Generate image tag(s) |
| 84 | + id: image-tags |
| 85 | + env: |
| 86 | + IMAGE_TAG: ${{ steps.tag.outputs.image_tag }} |
| 87 | + SHA: ${{ github.sha }} |
| 88 | + run: | |
| 89 | + FULL_IMAGE_NAME="${_AZURE_REGISTRY}/${_IMAGE_NAME}" |
| 90 | + TAGS="${FULL_IMAGE_NAME}:${IMAGE_TAG}" |
| 91 | + echo "primary_tag=$TAGS" >> "$GITHUB_OUTPUT" |
| 92 | + if [[ "$IMAGE_TAG" == "dev" ]]; then |
| 93 | + SHORT_SHA="$(git rev-parse --short "${SHA}")" |
| 94 | + TAGS="$TAGS,${FULL_IMAGE_NAME}:${IMAGE_TAG}-${SHORT_SHA}" |
| 95 | + fi |
| 96 | + echo "tags=$TAGS" >> "$GITHUB_OUTPUT" |
| 97 | +
|
| 98 | + ########## Build Docker image ########## |
| 99 | + - name: Build Docker image |
| 100 | + id: build-docker |
| 101 | + uses: docker/build-push-action@263435318d21b8e681c14492fe198d362a7d2c83 # v6.18.0 |
| 102 | + with: |
| 103 | + context: . |
| 104 | + file: Dockerfile |
| 105 | + platforms: linux/amd64 |
| 106 | + push: ${{ env.push_image == 'true' }} |
| 107 | + tags: ${{ steps.image-tags.outputs.tags }} |
| 108 | + env: |
| 109 | + DOCKER_BUILD_RECORD_UPLOAD: false |
| 110 | + |
| 111 | + - name: Install Cosign |
| 112 | + if: env.push_image == 'true' |
| 113 | + uses: sigstore/cosign-installer@faadad0cce49287aee09b3a48701e75088a2c6ad # v4.0.0 |
| 114 | + |
| 115 | + - name: Sign image with Cosign |
| 116 | + if: env.push_image == 'true' |
| 117 | + env: |
| 118 | + DIGEST: ${{ steps.build-docker.outputs.digest }} |
| 119 | + TAGS: ${{ steps.image-tags.outputs.tags }} |
| 120 | + run: | |
| 121 | + IFS=',' read -r -a tags_array <<< "${TAGS}" |
| 122 | + images=() |
| 123 | + for tag in "${tags_array[@]}"; do |
| 124 | + images+=("${tag}@${DIGEST}") |
| 125 | + done |
| 126 | + cosign sign --yes ${images[@]} # Do not quote the array to expand properly |
| 127 | +
|
| 128 | + - name: Scan Docker image |
| 129 | + id: container-scan |
| 130 | + uses: anchore/scan-action@568b89d27fc18c60e56937bff480c91c772cd993 # v7.1.0 |
| 131 | + with: |
| 132 | + image: ${{ steps.image-tags.outputs.primary_tag }} |
| 133 | + fail-build: false |
| 134 | + output-format: sarif |
| 135 | + |
| 136 | + - name: Upload Grype results to GitHub |
| 137 | + if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository |
| 138 | + uses: github/codeql-action/upload-sarif@0499de31b99561a6d14a36a5f662c2a54f91beee # v4.31.2 |
| 139 | + with: |
| 140 | + sarif_file: ${{ steps.container-scan.outputs.sarif }} |
| 141 | + sha: ${{ contains(github.event_name, 'pull_request') && github.event.pull_request.head.sha || github.sha }} |
| 142 | + ref: ${{ contains(github.event_name, 'pull_request') && format('refs/pull/{0}/head', github.event.pull_request.number) || github.ref }} |
| 143 | + |
| 144 | + - name: Log out of Docker |
| 145 | + run: | |
| 146 | + docker logout "$_AZURE_REGISTRY" |
| 147 | +
|
| 148 | + - name: Log out from Azure |
| 149 | + uses: bitwarden/gh-actions/azure-logout@main |
0 commit comments