Merge pull request #113 from openfoodfacts/112-support-new-multi-flav… #82
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: Docker Compose Deployment | |
on: | |
push: | |
branches: | |
- 'main' | |
- deploy-* | |
tags: | |
- v*.*.* | |
jobs: | |
deploy: | |
runs-on: ubuntu-latest | |
strategy: | |
matrix: | |
env: | |
# FIXME name you envs, see reuse below | |
# it's also the name of the directory for the application | |
- ${{ startsWith(github.ref, 'refs/tags/v') && 'off-query-org' || 'off-query-net' }} | |
environment: ${{ matrix.env }} | |
concurrency: ${{ matrix.env }} | |
steps: | |
- name: Set various common variable for deployment | |
run: | | |
echo "PROJECT_DIR=${{ matrix.env }}" >> $GITHUB_ENV | |
- name: Set various variable for staging deployment | |
if: matrix.env == 'off-query-net' | |
run: | | |
echo "SSH_PROXY_HOST=ovh1.openfoodfacts.org" >> $GITHUB_ENV | |
echo "SSH_USERNAME=off" >> $GITHUB_ENV | |
# deploy target | |
echo "SSH_HOST=10.1.0.200" >> $GITHUB_ENV | |
# configurations | |
echo "COMMON_NET_NAME=po_webnet" >> $GITHUB_ENV | |
echo "MONGO_URI=mongodb://10.1.0.200:27017" >> $GITHUB_ENV | |
echo "REDIS_URL=redis://redis:6379" >> $GITHUB_ENV | |
# Keep debug logging on staging | |
echo "LOG_LEVEL=debug" >> $GITHUB_ENV | |
# Postgres config | |
echo "POSTGRES_SHM_SIZE=256m" >> $GITHUB_ENV | |
echo "QUERY_POSTGRES_SHARED_BUFFERS=4GB" >> $GITHUB_ENV | |
echo "QUERY_POSTGRES_WORK_MEM=256MB" >> $GITHUB_ENV | |
- name: Set various variable for production deployment | |
if: matrix.env == 'off-query-org' | |
run: | | |
# This is the OSM proxy server, that does have an ipv4 address | |
echo "SSH_PROXY_HOST=45.147.209.254" >> $GITHUB_ENV | |
echo "SSH_USERNAME=off" >> $GITHUB_ENV | |
echo "SSH_PROXY_USERNAME=off" >> $GITHUB_ENV | |
echo "SSH_HOST=10.3.0.200" >> $GITHUB_ENV | |
# configurations | |
echo "COMMON_NET_NAME=common_net" >> $GITHUB_ENV | |
# Reduce log level on production | |
echo "LOG_LEVEL=log" >> $GITHUB_ENV | |
# mongodb and redis (through stunnel) | |
echo "MONGO_URI=mongodb://10.3.0.101:27017" >> $GITHUB_ENV | |
echo "REDIS_URL=redis://10.3.0.101:6379" >> $GITHUB_ENV | |
# Postgres config. We use memory to gain performance on reads and writes | |
echo "POSTGRES_SHM_SIZE=2g" >> $GITHUB_ENV | |
echo "QUERY_POSTGRES_SHARED_BUFFERS=16GB" >> $GITHUB_ENV | |
echo "QUERY_POSTGRES_WORK_MEM=1GB" >> $GITHUB_ENV | |
- name: Wait for container build workflow | |
uses: tomchv/[email protected] | |
id: wait-build | |
with: | |
token: ${{ secrets.GITHUB_TOKEN }} | |
checkName: build | |
ref: ${{ github.event.pull_request.head.sha || github.sha }} | |
intervalSeconds: 10 | |
timeoutSeconds: 600 # 10m | |
- name: Do something if build isn't launched | |
if: steps.wait-build.outputs.conclusion == 'not found' | |
run: echo job does not exist && true | |
- name: Do something if build fail | |
if: steps.wait-build.outputs.conclusion == 'failure' | |
run: echo fail && false # fail if build fail | |
- name: Do something if build timeout | |
if: steps.wait-build.outputs.conclusion == 'timed_out' | |
run: echo Timeout && false # fail if build time out | |
- name: Checkout git repository | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ env.SSH_HOST }} | |
username: ${{ env.SSH_USERNAME }} | |
key: ${{ secrets.SSH_PRIVATE_KEY }} | |
proxy_host: ${{ env.SSH_PROXY_HOST }} | |
proxy_username: ${{ env.SSH_USERNAME }} | |
proxy_key: ${{ secrets.SSH_PRIVATE_KEY }} | |
script_stop: false | |
script: | | |
# Clone Git repository if not already there | |
[ ! -d '${{ env.PROJECT_DIR }}' ] && git clone --depth 1 https://github.com/${{ github.repository }} ${{ env.PROJECT_DIR }} --no-single-branch 2>&1 | |
# Go to repository directory | |
cd ${{ env.PROJECT_DIR }} | |
# Fetch newest commits (in case it wasn't freshly cloned) | |
git fetch --depth 1 | |
# Checkout current commit SHA | |
git checkout -qf ${{ github.sha }} | |
- name: Set environment variables | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ env.SSH_HOST }} | |
username: ${{ env.SSH_USERNAME }} | |
key: ${{ secrets.SSH_PRIVATE_KEY }} | |
proxy_host: ${{ env.SSH_PROXY_HOST }} | |
proxy_username: ${{ env.SSH_USERNAME }} | |
proxy_key: ${{ secrets.SSH_PRIVATE_KEY }} | |
script_stop: false | |
script: | | |
# Go to repository directory | |
cd ${{ env.PROJECT_DIR }} | |
# new env | |
rm .env | |
touch .env | |
# Set Docker Compose variables | |
echo "DOCKER_CLIENT_TIMEOUT=120" >> .env | |
echo "COMPOSE_HTTP_TIMEOUT=120" >> .env | |
echo "COMPOSE_PROJECT_NAME=off-query" >> .env | |
echo "COMPOSE_PATH_SEPARATOR=," >> .env | |
echo "RESTART_POLICY=always" >> .env | |
echo "COMPOSE_FILE=docker-compose.yml" >> .env | |
echo "TAG=sha-${{ github.sha }}" >> .env | |
# App environment variables | |
echo "POSTGRES_USER=postgres" >> .env | |
echo "POSTGRES_PASSWORD=${{ secrets.POSTGRES_PASSWORD }}" >> .env | |
echo "POSTGRES_DB=query" >> .env | |
echo "COMMON_NET_NAME=${{ env.COMMON_NET_NAME }}" >> .env | |
echo "MONGO_URI=${{ env.MONGO_URI }}" >> .env | |
echo "REDIS_URL=${{ env.REDIS_URL }}" >> .env | |
# Postgres config | |
echo "POSTGRES_SHM_SIZE=${{ env.POSTGRES_SHM_SIZE }}" >> .env | |
echo "QUERY_POSTGRES_SHARED_BUFFERS=${{ env.QUERY_POSTGRES_SHARED_BUFFERS }}" >> .env | |
echo "QUERY_POSTGRES_WORK_MEM=${{ env.QUERY_POSTGRES_WORK_MEM }}" >> .env | |
# Use docker volume for now | |
echo "QUERY_DATA_DIR=dbdata" >> .env | |
echo "LOG_LEVEL=${{ env.LOG_LEVEL }}" >> .env | |
- name: Start services | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ env.SSH_HOST }} | |
username: ${{ env.SSH_USERNAME }} | |
key: ${{ secrets.SSH_PRIVATE_KEY }} | |
proxy_host: ${{ env.SSH_PROXY_HOST }} | |
proxy_username: ${{ env.SSH_USERNAME }} | |
proxy_key: ${{ secrets.SSH_PRIVATE_KEY }} | |
script_stop: false | |
script: | | |
cd ${{ env.PROJECT_DIR }} | |
docker compose up -d 2>&1 | |
- name: Check services are up | |
uses: appleboy/ssh-action@master | |
if: ${{ always() }} | |
id: livecheck | |
with: | |
host: ${{ env.SSH_HOST }} | |
username: ${{ env.SSH_USERNAME }} | |
key: ${{ secrets.SSH_PRIVATE_KEY }} | |
proxy_host: ${{ env.SSH_PROXY_HOST }} | |
proxy_username: ${{ env.SSH_USERNAME }} | |
proxy_key: ${{ secrets.SSH_PRIVATE_KEY }} | |
script_stop: false | |
script: | | |
cd ${{ env.PROJECT_DIR }} | |
exit_code=0 | |
for service in `docker compose config --service | tr '\n' ' '`; do | |
if [ -z `docker compose ps -q $service` ] || [ -z `docker ps -q --no-trunc | grep $(docker compose ${{ env.compose_args }} ps -q $service)` ]; then | |
echo "$service: DOWN" | |
exit_code=1 | |
else | |
echo "$service: UP" | |
fi | |
done; | |
exit $exit_code; | |
- name: Cleanup obsolete Docker objects | |
uses: appleboy/ssh-action@master | |
if: ${{ always() }} | |
with: | |
host: ${{ env.SSH_HOST }} | |
username: ${{ env.SSH_USERNAME }} | |
key: ${{ secrets.SSH_PRIVATE_KEY }} | |
proxy_host: ${{ env.SSH_PROXY_HOST }} | |
proxy_username: ${{ env.SSH_USERNAME }} | |
proxy_key: ${{ secrets.SSH_PRIVATE_KEY }} | |
script_stop: false | |
script: | | |
cd ${{ env.PROJECT_DIR }} | |
docker system prune -af |