1
+ name : Build and Tag
2
+
3
+ on :
4
+ push :
5
+ branches : [main]
6
+ workflow_dispatch :
7
+
8
+ env :
9
+ REGISTRY : ghcr.io
10
+ FRONTEND_IMAGE_NAME : terramino-frontend
11
+ BACKEND_IMAGE_NAME : terramino-backend
12
+ HCP_CLIENT_ID : ${{ secrets.HCP_CLIENT_ID }}
13
+ HCP_CLIENT_SECRET : ${{ secrets.HCP_CLIENT_SECRET }}
14
+ HCP_PROJECT_ID : ${{ secrets.HCP_PROJECT_ID }}
15
+ HCP_ORGANIZATION_ID : ${{ secrets.HCP_ORGANIZATION_ID }}
16
+ AWS_REGION : ${{ secrets.AWS_REGION }}
17
+
18
+ jobs :
19
+ check-files-changed :
20
+ name : Check changed files
21
+ runs-on : ubuntu-latest
22
+
23
+ outputs :
24
+ build-packer : ${{ steps.check-packer.outputs.changed }}
25
+ build-container : ${{ steps.check-app.outputs.changed }}
26
+ update-terraform : ${{ steps.check-terraform.changed }}
27
+ manual-run : ${{ steps.manual-run.manual }}
28
+
29
+ steps :
30
+ - name : Checkout Repository
31
+ uses : actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac # v4.0.0
32
+ with :
33
+ fetch-depth : 2
34
+
35
+ - name : Check for Packer changes
36
+ id : check-packer
37
+ working-directory : .github/scripts
38
+ run : ./check_files_changed.sh "shared/** image.pkr.hcl" "${{ github.event_name }}" "${{ github.event.pull_request.base.sha }}" "${{ github.sha }}"
39
+
40
+ - name : Check for application changes
41
+ id : check-app
42
+ working-directory : .github/scripts
43
+ run : ./check_files_changed.sh "app/**" "${{ github.event_name }}" "${{ github.event.pull_request.base.sha }}" "${{ github.sha }}"
44
+
45
+ - name : Check for Terraform changes
46
+ id : check-terraform
47
+ working-directory : .github/scripts
48
+ run : ./check_files_changed.sh "*.tf" "${{ github.event_name }}" "${{ github.event.pull_request.base.sha }}" "${{ github.sha }}"
49
+
50
+ - name : Check for manual run
51
+ id : manual-run
52
+ # run: echo "manual=${{ github.event_name == 'workflow_dispatch' }}" >> $GITHUB_ENV
53
+ run : echo "manual=false" >> $GITHUB_ENV
54
+
55
+ build-packer :
56
+ name : Build AMI
57
+ runs-on : ubuntu-latest
58
+ needs : check-files-changed
59
+ if : ${{ needs.check-files-changed.outputs.build-packer == 'true' || needs.check-files-changed.outputs.manual-run == 'true' }}
60
+ steps :
61
+ - name : Checkout Repository
62
+ uses : actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac # v4.0.0
63
+
64
+ - name : Configure AWS Credentials
65
+ uses : aws-actions/configure-aws-credentials@8c3f20df09ac63af7b3ae3d7c91f105f857d8497 # v4.0.0
66
+ with :
67
+ aws-access-key-id : ${{ secrets.AWS_ACCESS_KEY_ID }}
68
+ aws-secret-access-key : ${{ secrets.AWS_SECRET_ACCESS_KEY }}
69
+ aws-region : ${{ secrets.AWS_REGION }}
70
+
71
+ - name : Setup Packer
72
+ uses : hashicorp/setup-packer@main
73
+ id : setup-packer
74
+
75
+ - name : Packer Init
76
+ run : packer init .
77
+
78
+ - name : Packer Build
79
+ run : PKR_VAR_region="$AWS_REGION" packer build .
80
+
81
+ - name : Create and set channel
82
+ working-directory : .github/scripts
83
+ run : ./create_channel_version.sh nomad-consul-vault ${{ github.sha }} production
84
+
85
+ build-application :
86
+ name : Build Terramino images
87
+ runs-on : ubuntu-latest
88
+ needs : check-files-changed
89
+ if : ${{ needs.check-files-changed.outputs.build-container == 'true' || needs.check-files-changed.outputs.manual-run == 'true' }}
90
+
91
+ permissions :
92
+ contents : write
93
+ packages : write
94
+
95
+ steps :
96
+ - name : Set Timestamp
97
+ id : timestamp
98
+ run : echo "TIMESTAMP=$(date +%s)" >> $GITHUB_ENV
99
+
100
+ - name : Set repository name
101
+ id : set-repo
102
+ run : echo "REPO=${GITHUB_REPOSITORY@L}" >> "${GITHUB_ENV}"
103
+
104
+ - name : Checkout repo
105
+ uses : actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac # v4.0.0
106
+
107
+ - name : Log in to GHCR
108
+ uses : docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1
109
+ with :
110
+ registry : ${{ env.REGISTRY }}
111
+ username : ${{ github.actor }}
112
+ password : ${{ secrets.GITHUB_TOKEN }}
113
+
114
+ # Frontend image build and push
115
+ - name : Build and push frontend image
116
+ id : frontend-push
117
+ uses : docker/build-push-action@f2a1d5e99d037542a71f64918e516c093c6f3fc4
118
+ with :
119
+ context : app/
120
+ file : app/Dockerfile.frontend
121
+ push : true
122
+ tags : ${{ env.REGISTRY }}/${{ env.REPO }}/${{ env.FRONTEND_IMAGE_NAME }}:${{ env.TIMESTAMP }}
123
+
124
+ - id : get-frontend-build-name
125
+ name : Save frontend image path to file
126
+ env :
127
+ GH_TOKEN : ${{ secrets.GITHUB_TOKEN }}
128
+ run : |
129
+ imageName=${{ fromJSON(steps.frontend-push.outputs.metadata)['image.name'] }}
130
+ echo "$imageName" > latest-frontend.version
131
+
132
+ # Backend image build and push
133
+ - name : Build and push backend image
134
+ id : backend-push
135
+ uses : docker/build-push-action@f2a1d5e99d037542a71f64918e516c093c6f3fc4
136
+ with :
137
+ context : app/
138
+ file : app/Dockerfile.backend
139
+ push : true
140
+ tags : ${{ env.REGISTRY }}/${{ env.REPO }}/${{ env.BACKEND_IMAGE_NAME }}:${{ env.TIMESTAMP }}
141
+
142
+ - id : get-backend-build-name
143
+ name : Save backend image path to file
144
+ run : |
145
+ imageName=${{ fromJSON(steps.backend-push.outputs.metadata)['image.name'] }}
146
+ echo "$imageName" > latest-backend.version
147
+
148
+ - name : Commit image files to repo
149
+ env :
150
+ GH_TOKEN : ${{ secrets.GITHUB_TOKEN }}
151
+ run : |
152
+ git config user.name "github-actions"
153
+ git config user.email "github-actions[bot]@users.noreply.github.com"
154
+ git add latest-frontend.version latest-backend.version
155
+ git commit -m "Updated frontend and backend image URIs."
156
+ git push
157
+
158
+ tag-release :
159
+ name : Create tag
160
+ runs-on : ubuntu-latest
161
+ needs : [check-files-changed, build-packer, build-application]
162
+ if : ${{ always() && (needs.build-packer.result == 'success' || needs.build-application.result == 'success' || needs.check-files-changed.outputs.update-terraform == 'true' || needs.check-files-changed.outputs.manual-run == 'true' ) }}
163
+
164
+ permissions :
165
+ contents : write
166
+ packages : write
167
+
168
+ steps :
169
+ - name : Checkout Repository
170
+ uses : actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac # v4.0.0
171
+
172
+ - name : Bump version and create tag
173
+ working-directory : .github/scripts
174
+ run : ./bump_and_create_tag.sh
175
+
0 commit comments