|
| 1 | +name: Build and Push Docker image to ECR |
| 2 | + |
| 3 | +permissions: |
| 4 | + contents: write |
| 5 | + id-token: write |
| 6 | + |
| 7 | +on: |
| 8 | + workflow_dispatch: |
| 9 | + inputs: |
| 10 | + ignore_trivy_scan: |
| 11 | + type: boolean |
| 12 | + description: Ignore vulnerabilities if they are found |
| 13 | + required: true |
| 14 | + default: true |
| 15 | + |
| 16 | +jobs: |
| 17 | + build-and-push: |
| 18 | + name: Build and push image to ECR |
| 19 | + runs-on: ubuntu-latest |
| 20 | + env: |
| 21 | + ECR_REPOSITORY: ccdi-cbioportal-content-ui |
| 22 | + |
| 23 | + steps: |
| 24 | + - name: Checkout repository |
| 25 | + uses: actions/checkout@ee0669bd1cc54295c223e0bb666b733df41de1c5 #2.7.0 |
| 26 | + |
| 27 | + |
| 28 | + - name: Set Image Tag |
| 29 | + env: |
| 30 | + BRANCH_NAME: ${{ github.head_ref || github.ref_name }} |
| 31 | + run: | |
| 32 | + # Get all tags for the repo and find the latest tag for the branch being built |
| 33 | + git fetch --tags --force --quiet |
| 34 | + tag=$(git tag -l $BRANCH_NAME* | sort -V | tail -1) |
| 35 | +
|
| 36 | + if [ ! -z "$tag" ]; |
| 37 | + then |
| 38 | + # Increment the build number if a tag is found |
| 39 | + build_num=$(echo "${tag##*.}") |
| 40 | + build_num=$((build_num+1)) |
| 41 | + echo "IMAGE_TAG=$BRANCH_NAME.$build_num" >> $GITHUB_ENV |
| 42 | + else |
| 43 | + # If no tag is found create a new tag name |
| 44 | + build_num=1 |
| 45 | + echo "IMAGE_TAG=$BRANCH_NAME.$build_num" >> $GITHUB_ENV |
| 46 | + fi |
| 47 | +
|
| 48 | + - name: Set up Docker Buildx |
| 49 | + uses: docker/setup-buildx-action@v2 |
| 50 | + |
| 51 | + - name: Build Docker image |
| 52 | + run: | |
| 53 | + docker buildx build --no-cache --output type=docker --platform linux/amd64 --build-arg NEXT_PUBLIC_CONTENT_API_TOKEN=${{ secrets.NEXT_PUBLIC_CONTENT_API_TOKEN }} -t ${{ env.ECR_REPOSITORY }}:${{ env.IMAGE_TAG }} . |
| 54 | +
|
| 55 | + - name: Set Trivy exit code |
| 56 | + run: | |
| 57 | + if [[ ${{ inputs.ignore_trivy_scan }} == true ]]; |
| 58 | + then |
| 59 | + echo 'TRIVY_EXIT_CODE=0' >> $GITHUB_ENV |
| 60 | + else |
| 61 | + echo 'TRIVY_EXIT_CODE=1' >> $GITHUB_ENV |
| 62 | + fi |
| 63 | +
|
| 64 | + - name: Run Trivy vulnerability scanner |
| 65 | + id: trivy-scan |
| 66 | + uses: aquasecurity/trivy-action@6c175e9c4083a92bbca2f9724c8a5e33bc2d97a5 # v0.30.0 |
| 67 | + with: |
| 68 | + image-ref: '${{ env.ECR_REPOSITORY }}:${{ env.IMAGE_TAG }}' |
| 69 | + format: 'table' |
| 70 | + exit-code: '${{ env.TRIVY_EXIT_CODE }}' |
| 71 | + ignore-unfixed: true |
| 72 | + severity: 'CRITICAL,HIGH' |
| 73 | + |
| 74 | + - name: Create Git tag for Image |
| 75 | + run: | |
| 76 | + git config user.name "GitHub Actions" |
| 77 | + git config user.email "github-actions@users.noreply.github.com" |
| 78 | + git tag ${{ env.IMAGE_TAG }} |
| 79 | + git push origin ${{ env.IMAGE_TAG }} |
| 80 | + |
| 81 | + - name: Authenticate to OIDC |
| 82 | + uses: aws-actions/configure-aws-credentials@e3dd6a429d7300a6a4c196c26e071d42e0343502 # v4.0.2 |
| 83 | + with: |
| 84 | + role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME }} |
| 85 | + aws-region: ${{ secrets.AWS_REGION }} |
| 86 | + role-session-name: ${{ github.actor }} |
| 87 | + |
| 88 | + - name: Login to Amazon ECR |
| 89 | + id: login-aws-ecr |
| 90 | + uses: aws-actions/amazon-ecr-login@062b18b96a7aff071d4dc91bc00c4c1a7945b076 # v2.0.1 |
| 91 | + |
| 92 | + - name: Push Docker image to ECR |
| 93 | + id: push-image |
| 94 | + env: |
| 95 | + ECR_REGISTRY: ${{ steps.login-aws-ecr.outputs.registry }} |
| 96 | + run: | |
| 97 | + docker tag $ECR_REPOSITORY:$IMAGE_TAG $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG |
| 98 | + docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG |
| 99 | +
|
| 100 | + - name: "Docker image successfully pushed with tag: ${{ env.IMAGE_TAG }}" |
| 101 | + run: | |
| 102 | + echo "The Docker image has been successfully pushed with the tag: ${{ env.IMAGE_TAG }}" |
0 commit comments