Skip to content

🔧 Build and Deploy #316

🔧 Build and Deploy

🔧 Build and Deploy #316

name: Build and Deploy
on:
# Push includes PR merge
push:
branches:
- main
- staging
- development
paths:
# Workflow is triggered only if src changes
- "src/**"
# Allow manual trigger
workflow_dispatch:
env:
REGISTRY: ghcr.io
GIT_BRANCH: ${{ github.ref_name }}
jobs:
build-and-push-images:
runs-on: ubuntu-latest
environment:
name: ${{ github.ref_name }}
permissions:
contents: read
packages: write
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Log in to the Container registry
uses: docker/login-action@v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Persist env vars
run: echo "${{ secrets.DOTENV }}" >> $GITHUB_ENV
- name: Extract api version
id: extract_api_version
run: |
cd src/backend
echo "API_VERSION=$(python -c 'from app.__version__ import __version__; print(__version__)')" >> $GITHUB_ENV
- name: Extract frontend versions
id: extract_frontend_versions
run: |
cd src/frontend
echo "FRONTEND_MAIN_VERSION=$(jq -r '.version' main/package.json)" >> $GITHUB_ENV
echo "FRONTEND_MAP_VERSION=$(jq -r '.version' fmtm_openlayer_map/package.json)" >> $GITHUB_ENV
- name: Build and push backend
uses: docker/build-push-action@v4
with:
context: src/backend
target: prod
push: true
tags: |
"ghcr.io/hotosm/fmtm/backend:${{ env.API_VERSION }}-${{ github.ref_name }}"
"ghcr.io/hotosm/fmtm/backend:latest"
build-args: |
APP_VERSION=${{ env.API_VERSION }}
- name: Build and push frontend main
uses: docker/build-push-action@v4
with:
context: src/frontend
file: src/frontend/prod.dockerfile
push: true
tags: "ghcr.io/hotosm/fmtm/frontend/main:${{ env.FRONTEND_MAIN_VERSION }}-${{ github.ref_name }}"
build-args: |
APP_NAME=main
APP_VERSION=${{ env.FRONTEND_MAIN_VERSION }}
API_URL=${{ env.URL_SCHEME }}://${{ env.API_URL }}
FRONTEND_MAIN_URL=${{ env.URL_SCHEME }}://${{ env.FRONTEND_MAIN_URL }}
FRONTEND_MAP_URL=${{ env.URL_SCHEME }}://${{ env.FRONTEND_MAP_URL }}
- name: Build and push frontend map
uses: docker/build-push-action@v4
with:
context: src/frontend
file: src/frontend/prod.dockerfile
push: true
tags: "ghcr.io/hotosm/fmtm/frontend/map:${{ env.FRONTEND_MAP_VERSION }}-${{ github.ref_name }}"
build-args: |
APP_NAME=fmtm_openlayer_map
APP_VERSION=${{ env.FRONTEND_MAP_VERSION }}
API_URL=${{ env.URL_SCHEME }}://${{ env.API_URL }}
FRONTEND_MAIN_URL=${{ env.URL_SCHEME }}://${{ env.FRONTEND_MAIN_URL }}
FRONTEND_MAP_URL=${{ env.URL_SCHEME }}://${{ env.FRONTEND_MAP_URL }}
deploy-containers:
runs-on: ubuntu-latest
needs: build-and-push-images
environment:
name: ${{ github.ref_name }}
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Extract api version
id: extract_api_version
run: |
cd src/backend
echo "API_VERSION=$(python -c 'from app.__version__ import __version__; print(__version__)')" >> $GITHUB_OUTPUT
- name: Extract frontend versions
id: extract_frontend_versions
run: |
cd src/frontend
echo "FRONTEND_MAIN_VERSION=$(jq -r '.version' main/package.json)" >> $GITHUB_OUTPUT
echo "FRONTEND_MAP_VERSION=$(jq -r '.version' fmtm_openlayer_map/package.json)" >> $GITHUB_OUTPUT
- name: Environment to .env
run: |
echo "${{ secrets.DOTENV }}" > .env
echo "API_VERSION=${{ steps.extract_api_version.outputs.API_VERSION }}" >> .env
echo "FRONTEND_MAIN_VERSION=${{ steps.extract_frontend_versions.outputs.FRONTEND_MAIN_VERSION }}" >> .env
echo "FRONTEND_MAP_VERSION=${{ steps.extract_frontend_versions.outputs.FRONTEND_MAP_VERSION }}" >> .env
- uses: webfactory/[email protected]
with:
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
- name: Disable Host key verification
# Hack to prevent "Host key verification failed". Should be replaced with a ssh-keyscan based solution
run: echo "StrictHostKeyChecking no" >> ~/.ssh/config
- name: Deploy
run: |
docker compose --file docker-compose.deploy.yml pull
docker compose --file docker-compose.deploy.yml up --detach
env:
DOCKER_HOST: ${{ vars.DOCKER_HOST }}