-
Notifications
You must be signed in to change notification settings - Fork 3
161 lines (137 loc) · 4.52 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
name: Build and deploy
on:
push:
branches:
- master
pull_request:
branches:
- master
types: [opened, reopened, synchronize, labeled]
permissions:
contents: write
deployments: write
packages: write
jobs:
build:
name: Build
runs-on: ubuntu-latest
env:
DOCKER_IMAGE: ghcr.io/dfe-digital/gias-api
outputs:
IMAGE_TAG: ${{ env.IMAGE_TAG }}
GIT_BRANCH: ${{ env.GIT_BRANCH }}
services:
postgres:
image: postgis/postgis
ports:
- 5432:5432
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Set up Ruby
uses: ruby/setup-ruby@ec02537da5712d66d4d50a0f33b7eb52773b5ed1
with:
ruby-version: '3.1'
- name: Install dependencies
run: bundle install
- name: Prepare SQLite database
run: make api_db pg_username="postgres:postgres"
- name: Set docker image tag (push)
if: github.event_name == 'push'
run: |
GIT_REF=${{ github.ref }}
echo "GIT_BRANCH=${GIT_REF##*/}" >> $GITHUB_ENV # GIT_BRANCH will be main for refs/heads/main
# add date as we will want to rebuild the same SHA with different daily data, and image tags
# must be distinct for them to require deployment
echo "IMAGE_TAG=${{ github.sha }}-$(date +%Y%m%d%H%M%S)" >> $GITHUB_ENV
- name: Set docker image tag (pull_request)
if: github.event_name == 'pull_request'
run: |
# This is the actual PR branch
GIT_REF=${{ github.head_ref }}
echo "GIT_BRANCH=${GIT_REF##*/}" >> $GITHUB_ENV
echo "IMAGE_TAG=${{ github.event.pull_request.head.sha }}" >> $GITHUB_ENV
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Login to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build Docker Image
uses: docker/build-push-action@v4
with:
context: .
tags: |
${{ env.DOCKER_IMAGE }}:${{ env.IMAGE_TAG }}
${{ env.DOCKER_IMAGE }}:${{ env.GIT_BRANCH }}
push: false
load: true
cache-to: type=inline
cache-from: |
type=registry,ref=${{ env.DOCKER_IMAGE }}:master
type=registry,ref=${{ env.DOCKER_IMAGE }}:${{ env.IMAGE_TAG }}
type=registry,ref=${{ env.DOCKER_IMAGE }}:${{ env.GIT_BRANCH }}
build-args: |
SHA=${{ env.IMAGE_TAG }}
- name: Push ${{ env.DOCKER_IMAGE }} images
run: docker image push --all-tags ${{ env.DOCKER_IMAGE }}
test:
name: Tests
needs: [build]
runs-on: ubuntu-latest
container:
image: ghcr.io/dfe-digital/gias-api:${{ needs.build.outputs.IMAGE_TAG }}
credentials:
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
env:
RACK_ENV: test
APP_ENV: test
defaults:
run:
working-directory: /app
steps:
- name: Run tests
run: bundle exec rspec
deploy:
name: Deploy ${{ matrix.environment }}
needs: [build, test]
# if: ${{ success() && github.ref == 'refs/heads/master' }}
runs-on: ubuntu-latest
environment: ${{ matrix.environment }}
strategy:
max-parallel: 1
matrix:
include:
- environment: development
hostname: gias-api-development.test.teacherservices.cloud
- environment: production
hostname: FAIL
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Load terraform
uses: hashicorp/setup-terraform@v1
with:
terraform_version: 1.5.1
terraform_wrapper: false
- name: Set Azure credentials
uses: DFE-Digital/github-actions/set-arm-environment-variables@master
with:
azure-credentials: ${{ secrets.AZURE_CREDENTIALS }}
- name: Terraform apply
run: make -f tsc.mk ci development terraform-apply
env:
DOCKER_IMAGE_TAG: ${{ needs.build.outputs.IMAGE_TAG }}
- name: Smoke test
run: curl -s https://${{ matrix.hostname }}/api/schools/100000 | grep -q "The Aldgate School"