forked from hotosm/tasking-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #120 from naxa-developers/ci-to-gh-actions
CI workflow using github Actions
- Loading branch information
Showing
9 changed files
with
913 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
name: Build and Deploy | ||
|
||
on: | ||
# Push includes PR merge | ||
push: | ||
branches: | ||
- main | ||
- staging | ||
- develop | ||
- ci-gh-workflows | ||
paths: | ||
# Workflow is triggered only if src changes | ||
- backend/** | ||
- frontend/** | ||
# Allow manual trigger | ||
workflow_dispatch: | ||
|
||
jobs: | ||
backend-test: | ||
uses: naxa-developers/tasking-manager/.github/workflows/test_compose.yml@ci-gh-workflows | ||
with: | ||
image_name: ghcr.io/${{ github.repository }}/backend | ||
pre_command: | | ||
docker compose --file docker-compose.yml up -d | ||
sleep 15 | ||
docker compose --file docker-compose.yml logs tm-backend | ||
compose_service: tm-backend | ||
build_target: prod | ||
compose_command: curl -i http://localhost:5000/api/v2/ | ||
tag_override: ci-${{ github.ref_name }} | ||
# coverage: true | ||
example_env_file_path: example.env | ||
env_file_path: tasking-manager.env | ||
secrets: inherit | ||
|
||
backend-build: | ||
uses: hotosm/gh-workflows/.github/workflows/[email protected] | ||
with: | ||
context: . | ||
build_target: prod | ||
image_name: ghcr.io/${{ github.repository }}/backend | ||
dockerfile: Dockerfile | ||
scan_image: false | ||
secrets: inherit | ||
|
||
frontend-test: | ||
uses: naxa-developers/tasking-manager/.github/workflows/frontend-test.yml@ci-gh-workflows | ||
secrets: inherit | ||
with: | ||
node-version: 16.x | ||
context: ./frontend | ||
cache-key-file: ./frontend/yarn.lock | ||
package-manager: yarn | ||
test_frontend_command: | | ||
CI=true yarn test -w 1 | ||
# test_frontend_build: false | ||
# build_test_frontend_command: | | ||
# CI=true GENERATE_SOURCEMAP=false yarn build | ||
|
||
frontend-build: | ||
uses: naxa-developers/tasking-manager/.github/workflows/frontend-build.yml@ci-gh-workflows | ||
secrets: inherit | ||
with: | ||
node-version: 16.x | ||
context: ./frontend | ||
cache-key-file: ./frontend/yarn.lock | ||
package-manager: yarn | ||
build-dist-folder-path: ./frontend/build | ||
|
||
frontend-deploy: | ||
runs-on: ubuntu-latest | ||
needs: | ||
- frontend-test | ||
- frontend-build | ||
name: Deploy Frontend Static Files | ||
steps: | ||
- name: Download build artifacts | ||
uses: actions/download-artifact@v4 | ||
with: | ||
name: ${{ needs.frontend-build.outputs.artifact-name }} | ||
path: ./build | ||
|
||
- name: Debug check files | ||
run: | | ||
ls -alh | ||
ls -alh build | ||
backend_deploy_to_vm: | ||
name: Deploy Backend to VM | ||
needs: | ||
- backend-test | ||
- backend-build | ||
uses: naxa-developers/tasking-manager/.github/workflows/remote_deploy_compose.yml@ci-gh-workflows | ||
with: | ||
docker_compose_file: docker-compose.yml | ||
environment: ${{ github.ref_name }} | ||
example_env_file_path: example.env | ||
env_file_path: tasking-manager.env | ||
secrets: inherit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
name: Node Frontend build | ||
|
||
on: | ||
workflow_call: | ||
inputs: | ||
runner-class: | ||
description: "Github Runner class to use" | ||
required: false | ||
type: string | ||
default: "ubuntu-latest" | ||
node-version: | ||
description: "Node version to use." | ||
required: false | ||
type: string | ||
default: "18.x" | ||
node-version-file: | ||
description: "Node version file to use. node-version overrides this parameter." | ||
required: false | ||
type: string | ||
default: "" | ||
context: | ||
description: "Root directory to start the build from." | ||
required: false | ||
type: string | ||
default: "." | ||
cache: | ||
description: "Use node modules installation caching. Default true." | ||
required: false | ||
type: boolean | ||
default: true | ||
cache-key-file: | ||
description: "Key file for cache." | ||
required: false | ||
type: string | ||
default: "${{ inputs.context }}/package.json" | ||
package-manager: | ||
description: "Package manager to use. Supports [npm, yarn]" | ||
required: false | ||
type: string | ||
default: "npm" | ||
build-script-name: | ||
description: "Build script name in package.json" | ||
required: false | ||
type: string | ||
default: "build" | ||
upload-artifacts: | ||
description: "Upload artifacts to Github" | ||
required: false | ||
type: boolean | ||
default: true | ||
build-dist-folder-path: | ||
description: "Path to folder that stores build files" | ||
required: false | ||
type: string | ||
default: "${{ inputs.context }}/dist" | ||
|
||
outputs: | ||
artifact-name: | ||
description: "Node built artifact" | ||
value: ${{ jobs.node-build.outputs.artifact-name }} | ||
|
||
jobs: | ||
node-build: | ||
runs-on: ${{ inputs.runner-class }} | ||
|
||
environment: | ||
name: ${{ github.ref_name }} | ||
|
||
outputs: | ||
artifact-name: ${{ steps.get_artifact_name.outputs.artifact_name }} | ||
|
||
permissions: | ||
contents: read | ||
packages: write | ||
|
||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@v4 | ||
|
||
- id: node_setup | ||
name: Install node | ||
uses: actions/setup-node@v4 | ||
#reference: https://github.com/actions/setup-node | ||
with: | ||
node-version: ${{ inputs.node-version }} | ||
node-version-file: ${{ inputs.node-version-file && inputs.node-version-file || '' }} | ||
|
||
- name: Cache Node packages | ||
uses: actions/cache@v3 | ||
env: | ||
cache_name: node-${{ inputs.node-version || hashFiles(inputs.node-version-file) }}-${{ inputs.package-manager }}-${{ hashFiles(inputs.cache-key-file) }} | ||
with: | ||
key: ${{ runner.os }}-build-${{ env.cache_name }} | ||
path: | | ||
~/.npm | ||
restore-keys: | | ||
${{ runner.os }}-build-${{ env.cache_name }} | ||
- id: node_packages_install | ||
name: Install node pacakges | ||
working-directory: ${{ inputs.context }} | ||
run: | | ||
case "${{ inputs.package-manager }}" in | ||
yarn) | ||
yarn | ||
;; | ||
npm) | ||
npm i | ||
;; | ||
esac | ||
- id: vars_and_secrets | ||
name: Vars and Secrets to Env file | ||
env: | ||
VARS_CONTEXT: ${{ toJson(vars) }} | ||
SECRETS_CONTEXT: ${{ toJson(secrets) }} | ||
shell: bash | ||
run: | | ||
parsed_vars=$(jq -n --argjson VARS_CONTEXT "$VARS_CONTEXT" --argjson SECRETS_CONTEXT "$SECRETS_CONTEXT" "$VARS_CONTEXT+$SECRETS_CONTEXT") | ||
to_envs() { jq -r "to_entries[] | \"\(.key)=\\\"\(.value)\\\"\n\""; } | ||
echo "$parsed_vars" | to_envs > ${{ inputs.context }}/.env | ||
- id: build_frontend | ||
name: Build Frontend | ||
working-directory: ${{ inputs.context }} | ||
run: | | ||
case "${{ inputs.package-manager }}" in | ||
yarn) | ||
yarn ${{ inputs.build-script-name }} | ||
;; | ||
npm) | ||
npm run ${{ inputs.build-script-name }} | ||
;; | ||
esac | ||
- id: upload_build_artifacts | ||
name: Upload build files as build artifacts | ||
uses: actions/upload-artifact@v4 | ||
if: ${{ inputs.upload-artifacts }} | ||
with: | ||
name: ${{ github.repository_id }}-${{ github.sha }}-frontend-build-dist | ||
path: ${{ inputs.build-dist-folder-path }} | ||
retention-days: 1 | ||
|
||
- id: get_artifact_name | ||
name: Get First Image Name | ||
run: | | ||
echo "artifact_name=${{ github.repository_id }}-${{ github.sha }}-frontend-build-dist" >> $GITHUB_OUTPUT | ||
echo "Frontend Artifact Name: $artifact_name" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
name: Frontend Test | ||
|
||
on: | ||
workflow_call: | ||
inputs: | ||
runner-class: | ||
description: "Github Runner class to use" | ||
required: false | ||
type: string | ||
default: "ubuntu-latest" | ||
node-version: | ||
description: "Node version to use." | ||
required: false | ||
type: string | ||
default: "18.x" | ||
node-version-file: | ||
description: "Node version file to use. node-version overrides this parameter." | ||
required: false | ||
type: string | ||
default: "" | ||
context: | ||
description: "Root directory to start the build from." | ||
required: false | ||
type: string | ||
default: "." | ||
cache: | ||
description: "Use node modules installation caching. Default true." | ||
required: false | ||
type: boolean | ||
default: true | ||
cache-key-file: | ||
description: "Key file for cache." | ||
required: false | ||
type: string | ||
default: "${{ inputs.context }}/package.json" | ||
package-manager: | ||
description: "Package manager to use. Supports [npm, yarn]" | ||
required: false | ||
type: string | ||
default: "npm" | ||
test_frontend_command: | ||
description: "Testing command to run" | ||
required: true | ||
type: string | ||
test_frontend_build: | ||
description: "Perform a build test" | ||
required: false | ||
type: boolean | ||
default: true | ||
build_test_frontend_command: | ||
description: "Command to do build test on frontend" | ||
required: false | ||
type: string | ||
|
||
|
||
jobs: | ||
node-build: | ||
runs-on: ${{ inputs.runner-class }} | ||
|
||
environment: | ||
name: ${{ github.ref_name }} | ||
|
||
outputs: | ||
artifact-name: ${{ steps.get_artifact_name.outputs.artifact_name }} | ||
|
||
permissions: | ||
contents: read | ||
packages: write | ||
|
||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@v4 | ||
|
||
- id: node_setup | ||
name: Install node | ||
uses: actions/setup-node@v4 | ||
#reference: https://github.com/actions/setup-node | ||
with: | ||
node-version: ${{ inputs.node-version }} | ||
node-version-file: ${{ inputs.node-version-file && inputs.node-version-file || '' }} | ||
|
||
- name: Cache Node packages | ||
uses: actions/cache@v3 | ||
env: | ||
cache_name: node-${{ inputs.node-version || hashFiles(inputs.node-version-file) }}-${{ inputs.package-manager }}-${{ hashFiles(inputs.cache-key-file) }} | ||
with: | ||
key: ${{ runner.os }}-build-${{ env.cache_name }} | ||
path: | | ||
~/.npm | ||
restore-keys: | | ||
${{ runner.os }}-build-${{ env.cache_name }} | ||
- id: node_packages_install | ||
name: Install node pacakges | ||
working-directory: ${{ inputs.context }} | ||
run: | | ||
case "${{ inputs.package-manager }}" in | ||
yarn) | ||
yarn | ||
;; | ||
npm) | ||
npm i | ||
;; | ||
esac | ||
- id: vars_and_secrets | ||
name: Vars and Secrets to Env file | ||
env: | ||
VARS_CONTEXT: ${{ toJson(vars) }} | ||
SECRETS_CONTEXT: ${{ toJson(secrets) }} | ||
shell: bash | ||
run: | | ||
parsed_vars=$(jq -n --argjson VARS_CONTEXT "$VARS_CONTEXT" --argjson SECRETS_CONTEXT "$SECRETS_CONTEXT" "$VARS_CONTEXT+$SECRETS_CONTEXT") | ||
to_envs() { jq -r "to_entries[] | \"\(.key)=\\\"\(.value)\\\"\n\""; } | ||
echo "$parsed_vars" | to_envs > ${{ inputs.context }}/.env | ||
- id: test_frontend | ||
name: Test Frontend | ||
working-directory: ${{ inputs.context }} | ||
run: | | ||
${{ inputs.test_frontend_command }} | ||
- id: test_frontend_build | ||
name: Build Frontend | ||
if: ${{ inputs.test_frontend_build && inputs.build_test_frontend_command }} | ||
working-directory: ${{ inputs.context }} | ||
run: | | ||
${{ inputs.build_test_frontend_command }} |
Oops, something went wrong.