-
-
Notifications
You must be signed in to change notification settings - Fork 46
137 lines (118 loc) · 4.68 KB
/
build_and_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
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
name: Build and Deploy
on:
# Push includes PR merge
push:
branches:
- main
- staging
- development
paths:
# Workflow is triggered only if src changes
- "src/**"
# Allow manual trigger
workflow_dispatch:
env:
REGISTRY: ghcr.io
GIT_BRANCH: ${{ github.ref_name }}
jobs:
build-and-push-images:
runs-on: ubuntu-latest
environment:
name: ${{ github.ref_name }}
permissions:
contents: read
packages: write
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Log in to the Container registry
uses: docker/login-action@v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Persist env vars
run: echo "${{ secrets.DOTENV }}" >> $GITHUB_ENV
- name: Extract api version
id: extract_api_version
run: |
cd src/backend
echo "API_VERSION=$(python -c 'from app.__version__ import __version__; print(__version__)')" >> $GITHUB_ENV
- name: Extract frontend versions
id: extract_frontend_versions
run: |
cd src/frontend
echo "FRONTEND_MAIN_VERSION=$(jq -r '.version' main/package.json)" >> $GITHUB_ENV
echo "FRONTEND_MAP_VERSION=$(jq -r '.version' fmtm_openlayer_map/package.json)" >> $GITHUB_ENV
- name: Build and push backend
uses: docker/build-push-action@v4
with:
context: src/backend
target: prod
push: true
tags: "ghcr.io/hotosm/fmtm/backend:${{ env.API_VERSION }}-${{ github.ref_name }}"
build-args: |
APP_VERSION=${{ env.API_VERSION }}
- name: Build and push frontend main
uses: docker/build-push-action@v4
with:
context: src/frontend
file: src/frontend/prod.dockerfile
push: true
tags: "ghcr.io/hotosm/fmtm/frontend/main:${{ env.FRONTEND_MAIN_VERSION }}-${{ github.ref_name }}"
build-args: |
APP_NAME=main
APP_VERSION=${{ env.FRONTEND_MAIN_VERSION }}
API_URL=${{ env.URL_SCHEME }}://${{ env.API_URL }}
FRONTEND_MAIN_URL=${{ env.URL_SCHEME }}://${{ env.FRONTEND_MAIN_URL }}
FRONTEND_MAP_URL=${{ env.URL_SCHEME }}://${{ env.FRONTEND_MAP_URL }}
- name: Build and push frontend map
uses: docker/build-push-action@v4
with:
context: src/frontend
file: src/frontend/prod.dockerfile
push: true
tags: "ghcr.io/hotosm/fmtm/frontend/map:${{ env.FRONTEND_MAP_VERSION }}-${{ github.ref_name }}"
build-args: |
APP_NAME=fmtm_openlayer_map
APP_VERSION=${{ env.FRONTEND_MAP_VERSION }}
API_URL=${{ env.URL_SCHEME }}://${{ env.API_URL }}
FRONTEND_MAIN_URL=${{ env.URL_SCHEME }}://${{ env.FRONTEND_MAIN_URL }}
FRONTEND_MAP_URL=${{ env.URL_SCHEME }}://${{ env.FRONTEND_MAP_URL }}
deploy-containers:
runs-on: ubuntu-latest
needs: build-and-push-images
environment:
name: ${{ github.ref_name }}
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Extract api version
id: extract_api_version
run: |
cd src/backend
echo "API_VERSION=$(python -c 'from app.__version__ import __version__; print(__version__)')" >> $GITHUB_OUTPUT
- name: Extract frontend versions
id: extract_frontend_versions
run: |
cd src/frontend
echo "FRONTEND_MAIN_VERSION=$(jq -r '.version' main/package.json)" >> $GITHUB_OUTPUT
echo "FRONTEND_MAP_VERSION=$(jq -r '.version' fmtm_openlayer_map/package.json)" >> $GITHUB_OUTPUT
- name: Environment to .env
run: |
echo "${{ secrets.DOTENV }}" > .env
echo "API_VERSION=${{ steps.extract_api_version.outputs.API_VERSION }}" >> .env
echo "FRONTEND_MAIN_VERSION=${{ steps.extract_frontend_versions.outputs.FRONTEND_MAIN_VERSION }}" >> .env
echo "FRONTEND_MAP_VERSION=${{ steps.extract_frontend_versions.outputs.FRONTEND_MAP_VERSION }}" >> .env
- uses: webfactory/ssh-agent@v0.7.0
with:
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
- name: Disable Host key verification
# Hack to prevent "Host key verification failed". Should be replaced with a ssh-keyscan based solution
run: echo "StrictHostKeyChecking no" >> ~/.ssh/config
- name: Deploy
run: |
docker compose --file docker-compose.deploy.yml pull
docker compose --file docker-compose.deploy.yml up --detach
env:
DOCKER_HOST: ${{ vars.DOCKER_HOST }}