-
Notifications
You must be signed in to change notification settings - Fork 79
79 lines (76 loc) · 2.78 KB
/
build_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
name: Publish Docker image and deploy it
on:
release:
types: [published]
push:
branches:
- main
- develop
env:
image_name: lucyparsons/openoversight
# env_name: 'prod' if ref_type is 'tag' indicating a release, otherwise 'staging'
env_name: ${{ github.ref_type == 'tag' && 'prod' || 'staging' }}
# docker_name: 'stable' if ref_type is 'tag', otherwise 'latest'
docker_name: ${{ github.ref_type == 'tag' && 'stable' || 'latest' }}
# release version if it is a release
release_version: ${{ github.ref_type == 'tag' && github.ref_name || 'latest'}}
python_version: "3.11"
jobs:
push_to_registry:
name: Push Docker image to GitHub Packages
runs-on: ubuntu-latest
permissions:
packages: write
contents: read
steps:
- name: Check out the repo
uses: actions/checkout@v3
- name: Log in to GitHub Docker Registry
uses: docker/login-action@v2
with:
registry: https://ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push image
uses: docker/build-push-action@v4
with:
file: dockerfiles/web/Dockerfile-prod
push: true
build-args: PYTHON_VERSION=${{ env.python_version }}
# Build the slimmer production target
target: production
tags: |
ghcr.io/${{ env.image_name }}:${{ github.sha }}
ghcr.io/${{ env.image_name }}:${{ env.docker_name }}
ghcr.io/${{ env.image_name }}:${{ env.docker_name }}-py${{ env.python_version }}
ghcr.io/${{ env.image_name }}:${{ env.release_version }}
deploy:
name: Deploy code to respective server
needs: push_to_registry
runs-on: ubuntu-latest
permissions:
packages: read
defaults:
run:
shell: bash
steps:
- name: Check out the repo
uses: actions/checkout@v3
- name: install fabric
run: pip install fabric2
- name: setup ssh connection
run: install -m 600 -D /dev/null ~/.ssh/id_ed25519
# choose SSH key depending on which environment we want to deploy to
- name: ssh key staging
if: ${{ env.env_name == 'staging' }}
run: echo "${{secrets.SSH_STAGING_PRIVATE_KEY}}" > ~/.ssh/id_ed25519
- name: ssh key prod
if: ${{ env.env_name == 'prod' }}
run: echo "${{secrets.SSH_PROD_PRIVATE_KEY}}" > ~/.ssh/id_ed25519
# Next three steps use fabric2's "invoke" command which runs tasks defined in tasks.py
- name: backup db
run: invoke backup ${{ env.env_name }}
- name: deploy
run: invoke deploy ${{ env.env_name }} ${{ github.actor }} ${{ secrets.GITHUB_TOKEN }} ${{ github.ref_name }}
- name: cleanup
run: invoke cleanup ${{ env.env_name }}