Skip to content

fix

fix #124

name: Build and Deploy
on:
# Push includes PR merge
push:
branches:
- main
- staging
- develop
paths:
# Workflow is triggered only if src changes
- src/**
# Allow manual trigger
workflow_dispatch:
jobs:
backend-build:
uses: hotosm/gh-workflows/.github/workflows/[email protected]
with:
context: ./src/backend
build_target: service
image_name: ghcr.io/${{ github.repository }}/backend
dockerfile: Dockerfile
secrets: inherit
encode-envs:
runs-on: ubuntu-latest
outputs:
base64_env: ${{ steps.vars_and_secrets_to_b64.outputs.base64_env }}
steps:
- id: vars_and_secrets_to_b64
name: Vars and Secrets to base64 encoded
env:
VARS_CONTEXT: ${{ toJson(vars) }}
SECRETS_CONTEXT: ${{ toJson(secrets) }}
run: |
parsed_vars=$(jq -n --argjson VARS_CONTEXT "$VARS_CONTEXT" --argjson SECRETS_CONTEXT "$SECRETS_CONTEXT" '$VARS_CONTEXT + $SECRETS_CONTEXT')
echo "Concat vars+secrets: ${parsed_vars}"
# Random delimeter string for security
delim=$(dd if=/dev/urandom bs=15 count=1 status=none | base64)
EOF=$delim
# Func to parse JSON with multiline strings, using delimeter (Github specific)
to_envs() { jq -r "to_entries[] | \"\(.key)=\(.value)\n\""; }
base64_kv=$(echo "$parsed_vars" | to_envs | base64)
echo "base64_env<<$EOF" >> $GITHUB_OUTPUT
echo "$base64kv"
echo "$EOF" >> $GITHUB_OUTPUT
echo $GITHUB_OUTPUT
frontend-build:
uses: hotosm/gh-workflows/.github/workflows/[email protected]
needs:
- encode-envs
with:
context: ./src/frontend
build_target: live
image_name: ghcr.io/${{ github.repository }}/frontend
dockerfile: Dockerfile
extra_build_args: B64_ARGS_TO_ENV=${{ needs.encode-envs.outputs.base64_env }}
secrets: inherit
postgres-vol-creation:
runs-on: ubuntu-latest
name: Create database volume
needs:
- backend-build
environment: ${{ github.ref_name }}
steps:
- name: Setup SSH Key
uses: webfactory/[email protected]
with:
ssh-private-key: "${{ secrets.SSH_PRIVATE_KEY }}"
- name: Add host keys to known_hosts
run: |
ssh-keyscan "${{ vars.SSH_HOST }}" >> ~/.ssh/known_hosts
- name: Deploy to VM
run: |
# Create db data volume if not exists
docker volume create drone-tm-db-data-${{ github.ref_name }} || true
env:
DOCKER_HOST: "ssh://${{ vars.SSH_USER }}@${{ vars.SSH_HOST }}"
deploy_to_vm:
name: Deploy to VM
needs:
- postgres-vol-creation
- frontend-build
uses: hotosm/gh-workflows/.github/workflows/[email protected]
with:
docker_compose_file: docker-compose.vm.yml
environment: ${{ github.ref_name }}
secrets: inherit