-
Notifications
You must be signed in to change notification settings - Fork 0
89 lines (89 loc) · 3.15 KB
/
deploy.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
# TODO pull environment into workflow input
name: Deploy
on:
workflow_run:
workflows: [Run tests]
types: [completed]
branches: [master]
jobs:
deploy:
if: ${{ github.event.workflow_run.conclusion == 'success' }}
runs-on: ubuntu-latest
environment: production
permissions:
id-token: write
contents: read
steps:
- uses: actions/checkout@v2
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
role-to-assume: ${{ secrets.AWS_ECR_ROLE }}
# not a secret, but you can only configure secrets with the Github Terraform provider,
# not environment variables.
aws-region: ${{ secrets.AWS_REGION }}
- name: Log in to AWS ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Docker meta
id: meta
uses: docker/metadata-action@v4
with:
# list of Docker images to use as base name for tags
images: |
${{ steps.login-ecr.outputs.registry }}/${{ secrets.AWS_ECR_REPOSITORY }}
# generate Docker tags based on the following events/attributes
tags: |
type=ref,event=branch
type=ref,event=pr
type=sha,format=long
type=raw,value=latest
# TODO split out bundle install into separate step that caches in GHA cache,
# currently it's not cached at all due to multi-stage builds
- name: Build and push
uses: docker/build-push-action@v4
with:
context: .
push: true
pull: true
tags: ${{ steps.meta.outputs.tags }}
build-args: |
PORT=3030
ENVIRONMENT=production
cache-from: |
type=gha
type=registry,ref=${{ steps.login-ecr.outputs.registry }}/${{ secrets.AWS_ECR_REPOSITORY }}:latest
cache-to: |
type=gha
secrets: |
SECRET_KEY_BASE=${{ secrets.SECRET_KEY_BASE }}
target: server-shrinkwrapped
# TODO split this into a separate dependent job
- name: Trigger deploy of ECS service
env:
CLUSTER: ${{ secrets.AWS_ECS_CLUSTER }}
SERVICE: ${{ secrets.AWS_ECS_SERVICE }}
run: |
aws ecs update-service --cluster $CLUSTER --service $SERVICE --force-new-deployment
# This step assumes that the database is accessible from the public internet.
- name: Run migrations
env:
DATABASE_USERNAME: ${{ secrets.DATABASE_USERNAME }}
DATABASE_PASSWORD: ${{ secrets.DATABASE_PASSWORD }}
DATABASE_HOST: ${{ secrets.DATABASE_HOST }}
DATABASE_PORT: ${{ secrets.DATABASE_PORT }}
DATABASE_NAME: ${{ secrets.DATABASE_NAME }}
ECR_IMAGE: ${{ steps.login-ecr.outputs.registry }}/${{ secrets.AWS_ECR_REPOSITORY }}:latest
SECRET_KEY_BASE: ${{ secrets.SECRET_KEY_BASE }}
run: |
docker run \
--rm \
-e RAILS_ENV=production \
-e SECRET_KEY_BASE=$SECRET_KEY_BASE \
--env-file <(printenv | grep DATABASE_) \
$ECR_IMAGE \
bundle exec rails db:migrate