1
+ name : Build Image
2
+ permissions :
3
+ contents : write
4
+ id-token : write
5
+ on :
6
+ workflow_dispatch :
7
+ inputs :
8
+ environment :
9
+ description : ' Which account the ECR repository is in'
10
+ type : environment
11
+ fail_on_trivy_scan :
12
+ type : boolean
13
+ description : fail the build if vulnerabilities are found
14
+ required : true
15
+ default : false
16
+ jobs :
17
+ build :
18
+ name : Build Image
19
+ runs-on : ubuntu-latest
20
+ environment : ${{ inputs.environment }}
21
+ env :
22
+ ECR_REPOSITORY : ccdi-ccdc-backend
23
+ SLACK_WEBHOOK_URL : ${{ secrets.SLACK_WEBHOOK_URL }}
24
+ AWS_ROLE_TO_ASSUME : ${{ secrets.AWS_ROLE_TO_ASSUME }}
25
+ AWS_REGION : ${{ secrets.AWS_REGION }}
26
+
27
+ steps :
28
+
29
+ - name : Check out code
30
+ uses : actions/checkout@ee0669bd1cc54295c223e0bb666b733df41de1c5
31
+
32
+ - name : Set Image Tag
33
+ env :
34
+ BRANCH_NAME : ${{ github.head_ref || github.ref_name }}
35
+ run : |
36
+ # Get all tags for the repo and find the latest tag for the branch being built
37
+ git fetch --tags --force --quiet
38
+ tag=$(git tag -l $BRANCH_NAME* | tail -1)
39
+ if [ ! -z "$tag" ];
40
+ then
41
+ # Increment the build number if a tag is found
42
+ build_num=$(echo "${tag##*.}")
43
+ build_num=$((build_num+1))
44
+ echo "IMAGE_TAG=$GITHUB_REF_NAME.$build_num" >> $GITHUB_ENV
45
+ else
46
+ # If no tag is found create a new tag name
47
+ build_num=1
48
+ echo "IMAGE_TAG=$GITHUB_REF_NAME.$build_num" >> $GITHUB_ENV
49
+ fi
50
+ - name : Build
51
+ id : build-image
52
+ run : |
53
+ docker build -t $ECR_REPOSITORY:$IMAGE_TAG .
54
+
55
+ - name : Set Trivy exit code
56
+ run : |
57
+ if [[ ${{ inputs.fail_on_trivy_scan }} == true ]];
58
+ then
59
+ echo 'TRIVY_EXIT_CODE=1' >> $GITHUB_ENV
60
+ else
61
+ echo 'TRIVY_EXIT_CODE=0' >> $GITHUB_ENV
62
+ fi
63
+
64
+ - name : Run Trivy vulnerability scanner
65
+ id : trivy-scan
66
+ uses : aquasecurity/trivy-action@b2933f565dbc598b29947660e66259e3c7bc8561
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 : AWS OIDC Authentication
75
+ uses : aws-actions/configure-aws-credentials@e3dd6a429d7300a6a4c196c26e071d42e0343502
76
+ with :
77
+ role-to-assume : ${{ env.AWS_ROLE_TO_ASSUME }}
78
+ aws-region : ${{ env.AWS_REGION }}
79
+ role-session-name : ${{ github.actor }}
80
+
81
+ - name : Create Git tag for Image
82
+ run : |
83
+ git config user.name "GitHub Actions"
84
+ git config user.email "[email protected] "
85
+ git tag ${{ env.IMAGE_TAG }}
86
+ git push origin ${{ env.IMAGE_TAG }}
87
+
88
+ - name : Login to Amazon ECR
89
+ id : login-aws-ecr
90
+ uses : aws-actions/amazon-ecr-login@5a88a04c91d5c6f97aae0d9be790e64d9b1d47b7
91
+
92
+ - name : Push to Amazon 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 : Slack Notification
101
+ uses : act10ns/slack@87c73aef9f8838eb6feae81589a6b1487a4a9e08
102
+ with :
103
+ status : ${{ job.status }}
104
+ steps : ${{ toJson(steps) }}
105
+ if : always()
0 commit comments