-
Notifications
You must be signed in to change notification settings - Fork 3
161 lines (142 loc) · 5.27 KB
/
main.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: CI/CD
on:
- push
jobs:
test-backend:
name: Test backend
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/setup-node-using-nvm
- name: Install dependencies
run: yarn
- name: Build
run: npm run build
- name: Run tests with coverage report
run: yarn --cwd=packages/backend run test:coverage --forceExit
- name: List coverage directory
run: ls -la ./packages/backend
# TODO: Add later (does not work atm)
# - name: Coveralls
# uses: coverallsapp/github-action@master
# with:
# github-token: ${{ secrets.GITHUB_TOKEN }}
# path-to-lcov: ./packages/backend/coverage/lcov.info
# test-frontend:
# name: Test frontend
# runs-on: ubuntu-latest
# steps:
# - uses: actions/checkout@v1
# - uses: actions/setup-node@v1
# with:
# node-version: '12.x'
# - name: Install dependencies
# run: yarn --cwd=frontend install
# - name: Run tests
# run: yarn --cwd=frontend test
# Test that building local dev env using docker works
test-build:
name: Build for dev env
runs-on: ubuntu-latest
steps:
- run: echo "Ref ${{ github.ref }}"
- name: Checkout code
uses: actions/checkout@v4
- name: Build docker image
run: |
docker build -t diamonds2_base:latest -f .docker/dockerfiles/base .
docker compose -f docker-compose.yml build
# Build and push production images
build-prod:
name: Build for prod env
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main'
needs:
- test-backend
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Login to GitHub registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build docker image
run: |
docker build -t diamonds2_base:latest -f .docker/dockerfiles/base .
docker compose -f docker-compose.prod-build.yml build
- name: Extract branch name
shell: bash
run: |
echo "BRANCH_NAME=$(echo ${GITHUB_REF#refs/heads/} | sed 's/\//_/g')" >> $GITHUB_ENV
echo "${BRANCH_NAME}"
- name: Tag docker images
run: |
docker tag diamonds2-frontend ghcr.io/etimo/diamonds2/frontend:latest-${BRANCH_NAME}
docker tag diamonds2-frontend ghcr.io/etimo/diamonds2/frontend:${BRANCH_NAME}-${{ github.sha }}
docker tag diamonds2-backend ghcr.io/etimo/diamonds2/backend:latest-${BRANCH_NAME}
docker tag diamonds2-backend ghcr.io/etimo/diamonds2/backend:${BRANCH_NAME}-${{ github.sha }}
- name: Push docker images
run: |
docker push ghcr.io/etimo/diamonds2/frontend:latest-${BRANCH_NAME}
docker push ghcr.io/etimo/diamonds2/frontend:${BRANCH_NAME}-${{ github.sha }}
docker push ghcr.io/etimo/diamonds2/backend:latest-${BRANCH_NAME}
docker push ghcr.io/etimo/diamonds2/backend:${BRANCH_NAME}-${{ github.sha }}
deploy-prod:
name: Deploy to production
needs: build-prod
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main'
steps:
- uses: chrnorm/deployment-action@releases/v1
name: Create GitHub deployment
id: deployment
with:
token: "${{ github.token }}"
target_url: https://diamonds.etimo.se
environment: prod
initial_status: "in_progress"
- name: Update apps
uses: appleboy/ssh-action@v1
with:
host: ${{ secrets.DEPLOY_ETIMO_AWS_HOST }}
USERNAME: ${{ secrets.DEPLOY_ETIMO_AWS_USERNAME }}
PORT: 22
KEY: ${{ secrets.DEPLOY_ETIMO_AWS_KEY }}
script: |
cd diamonds2
git fetch
git checkout --progress --force ${{ github.sha }}
docker-compose down --remove-orphans
docker system prune -af
source ~/.bash_profile
echo ${{ secrets.GITHUB_TOKEN }} | docker login ghcr.io -u ${{ github.actor }} --password-stdin
DIAMONDS_DOCKER_TAG=main-${{ github.sha }} docker-compose -f docker-compose.prod-run.yml up -d
- name: Update autoscaler
uses: appleboy/ssh-action@v1
with:
host: ${{ secrets.DEPLOY_ETIMO_AWS_AUTOSCALER_HOST }}
USERNAME: ${{ secrets.DEPLOY_ETIMO_AWS_USERNAME }}
PORT: 22
KEY: ${{ secrets.DEPLOY_ETIMO_AWS_KEY }}
script: |
cd diamonds2
git fetch
git checkout --progress --force ${{ github.sha }}
- name: Update deployment status (success)
if: success()
uses: chrnorm/deployment-status@releases/v1
with:
token: "${{ github.token }}"
target_url: https://diamonds.etimo.se
state: "success"
deployment_id: ${{ steps.deployment.outputs.deployment_id }}
- name: Update deployment status (failure)
if: failure()
uses: chrnorm/deployment-status@releases/v1
with:
token: "${{ github.token }}"
target_url: https://diamonds.etimo.se
state: "failure"
deployment_id: ${{ steps.deployment.outputs.deployment_id }}