generated from gzp79/shine-service-template
-
Notifications
You must be signed in to change notification settings - Fork 0
167 lines (144 loc) · 4.89 KB
/
ci.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
name: Master CI
on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]
# cancel outdated builds
concurrency:
group: ${{ github.workflow }}-${{ github.ref || github.run_id }}
cancel-in-progress: true
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- uses: actions/checkout@v4
with:
submodules: recursive
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: 18.x
- name: Prepare tests
run: |
npm ci
npm run lint
npm run mkcert
working-directory: ./integration-test
- name: Setup Docker Buildx (1)
run: |
docker network create shine
- name: Setup Docker Buildx (2)
uses: docker/setup-buildx-action@v3
with:
driver: docker-container
driver-opts: network=shine
- name: Docker meta
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: type=sha
- name: Prepare MockBox
id: mockbox
run: |
docker compose -f docker-compose.yml -p shine-identity up -d
echo "pg_host=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' shine-identity-postgres-1)" >> $GITHUB_OUTPUT
echo "redis_host=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' shine-identity-redis-1)" >> $GITHUB_OUTPUT
working-directory: ./service
- name: Docker build
uses: docker/build-push-action@v6
with:
context: service
target: test
push: false
load: true
add-hosts: |
postgres.mockbox.com:${{ steps.mockbox.outputs.pg_host }}
redis.mockbox.com:${{ steps.mockbox.outputs.redis_host }}
cache-from: type=gha,mode=max
cache-to: type=gha,mode=max
tags: ${{ env.IMAGE_NAME }}:test
- name: Add hosts to /etc/hosts
run: |
sudo echo "127.0.0.1 sandbox.com" | sudo tee -a /etc/hosts
sudo echo "127.0.0.1 web.sandbox.com" | sudo tee -a /etc/hosts
sudo echo "127.0.0.1 cloud.sandbox.com" | sudo tee -a /etc/hosts
sudo echo "127.0.0.1 mockbox.com" | sudo tee -a /etc/hosts
sudo echo "127.0.0.1 postgres.mockbox.com" | sudo tee -a /etc/hosts
sudo echo "127.0.0.1 redis.mockbox.com" | sudo tee -a /etc/hosts
- name: Integration test
run: |
echo "::group::Starting service in the test environment"
docker compose -f ../service/docker-compose.yml -p shine-identity --profile test up -d
echo "::endgroup::"
echo "::group::Running test"
npm run test
npm run test:report
echo "::endgroup::"
working-directory: ./integration-test
- name: Publish test results
if: github.ref == 'refs/heads/master' || failure()
uses: actions/upload-artifact@v4
with:
name: test-reports
path: |
./integration-test/reports/
- name: Log in to the Container registry
if: github.ref == 'refs/heads/master'
uses: docker/login-action@v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build production
if: github.ref == 'refs/heads/master'
uses: docker/build-push-action@v4
with:
context: service
target: prod
push: true
# these setting are added to allow the reuse of the layer from the build step
# without host parameters it would trigger a new build
add-hosts: |
postgres.mockbox.com:${{ steps.mockbox.outputs.pg_host }}
redis.mockbox.com:${{ steps.mockbox.outputs.redis_host }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
- name: Create release artifacts
if: github.ref == 'refs/heads/master'
run: |
echo "[build]" >> fly.toml
echo " image = \"${{ steps.meta.outputs.tags }}\"" >> fly.toml
cp fly.toml release/
working-directory: ./service
- name: Publish release artifacts
if: github.ref == 'refs/heads/master'
uses: actions/upload-artifact@v4
with:
name: release-artifacts
path: |
./service/release/
deploy:
needs: build
if: github.ref == 'refs/heads/master'
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- name: Init fly
uses: superfly/flyctl-actions/setup-flyctl@v1
- name: Download release artifacts
uses: actions/download-artifact@v4
with:
name: release-artifacts
- name: Deploy to fly
run: flyctl deploy
env:
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}