Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CI workflow using github Actions #120

Merged
merged 25 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
b881e1e
fix: conflicts with example.env file
nischalstha9 Aug 30, 2024
ef0b70f
fix: workflow name
nischalstha9 Aug 30, 2024
b978280
change branch name for test
nischalstha9 Aug 30, 2024
20c7130
fix: path for .env
nischalstha9 Sep 4, 2024
6fb8ee3
fix: frontend build branch
nischalstha9 Sep 4, 2024
30629a6
try vars_sec to .env file
nischalstha9 Sep 4, 2024
65f0c21
CI: try build and verify
nischalstha9 Sep 5, 2024
fb41343
fix: file name for remote deploy
nischalstha9 Sep 5, 2024
730c07f
ci: fix frontend build dist name
nischalstha9 Sep 5, 2024
80bba5e
ci: download artifacts to folder
nischalstha9 Sep 5, 2024
8fa5524
ci: add test
nischalstha9 Sep 5, 2024
9de78b3
CI: use naxa-developers workflow
nischalstha9 Sep 5, 2024
7c55c82
ci: disable coverage
nischalstha9 Sep 5, 2024
f9c6d1e
fix: conflicts from upstream rebase
nischalstha9 Sep 5, 2024
f817223
ci: disable build on frotnend test
nischalstha9 Sep 5, 2024
bd1f50c
ci: skil test
nischalstha9 Sep 5, 2024
ae98c81
wait for db
nischalstha9 Sep 5, 2024
93cf91c
fix entrypoint
nischalstha9 Sep 5, 2024
b0637d6
fix: compose cmd on test backend
nischalstha9 Sep 5, 2024
4121fba
fix: compose cmd on test backend
nischalstha9 Sep 5, 2024
d7f0af0
fix: run -> exec on test compose
nischalstha9 Sep 5, 2024
6b201b3
fix: run -> exec on test compose
nischalstha9 Sep 5, 2024
728da80
ci: add print logs
nischalstha9 Sep 6, 2024
fcf3675
ci: everything together, and dry run
mahesh-naxa Sep 16, 2024
8580ca4
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 17, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions .github/workflows/build_and_deploy.yml
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
149 changes: 149 additions & 0 deletions .github/workflows/frontend-build.yml
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"
128 changes: 128 additions & 0 deletions .github/workflows/frontend-test.yml
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 }}
Loading
Loading