Skip to content

Commit 39a1bf8

Browse files
add reusable workflow
1 parent 0cf2672 commit 39a1bf8

File tree

3 files changed

+72
-50
lines changed

3 files changed

+72
-50
lines changed

.github/workflows/deployment.yml

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
name: Deployment flow
2+
on:
3+
workflow_call:
4+
inputs:
5+
environment:
6+
type: string
7+
required: true
8+
runner:
9+
type: string
10+
required: true
11+
env_file:
12+
type: string
13+
required: true
14+
jobs:
15+
checkout:
16+
runs-on: "self-hosted,${{inputs.runner}}"
17+
environment: ${{inputs.environment}}
18+
steps:
19+
- name: Get code
20+
uses: actions/checkout@v4
21+
- name: Cache dependencies
22+
id: cache
23+
uses: actions/cache@v4
24+
with:
25+
path: node_modules
26+
key: deps-node-modules-${{ hashFiles('**/package-lock.json') }}
27+
- name: Install dependencies
28+
if: steps.cache.outputs.cache-hit != 'true'
29+
run: npm ci
30+
create_env:
31+
needs: checkout
32+
runs-on: "self-hosted,${{inputs.runner}}"
33+
environment: ${{inputs.environment}}
34+
steps:
35+
- name: Create .env file
36+
run: |
37+
echo "DB_HOST=${{ secrets.DB_HOST }}" >> ${{inputs.env_file}}
38+
echo "DB_PORT=${{ secrets.DB_PORT }}" >> ${{inputs.env_file}}
39+
echo "DB_USER=${{ secrets.DB_USER }}" >> ${{inputs.env_file}}
40+
echo "DB_PASSWORD=${{ secrets.DB_PASSWORD }}" >> ${{inputs.env_file}}
41+
echo "DB_NAME=${{ secrets.DB_NAME }}" >> ${{inputs.env_file}}
42+
echo "DB_DROP_SCHEMA=${{ secrets.DB_DROP_SCHEMA }}" >> ${{inputs.env_file}}
43+
echo "AUTH_SECRET=${{ secrets.AUTH_SECRET }}" >> ${{inputs.env_file}}
44+
echo "JWT_EXPIRES_IN=${{ secrets.JWT_EXPIRES_IN }}" >> ${{inputs.env_file}}
45+
echo "PORT=${{ secrets.PORT }}" >> ${{inputs.env_file}}
46+
test:
47+
runs-on: "self-hosted,${{inputs.runner}}"
48+
needs: create_env
49+
steps:
50+
- name: Run tests
51+
run: npm run test
52+
build:
53+
needs: [create_env, test]
54+
runs-on: "self-hosted,${{inputs.runner}}"
55+
steps:
56+
- name: Build website
57+
id: build-website
58+
run: npm run build
59+
deploy:
60+
needs: build
61+
runs-on: "self-hosted,${{inputs.runner}}"
62+
steps:
63+
- name: Run deployment
64+
run: NODE_ENV=${{inputs.environment}} pm2 restart dist/main.js --name=events-api

.github/workflows/dev.yml

Lines changed: 7 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -5,53 +5,10 @@ on:
55
- dev
66
workflow_dispatch:
77
jobs:
8-
checkout:
9-
runs-on: [self-hosted, dev]
10-
environment: dev
11-
steps:
12-
- name: Get code
13-
uses: actions/checkout@v4
14-
- name: Cache dependencies
15-
id: cache
16-
uses: actions/cache@v4
17-
with:
18-
path: node_modules
19-
key: deps-node-modules-${{ hashFiles('**/package-lock.json') }}
20-
- name: Install dependencies
21-
if: steps.cache.outputs.cache-hit != 'true'
22-
run: npm ci
23-
create_env:
24-
needs: checkout
25-
runs-on: [self-hosted, dev]
26-
environment: dev
27-
steps:
28-
- name: Create .env file
29-
run: |
30-
echo "DB_HOST=${{ secrets.DB_HOST }}" >> dev.env
31-
echo "DB_PORT=${{ secrets.DB_PORT }}" >> dev.env
32-
echo "DB_USER=${{ secrets.DB_USER }}" >> dev.env
33-
echo "DB_PASSWORD=${{ secrets.DB_PASSWORD }}" >> dev.env
34-
echo "DB_NAME=${{ secrets.DB_NAME }}" >> dev.env
35-
echo "DB_DROP_SCHEMA=${{ secrets.DB_DROP_SCHEMA }}" >> dev.env
36-
echo "AUTH_SECRET=${{ secrets.AUTH_SECRET }}" >> dev.env
37-
echo "JWT_EXPIRES_IN=${{ secrets.JWT_EXPIRES_IN }}" >> dev.env
38-
echo "PORT=${{ secrets.PORT }}" >> dev.env
39-
test:
40-
runs-on: [self-hosted, dev]
41-
needs: create_env
42-
steps:
43-
- name: Run tests
44-
run: npm run test
45-
build:
46-
needs: [create_env, test]
47-
runs-on: [self-hosted, dev]
48-
steps:
49-
- name: Build website
50-
id: build-website
51-
run: npm run build
52-
deploy:
53-
needs: build
54-
runs-on: [self-hosted, dev]
55-
steps:
56-
- name: Run deployment
57-
run: NODE_ENV=dev pm2 restart dist/main.js --name=events-api
8+
run-deployment:
9+
uses: ./.github/workflows/deployment.yml
10+
with:
11+
environment: "dev"
12+
runner: "dev"
13+
env_file: "dev.env"
14+
secrets: inherit

.github/workflows/production.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ jobs:
3434
echo "PORT=${{ vars.PORT }}" >> production.env
3535
test:
3636
runs-on: [self-hosted, prod]
37+
needs: checkout
3738
steps:
3839
- name: Run tests
3940
run: npm run test

0 commit comments

Comments
 (0)