Skip to content

Commit 3e0dd9c

Browse files
committed
ci: add test
1 parent cb0ac29 commit 3e0dd9c

File tree

4 files changed

+511
-3
lines changed

4 files changed

+511
-3
lines changed

.github/workflows/build_and_deploy.yml

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,26 @@ on:
1616
workflow_dispatch:
1717

1818
jobs:
19+
backend-test:
20+
uses: hotosm/gh-workflows/.github/workflows/[email protected]
21+
with:
22+
image_name: ghcr.io/${{ github.repository }}/backend
23+
pre_command: docker compose up -d traefik
24+
compose_service: tm-backend
25+
build_target: prod
26+
compose_command: |
27+
pip install flake8
28+
flake8 manage.py backend tests migrations
29+
pip install 'black==23.12.1'
30+
black --check manage.py backend tests migrations
31+
tag_override: ci-${{ github.ref_name }}
32+
coverage: true
33+
secrets: inherit
34+
1935
backend-build:
2036
uses: hotosm/gh-workflows/.github/workflows/[email protected]
37+
needs:
38+
- backend-test
2139
with:
2240
context: .
2341
build_target: prod
@@ -26,9 +44,24 @@ jobs:
2644
scan_image: false
2745
secrets: inherit
2846

47+
frontend-test:
48+
uses: naxa-developers/tasking-manager/.github/workflows/frontend-test.yml@ci-gh-workflows
49+
secrets: inherit
50+
with:
51+
node-version: 16.x
52+
context: ./frontend
53+
cache-key-file: ./frontend/yarn.lock
54+
package-manager: yarn
55+
test_frontend_build: false
56+
test_frontend_command: |
57+
CI=true yarn test -w 1
58+
CI=true GENERATE_SOURCEMAP=false yarn build
59+
2960
frontend-build:
3061
uses: naxa-developers/tasking-manager/.github/workflows/frontend-build.yml@ci-gh-workflows
3162
secrets: inherit
63+
needs:
64+
- frontend-test
3265
with:
3366
node-version: 16.x
3467
context: ./frontend
@@ -60,7 +93,7 @@ jobs:
6093
- backend-build
6194
uses: naxa-developers/tasking-manager/.github/workflows/remote_deploy_compose.yml@ci-gh-workflows
6295
with:
63-
docker_compose_file: docker-compose.vm.yml
96+
docker_compose_file: docker-compose.yml
6497
environment: ${{ github.ref_name }}
6598
example_env_file_path: example.env
6699
env_file_path: tasking-manager.env

.github/workflows/frontend-build.yml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
name: Node build
1+
name: Node Frontend build
22

33
on:
44
workflow_call:
55
inputs:
6+
runner-class:
7+
description: "Github Runner class to use"
8+
required: false
9+
type: string
10+
default: "ubuntu-latest"
611
node-version:
712
description: "Node version to use."
813
required: false
@@ -56,7 +61,7 @@ on:
5661

5762
jobs:
5863
node-build:
59-
runs-on: ubuntu-latest
64+
runs-on: ${{ inputs.runner-class }}
6065

6166
environment:
6267
name: ${{ github.ref_name }}

.github/workflows/frontend-test.yml

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
name: Frontend Test
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
runner-class:
7+
description: "Github Runner class to use"
8+
required: false
9+
type: string
10+
default: "ubuntu-latest"
11+
node-version:
12+
description: "Node version to use."
13+
required: false
14+
type: string
15+
default: "18.x"
16+
node-version-file:
17+
description: "Node version file to use. node-version overrides this parameter."
18+
required: false
19+
type: string
20+
default: ""
21+
context:
22+
description: "Root directory to start the build from."
23+
required: false
24+
type: string
25+
default: "."
26+
cache:
27+
description: "Use node modules installation caching. Default true."
28+
required: false
29+
type: boolean
30+
default: true
31+
cache-key-file:
32+
description: "Key file for cache."
33+
required: false
34+
type: string
35+
default: "${{ inputs.context }}/package.json"
36+
package-manager:
37+
description: "Package manager to use. Supports [npm, yarn]"
38+
required: false
39+
type: string
40+
default: "npm"
41+
build-script-name:
42+
description: "Build script name in package.json"
43+
required: false
44+
type: string
45+
default: "build"
46+
test_frontend_command:
47+
description: "Testing command to run"
48+
required: true
49+
type: string
50+
test_frontend_build:
51+
description: "Perform a build test"
52+
required: false
53+
type: boolean
54+
default: true
55+
56+
jobs:
57+
node-build:
58+
runs-on: ${{ inputs.runner-class }}
59+
60+
environment:
61+
name: ${{ github.ref_name }}
62+
63+
outputs:
64+
artifact-name: ${{ steps.get_artifact_name.outputs.artifact_name }}
65+
66+
permissions:
67+
contents: read
68+
packages: write
69+
70+
steps:
71+
- name: Checkout repository
72+
uses: actions/checkout@v4
73+
74+
- id: node_setup
75+
name: Install node
76+
uses: actions/setup-node@v4
77+
#reference: https://github.com/actions/setup-node
78+
with:
79+
node-version: ${{ inputs.node-version }}
80+
node-version-file: ${{ inputs.node-version-file && inputs.node-version-file || '' }}
81+
82+
- name: Cache Node packages
83+
uses: actions/cache@v3
84+
env:
85+
cache_name: node-${{ inputs.node-version || hashFiles(inputs.node-version-file) }}-${{ inputs.package-manager }}-${{ hashFiles(inputs.cache-key-file) }}
86+
with:
87+
key: ${{ runner.os }}-build-${{ env.cache_name }}
88+
path: |
89+
~/.npm
90+
restore-keys: |
91+
${{ runner.os }}-build-${{ env.cache_name }}
92+
93+
- id: node_packages_install
94+
name: Install node pacakges
95+
working-directory: ${{ inputs.context }}
96+
run: |
97+
case "${{ inputs.package-manager }}" in
98+
yarn)
99+
yarn
100+
;;
101+
npm)
102+
npm i
103+
;;
104+
esac
105+
106+
- id: vars_and_secrets
107+
name: Vars and Secrets to Env file
108+
env:
109+
VARS_CONTEXT: ${{ toJson(vars) }}
110+
SECRETS_CONTEXT: ${{ toJson(secrets) }}
111+
shell: bash
112+
run: |
113+
parsed_vars=$(jq -n --argjson VARS_CONTEXT "$VARS_CONTEXT" --argjson SECRETS_CONTEXT "$SECRETS_CONTEXT" "$VARS_CONTEXT+$SECRETS_CONTEXT")
114+
to_envs() { jq -r "to_entries[] | \"\(.key)=\\\"\(.value)\\\"\n\""; }
115+
echo "$parsed_vars" | to_envs > ${{ inputs.context }}/.env
116+
117+
- id: test_frontend
118+
name: Test Frontend
119+
working-directory: ${{ inputs.context }}
120+
run: |
121+
${{ inputs.test_frontend_command }}
122+
123+
- id: build_frontend
124+
name: Build Frontend
125+
if: ${{ inputs.test_frontend_build }}
126+
working-directory: ${{ inputs.context }}
127+
run: |
128+
case "${{ inputs.package-manager }}" in
129+
yarn)
130+
yarn ${{ inputs.build-script-name }}
131+
;;
132+
npm)
133+
npm run ${{ inputs.build-script-name }}
134+
;;
135+
esac
136+

0 commit comments

Comments
 (0)