Skip to content

chore: always restart in prod #82

chore: always restart in prod

chore: always restart in prod #82

name: Docker Compose Deployment CI
on:
push:
branches:
- main
- deploy-*
tags:
- v*.*.*
jobs:
deploy:
runs-on: ubuntu-latest
strategy:
matrix:
env:
# name of envs
# This is also the name of the directory for the application
- ${{ startsWith(github.ref, 'refs/tags/v') && 'facets-kp-org' || 'facets-kp-net' }}
environment: ${{ matrix.env }}
concurrency: ${{ matrix.env }}
steps:
- name: Set various variable for staging deployment
if: matrix.env == 'facets-kp-net'
run: |
# deploy target
echo "SSH_HOST=10.1.0.200" >> $GITHUB_ENV
echo "SSH_PROXY_HOST=ovh1.openfoodfacts.org" >> $GITHUB_ENV
echo "SSH_USERNAME=off" >> $GITHUB_ENV
# expose port
echo "API_EXPOSE=8032" >> $GITHUB_ENV
# app parameters
echo "FACETS_OPENFOODFACTS=off:[email protected]" >> $GITHUB_ENV
echo "FACETS_TAXONOMY=https://off:[email protected]/api/v2/taxonomy" >> $GITHUB_ENV
- name: Set various variable for production deployment
if: matrix.env == 'facets-kp-org'
run: |
# deploy target
echo "SSH_HOST=10.1.0.201" >> $GITHUB_ENV
echo "SSH_PROXY_HOST=ovh1.openfoodfacts.org" >> $GITHUB_ENV
echo "SSH_USERNAME=off" >> $GITHUB_ENV
# expose port
echo "API_EXPOSE=8032" >> $GITHUB_ENV
# app parameters
echo "FACETS_OPENFOODFACTS=world.openfoodfacts.org" >> $GITHUB_ENV
echo "FACETS_TAXONOMY=https://world.openfoodfacts.org/api/v2/taxonomy" >> $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 == 'does not exist'
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: |
set -o errexit
# Clone Git repository if not already there
if [ ! -d '${{ matrix.env }}' ]
then
git clone --depth 1 https://github.com/${{ github.repository }} ${{ matrix.env }} --no-single-branch 2>&1
fi
# Go to repository directory
cd ${{ matrix.env }}
# 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: |
# fail on error
set -o errexit
# Go to repository directory
cd ${{ matrix.env }}
# 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=${{ matrix.env }}" >> .env
echo "COMPOSE_PATH_SEPARATOR=;" >> .env
echo "RESTART_POLICY=always" >> .env
echo "COMPOSE_FILE=docker-compose.yml;docker/prod.yml" >> .env
echo "TAG=sha-${{ github.sha }}" >> .env
# App environment variables
# expose globally (on the VM)
echo "API_EXPOSE=${{ env.API_EXPOSE }}" >> .env
echo "FACETS_HUNGER_GAME=https://hunger.openfoodfacts.org/questions" >> .env
echo "FACETS_WIKIDATA=https://www.wikidata.org/wiki/" >> .env
echo "FACETS_INAO=https://www.inao.gouv.fr/produit/" >> .env
echo "FACETS_OPENSTREETMAP=https://www.openstreetmap.org/relation/" >> .env
echo "FACETS_OPENFOODFACTS=${{ env.FACETS_OPENFOODFACTS }}" >> .env
echo "FACETS_TAXONOMY=${{ env.FACETS_TAXONOMY }}" >> .env
# No volume to create, yet
# - name: Create external Docker volumes
# 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 ${{ matrix.env }}
# docker volume create <VOLUME_NAME>
- 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 ${{ matrix.env }}
docker-compose up -d --remove-orphans 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 ${{ matrix.env }}
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 ${{ matrix.env }}
docker system prune -af
- uses: basos9/[email protected]
if: ${{ always() }}
with:
apiHost: https://grafana.openfoodfacts.org
apiToken: ${{ secrets.GRAFANA_API_TOKEN }}
text: <a href="https://github.com/${{github.repository}}/actions/runs/${{github.run_id}}">Deployment ${{ steps.livecheck.outcome }} on ${{ matrix.env }}</a>
tags: type:deployment,origin:github,status:${{ steps.livecheck.outcome }},repo:${{ github.repository }},sha:${{ github.sha }},app:robotoff,env:${{ matrix.env }}