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