-
Notifications
You must be signed in to change notification settings - Fork 262
195 lines (172 loc) · 7.59 KB
/
build_docker.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
name: "Build Docker Image"
on:
push:
branches:
- "master"
- "dev"
workflow_dispatch:
inputs:
canarytokens-docker-branch:
description: "Branch of the canarytokens-docker repo to pull for build. Defaults to master"
required: false
default: "master"
jobs:
build-and-commit-frontend-dist:
runs-on: ubuntu-20.04
env:
NODE_VERSION: '18'
permissions:
id-token: write
contents: write
steps:
- uses: actions/checkout@v3
- name: Record author
run: |
echo "# Owners" >> $GITHUB_STEP_SUMMARY
echo "Author: ${{ github.event.head_commit.author.name }} (${{ github.event.head_commit.author.email }})" >> $GITHUB_STEP_SUMMARY
echo "Committer: ${{ github.event.head_commit.committer.name }} (${{ github.event.head_commit.committer.email }})" >> $GITHUB_STEP_SUMMARY
- name: Restore Frontend Dist from Cache
uses: actions/cache@v3
id: cache-frontend-dist
with:
path: frontend_vue/dist
key: frontend-dist-${{ hashFiles('frontend_vue/**') }}
- uses: actions/setup-node@v3.5.1
if: steps.cache-frontend-dist.outputs.cache-hit != 'true'
with:
node-version: ${{ env.NODE_VERSION }}
- name: Install node modules
if: steps.cache-frontend-dist.outputs.cache-hit != 'true'
working-directory: frontend_vue
run: |
npm config set "@fortawesome:registry" https://npm.fontawesome.com/
npm config set '//npm.fontawesome.com/:_authToken' "${{ secrets.FONTAWESOME_NPM_AUTH_TOKEN }}"
npm ci
- name: Include git commit SHA and Google Maps Key for Frontend Development release
if: steps.cache-frontend-dist.outputs.cache-hit != 'true' && github.ref == 'refs/heads/dev'
working-directory: frontend_vue
run: |
touch .env
echo VITE_GITHUB_SHA=${GITHUB_SHA} >> .env
echo VITE_GOOGLE_MAPS_API_KEY="${{ secrets.GOOGLE_MAPS_API_KEY_DEV }}" >> .env
cat .env
- name: Include git Google Maps Key for Frontend Production release
if: steps.cache-frontend-dist.outputs.cache-hit != 'true' && github.ref == 'refs/heads/master'
working-directory: frontend_vue
run: |
touch .env
echo VITE_GOOGLE_MAPS_API_KEY="${{ secrets.GOOGLE_MAPS_API_KEY_PROD }}" >> .env
cat .env
- name: Build Frontend Dist
if: steps.cache-frontend-dist.outputs.cache-hit != 'true'
working-directory: frontend_vue
env:
NODE_OPTIONS: "--max_old_space_size=4096"
run: |
rm -rf dist
npm run build
- name: Commit report
if: steps.cache-frontend-dist.outputs.cache-hit != 'true'
run: |
git config --global user.name "Frontend Github Action"
git config --global user.email "max@thinkst.com"
git add -f frontend_vue/dist
git commit -am "Add Frontend Dist"
git push
build:
needs: build-and-commit-frontend-dist
runs-on: ubuntu-20.04
env:
COPYFILE_DISABLE: 1
steps:
- name: set Canarytokens Docker repo branch
run: |
if [ -z "${{ github.event.inputs.canarytokens-docker-branch }}" ]; then
BRANCH="master"
else
BRANCH="${{ github.event.inputs.canarytokens-docker-branch }}"
fi
echo "CANARYTOKENS_DOCKER_BRANCH=$BRANCH" >> "$GITHUB_ENV"
- uses: actions/checkout@v4
with:
repository: thinkst/canarytokens-docker
path: canarytokens-docker
ref: ${{ env.CANARYTOKENS_DOCKER_BRANCH }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Login to DockerHub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Fixup the build branch
run: |
sed -i'' "s/master/${GITHUB_REF##*/}/g" /home/runner/work/canarytokens/canarytokens/canarytokens-docker/canarytokens/Dockerfile
sed -i'' "s/_COMMIT_HASH_/${GITHUB_SHA##*/}/g" /home/runner/work/canarytokens/canarytokens/canarytokens-docker/canarytokens/Dockerfile
echo "Building for ${GITHUB_REF##*/}"
cat /home/runner/work/canarytokens/canarytokens/canarytokens-docker/canarytokens/Dockerfile
- name: Docker meta
id: meta
uses: docker/metadata-action@v3
with:
images: |
thinkst/canarytokens
tags: |
type=raw,value=latest,enable=${{ endsWith(github.ref, github.event.repository.default_branch) }}
type=schedule
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
type=sha
- name: Build and push
id: docker_build
uses: docker/build-push-action@v4
with:
context: /home/runner/work/canarytokens/canarytokens/canarytokens-docker/canarytokens
push: true
platforms: linux/amd64,linux/arm64
tags: ${{ steps.meta.outputs.tags }}
cache-from: type=gha
cache-to: type=gha,mode=max
build-args: |
CACHE_BUSTER_COMMIT=${{ github.sha }}
outputs:
canarytokens-docker-branch: ${{ env.CANARYTOKENS_DOCKER_BRANCH }}
dev-deploy:
if: github.repository == 'thinkst/canarytokens' && github.ref == 'refs/heads/dev'
runs-on: [self-hosted, dev]
needs: build
steps:
- name: Deploy to dev machine
run: |
cd /home/ubuntu/canarytokens-scripts/
./canarytokensdb_s3backup.sh
cd /home/ubuntu/canarytokens-docker
git stash && git checkout ${{ needs.build.outputs.canarytokens-docker-branch }} && git pull
sed "s/thinkst\/canarytokens$/thinkst\/canarytokens:${GITHUB_REF##*/}/g" common-services.yml > common-services-${GITHUB_REF##*/}.yml
sed "s/file: common-services.yml/file: common-services-${GITHUB_REF##*/}.yml/g" docker-compose-letsencrypt.yml > docker-compose-letsencrypt-${GITHUB_REF##*/}.yml
sed -i'' "s/CANARY_DEV_BUILD_ID=.*/CANARY_DEV_BUILD_ID=${GITHUB_SHA:0:8}/" frontend.env
sudo docker pull thinkst/canarytokens:${GITHUB_REF##*/}
sudo docker compose -f docker-compose-letsencrypt-${GITHUB_REF##*/}.yml pull
sudo docker compose -f docker-compose-letsencrypt-${GITHUB_REF##*/}.yml up -d
sudo docker system prune -f -a
staging-deploy:
if: github.repository == 'thinkst/canarytokens' && github.ref == 'refs/heads/master'
runs-on: [self-hosted, staging]
needs: build
steps:
- name: Deploy to staging machine
run: |
cd /home/ubuntu/canarytokens-scripts/
./canarytokensdb_s3backup.sh
cd /home/ubuntu/canarytokens-docker
git stash && git checkout ${{ needs.build.outputs.canarytokens-docker-branch }} && git pull
sed "s/thinkst\/canarytokens$/thinkst\/canarytokens:${GITHUB_REF##*/}/g" common-services.yml > common-services-${GITHUB_REF##*/}.yml
sed "s/file: common-services.yml/file: common-services-${GITHUB_REF##*/}.yml/g" docker-compose-aws-logging-letsencrypt.yml > docker-compose-aws-logging-letsencrypt-${GITHUB_REF##*/}.yml
sed -i'' "s/CANARY_DEV_BUILD_ID=.*/CANARY_DEV_BUILD_ID=${GITHUB_SHA:0:8}/" frontend.env
sudo docker pull thinkst/canarytokens:${GITHUB_REF##*/}
sudo docker compose -f docker-compose-aws-logging-letsencrypt-${GITHUB_REF##*/}.yml pull
sudo docker compose -f docker-compose-aws-logging-letsencrypt-${GITHUB_REF##*/}.yml up -d
sudo docker system prune -f -a