diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 5071f61..1c16a71 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -2,23 +2,71 @@ name: "-" on: workflow_call: + inputs: + REPO_REF: + required: false + type: string + description: "Branch or tag to build" + outputs: + COMMIT_SHA: + value: ${{ jobs.build.outputs.COMMIT_SHA }} + description: "The actual commit SHA that was built" workflow_dispatch: + inputs: + REPO_REF: + required: false + type: string + description: "Branch or tag to build" jobs: build: name: ${{ github.job.name }} runs-on: ${{ vars.RUNNER_TYPE }} + permissions: + id-token: write + contents: read + outputs: + COMMIT_SHA: ${{ steps.get-sha.outputs.COMMIT_SHA }} steps: - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6 + with: + ref: ${{ inputs.REPO_REF || github.ref }} + + - name: Get actual commit SHA + id: get-sha + run: | + COMMIT_SHA=$(git rev-parse HEAD) + echo "COMMIT_SHA=${COMMIT_SHA}" >> "$GITHUB_OUTPUT" + echo "Building commit: ${COMMIT_SHA}" + + - name: Configure AWS credentials + uses: aws-actions/configure-aws-credentials@d979d5b3a71173a29b74b5b88418bfda9437d885 # v6 + with: + role-to-assume: ${{ vars.DEV_ROLE_ARN }} + aws-region: us-east-1 + - name: Login to Amazon ECR + id: login-ecr + uses: aws-actions/amazon-ecr-login@fa648b43de3d4d023bcb3f89ed6940096949c419 # v2 + + - name: Set up Docker Buildx + run: | + docker buildx create --name mybuilder --use --bootstrap --driver-opt image=${{ steps.login-ecr.outputs.registry }}/docker-hub/moby/buildkit:latest - name: Build image + env: + COMMIT_SHA: ${{ steps.get-sha.outputs.COMMIT_SHA }} run: | - docker build --file Dockerfile -t ${{ github.sha }} . - docker save -o /tmp/${{ github.sha }}.tar ${{ github.sha }} + sed -i "s|public.ecr.aws|${{ steps.login-ecr.outputs.registry }}/ecr-public|g" Dockerfile + docker buildx build \ + --cache-from type=registry,ref=${{ steps.login-ecr.outputs.registry }}/${{ vars.ECR_BUILD_CACHE_REPO }}:${{ github.event.repository.name }} \ + --cache-to type=registry,ref=${{ steps.login-ecr.outputs.registry }}/${{ vars.ECR_BUILD_CACHE_REPO }}:${{ github.event.repository.name }},mode=max \ + --output type=docker,dest=/tmp/"${COMMIT_SHA}".tar \ + -t "${COMMIT_SHA}" \ + . - name: Upload artifact uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7 with: - name: ${{ github.sha }} - path: /tmp/${{ github.sha }}.tar - retention-days: 1 \ No newline at end of file + name: ${{ steps.get-sha.outputs.COMMIT_SHA }} + path: /tmp/${{ steps.get-sha.outputs.COMMIT_SHA }}.tar + retention-days: 1 diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml index 87030cb..76138b8 100644 --- a/.github/workflows/cd.yml +++ b/.github/workflows/cd.yml @@ -8,60 +8,55 @@ on: workflow_dispatch: inputs: + REPO_REF: + required: false + type: string + description: "Branch or tag to build" AWS_ENV: required: true default: DEV - type: choice + type: string description: Environment to Deploy - options: [ DEV, STAGE, PROD ] UUID: required: false type: string - description: UUID of workflow - -env: - SVC_NAME: "cesr_verifier" + description: "Automation only. Leave empty" jobs: - setup: + # Skips the whole chain when a deploy freeze is active, before any image is built. + preflight: if: | (contains(github.event.pull_request.title, '[no-deploy]') == false && github.event.pull_request.merged == true) || inputs.UUID || github.event_name == 'workflow_dispatch' - runs-on: ${{ vars.RUNNER_TYPE }} - steps: - - run: echo "Dummy" - outputs: - AWS_ENV: ${{ inputs.AWS_ENV || 'DEV' }} - SVC_NAME: ${{ env.SVC_NAME }} - - build: - needs: setup - uses: ./.github/workflows/build.yml + uses: provenant-dev/github-action-helper/.github/workflows/preflight-deploy.yml@main secrets: inherit + with: + DEPLOY_ENV: ${{ inputs.AWS_ENV || 'DEV' }} - tag-push: - name: ${{ needs.setup.outputs.AWS_ENV }} - needs: [ setup, build ] - uses: provenant-dev/github-action-helper/.github/workflows/tag-push.yml@main + publish: + needs: preflight + if: needs.preflight.outputs.can_deploy == 'true' + uses: ./.github/workflows/publish.yml secrets: inherit with: - AWS_ENV: ${{ needs.setup.outputs.AWS_ENV }} + REPO_REF: ${{ inputs.REPO_REF || github.event.pull_request.merge_commit_sha || github.ref }} + AWS_ENV: ${{ inputs.AWS_ENV || 'DEV' }} set-version: - name: ${{ needs.setup.outputs.AWS_ENV }} - needs: [ setup, tag-push ] + name: ${{ inputs.AWS_ENV || 'DEV' }} + needs: publish uses: provenant-dev/github-action-helper/.github/workflows/set-version.yml@main secrets: inherit with: - DEPLOY_ENV: ${{ needs.setup.outputs.AWS_ENV }} - RELEASE_VERSION: ${{ needs.tag-push.outputs.RELEASE_VERSION }} - SVC_NAME: ${{ needs.setup.outputs.SVC_NAME }} + DEPLOY_ENV: ${{ inputs.AWS_ENV || 'DEV' }} + RELEASE_VERSION: ${{ needs.publish.outputs.RELEASE_VERSION }} + SVC_NAME: "cesr_verifier" deploy: - name: ${{ needs.setup.outputs.AWS_ENV }} - needs: [ setup, set-version ] + name: ${{ inputs.AWS_ENV || 'DEV' }} + needs: set-version uses: provenant-dev/github-action-helper/.github/workflows/initiate-deployment.yml@main secrets: inherit with: - DEPLOY_ENV: ${{ needs.setup.outputs.AWS_ENV }} + DEPLOY_ENV: ${{ inputs.AWS_ENV || 'DEV' }} diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml new file mode 100644 index 0000000..3b0c806 --- /dev/null +++ b/.github/workflows/publish.yml @@ -0,0 +1,107 @@ +name: Publish +run-name: "${{ inputs.AWS_ENV || 'DEV' }}: publish ${{ inputs.REPO_REF || github.ref_name }} ${{ inputs.UUID }}" + +# Builds, scans and pushes the service image to ECR without deploying it. +# cd.yml calls it before set-version and deploy. +# +# Manual use: Actions -> Publish -> Run workflow. Pick the branch in +# "Use workflow from" and leave REPO_REF empty. To build a branch that does not +# contain this file yet, a tag or a commit, keep "Use workflow from" on the +# default branch and put the ref in REPO_REF. The image is pushed as +# :. + +on: + workflow_dispatch: + inputs: + REPO_REF: + required: false + type: string + description: "Optional. Branch, tag or SHA to build. Leave empty to build the branch picked in \"Use workflow from\"" + AWS_ENV: + required: true + default: DEV + type: string + description: "ECR to push the image to. Keep DEV unless you are releasing" + UUID: + required: false + type: string + description: "Automation only. Leave empty" + + workflow_call: + inputs: + REPO_REF: + required: false + type: string + description: "Branch, tag or commit to build" + AWS_ENV: + required: false + default: DEV + type: string + description: Environment whose ECR receives the image + outputs: + COMMIT_SHA: + value: ${{ jobs.setup.outputs.COMMIT_SHA }} + description: "The commit that was built" + RELEASE_VERSION: + value: ${{ jobs.tag-push.outputs.RELEASE_VERSION }} + description: "The image tag pushed to ECR" + +jobs: + setup: + runs-on: ${{ vars.RUNNER_TYPE }} + steps: + - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6 + with: + ref: ${{ inputs.REPO_REF || github.ref }} + + - name: Get commit SHA + id: get-sha + env: + REPO_REF: ${{ inputs.REPO_REF || github.ref }} + run: | + COMMIT_SHA=$(git rev-parse HEAD) + echo "COMMIT_SHA=${COMMIT_SHA}" >> "$GITHUB_OUTPUT" + echo "Commit SHA for REPO_REF '${REPO_REF}': ${COMMIT_SHA}" + outputs: + AWS_ENV: ${{ inputs.AWS_ENV || 'DEV' }} + COMMIT_SHA: ${{ steps.get-sha.outputs.COMMIT_SHA }} + REPO_REF: ${{ inputs.REPO_REF || github.ref }} + + check-image: + uses: provenant-dev/github-action-helper/.github/workflows/check-image.yml@main + needs: setup + secrets: inherit + with: + DEPLOY_ENV: ${{ needs.setup.outputs.AWS_ENV }} + COMMIT_HASH: ${{ needs.setup.outputs.COMMIT_SHA }} + + build: + needs: [ setup, check-image ] + if: needs.check-image.outputs.IMAGE_EXISTS != 'true' + uses: ./.github/workflows/build.yml + secrets: inherit + with: + REPO_REF: ${{ needs.setup.outputs.COMMIT_SHA }} + + vulnerability-scan: + needs: [setup, build] + uses: provenant-dev/github-action-helper/.github/workflows/vulnerability-scan-image.yml@main + secrets: inherit + permissions: + contents: write # POST built-image SBOM to GH Dependency Graph + id-token: write # OIDC -> ECR for Trivy DB pull + with: + IMAGE_ARTIFACT: ${{ needs.setup.outputs.COMMIT_SHA }} + + tag-push: + name: ${{ needs.setup.outputs.AWS_ENV }} + needs: [ setup, check-image, build ] + # Runs when the build was skipped because the image already exists. + if: ${{ always() && needs.setup.result == 'success' && !failure() && !cancelled() }} + uses: provenant-dev/github-action-helper/.github/workflows/tag-push.yml@main + secrets: inherit + with: + AWS_ENV: ${{ needs.setup.outputs.AWS_ENV }} + IMAGE_EXISTS: ${{ needs.check-image.outputs.IMAGE_EXISTS }} + IMAGE_TAG: ${{ needs.setup.outputs.COMMIT_SHA }} + REPO_REF: ${{ needs.setup.outputs.REPO_REF }}