create env from vars context #156
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
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 | |
environment: | |
name: ${{ github.ref_name }} | |
outputs: | |
ENCODED_ENV: ${{ steps.vars_and_secrets_to_b64.outputs.ENCODED_ENV }} | |
steps: | |
- name: Vars to Env File | |
env: | |
GIT_BRANCH: ${{ github.ref_name }} | |
VARS_CONTEXT: ${{ toJson(vars) }} | |
run: | | |
# Random delimeter string for security | |
delim=$(dd if=/dev/urandom bs=15 count=1 status=none | base64) | |
# Parse JSON with multiline strings, using delimeter (Github specific) | |
to_envs() { jq -r "to_entries[] | \"\(.key)<<$delim\n\(.value)\n$delim\n\""; } | |
# Create .env file from VARS_CONTEXT | |
if [ "${VARS_CONTEXT}" != "null" ]; then | |
echo "${VARS_CONTEXT}" | to_envs > .env | |
fi | |
- name: Vars and Secrets to base64 encoded | |
id: vars_and_secrets_to_b64 | |
run: | | |
ENCODED_ENV=$(cat .env | base64 -w0 | tr -d '\n' | tr -d '[:space:]' | xargs ) | |
echo "ENCODED_ENV=${ENCODED_ENV}" >> "$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: BASE64_ARGS_TO_ENV=${{ needs.encode-envs.outputs.ENCODED_ENV }} | |
secrets: inherit | |
postgres-vol-creation: | |
runs-on: ubuntu-latest | |
name: Create database volume | |
needs: | |
- backend-build | |
environment: | |
name: ${{ 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 |