Merge pull request #313 from sysblok/dev #100
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 production | |
on: | |
push: | |
# Publish `master` as Docker `prod` image. | |
branches: | |
- master | |
# Publish `v1.2.3` tags as releases. | |
tags: | |
- v* | |
env: | |
IMAGE_NAME: sysblokbot | |
jobs: | |
# Run tests. | |
# See also https://docs.docker.com/docker-hub/builds/automated-testing/ | |
test: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Cancel Previous Runs | |
uses: styfle/cancel-workflow-action@0.9.1 | |
with: | |
access_token: ${{ github.token }} | |
- uses: actions/checkout@v2 | |
with: | |
ref: master | |
- name: Python Style Checker | |
working-directory: ${{ github.workspace }} | |
run: | | |
pip3 install --upgrade pip | |
export PATH="$HOME/.local/bin:$PATH" | |
pip3 install pycodestyle | |
pycodestyle . --max-line-length 100 | |
- name: Notify us about failure | |
if: ${{ failure() }} | |
env: | |
TELEGRAM_TOKEN: ${{ secrets.TELEGRAM_PROD_TOKEN }} | |
TELEGRAM_ERROR_CHAT_ID: ${{ secrets.TELEGRAM_ERROR_CHAT_ID }} | |
run: | | |
curl -X POST \ | |
-H 'Content-Type: application/json' \ | |
-d '{"parse_mode": "markdown", "chat_id": "${{ env.TELEGRAM_ERROR_CHAT_ID }}", "text": "[github CI] pycodestyle [failed](https://github.com/sysblok/sysblokbot/actions/runs/${{github.run_id}})"}' \ | |
https://api.telegram.org/bot${{env.TELEGRAM_TOKEN}}/sendMessage | |
# - name: Python Pytest Github Action | |
# uses: fylein/python-pytest-github-action@v2 | |
# with: | |
# args: apt-get update && apt-get install -y git && pip3 install -e . && pip3 install -r requirements.txt && pytest tests/unit | |
- name: Notify us about failure | |
if: ${{ failure() }} | |
env: | |
TELEGRAM_TOKEN: ${{ secrets.TELEGRAM_PROD_TOKEN }} | |
TELEGRAM_ERROR_CHAT_ID: ${{ secrets.TELEGRAM_ERROR_CHAT_ID }} | |
run: | | |
curl -X POST \ | |
-H 'Content-Type: application/json' \ | |
-d '{"parse_mode": "markdown", "chat_id": ${{ env.TELEGRAM_ERROR_CHAT_ID }}, "text": "[github CI] Unit tests [failed](https://github.com/sysblok/sysblokbot/actions/runs/${{github.run_id}})"}' \ | |
https://api.telegram.org/bot${{env.TELEGRAM_TOKEN}}/sendMessage | |
# Push image to GitHub Packages. | |
# See also https://docs.docker.com/docker-hub/builds/ | |
push: | |
# Ensure test job passes before pushing image. | |
needs: test | |
runs-on: ubuntu-latest | |
if: github.event_name == 'push' | |
steps: | |
- uses: actions/checkout@v2 | |
with: | |
ref: master | |
- name: Generate build args | |
id: args | |
run: | | |
echo "::set-output name=commit_hash::$(git rev-parse HEAD)" | |
echo "::set-output name=commit_hash_short::$(git rev-parse --short HEAD)" | |
- name: Publish to Github Packages Registry with cache | |
uses: whoan/docker-build-with-cache-action@v5 | |
env: | |
IMAGE_NAME: ${{ env.IMAGE_NAME }} | |
COMMIT_HASH: "${{ steps.args.outputs.commit_hash }}" | |
COMMIT_HASH_SHORT: "${{ steps.args.outputs.commit_hash_short }}" | |
with: | |
image_name: ${{ github.repository }}/${{ env.IMAGE_NAME }} | |
registry: docker.pkg.github.com | |
username: sysblok | |
password: ${{ secrets.GITHUB_TOKEN }} | |
dockerfile: Dockerfile | |
image_tag: "prod,latest" | |
build_extra_args: "--build-arg=COMMIT_HASH --build-arg=COMMIT_HASH_SHORT" | |
- name: Notify us about failure | |
if: ${{ failure() }} | |
env: | |
TELEGRAM_TOKEN: ${{ secrets.TELEGRAM_PROD_TOKEN }} | |
TELEGRAM_ERROR_CHAT_ID: ${{ secrets.TELEGRAM_ERROR_CHAT_ID }} | |
run: | | |
curl -X POST \ | |
-H 'Content-Type: application/json' \ | |
-d '{"parse_mode": "markdown", "chat_id": "${{ env.TELEGRAM_ERROR_CHAT_ID }}", "text": "[github CI] build [failed](https://github.com/sysblok/sysblokbot/actions/runs/${{github.run_id}})" \ | |
}' \ | |
https://api.telegram.org/bot${{env.TELEGRAM_TOKEN}}/sendMessage | |
- name: Deploy package to digitalocean | |
uses: appleboy/ssh-action@master | |
env: | |
GITHUB_USERNAME: sysblok | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
IMAGE_NAME: ${{ env.IMAGE_NAME }} | |
ROOT_DIR: /home/${{ secrets.MASTER_DO_USER }}/prod | |
TELEGRAM_TOKEN: ${{ secrets.TELEGRAM_PROD_TOKEN }} | |
TELEGRAM_ERROR_CHAT_ID: ${{ secrets.TELEGRAM_ERROR_CHAT_ID }} | |
with: | |
host: ${{ secrets.MASTER_HOST }} | |
username: ${{ secrets.MASTER_DO_USER }} | |
passphrase: ${{ secrets.MASTER_DO_SSH_KEY_PASSWORD }} | |
key: ${{ secrets.MASTER_DO_SSH_KEY }} | |
port: ${{ secrets.MASTER_PORT }} | |
envs: GITHUB_USERNAME, GITHUB_TOKEN, IMAGE_NAME, ROOT_DIR, TELEGRAM_TOKEN, TELEGRAM_ERROR_CHAT_ID | |
script: | | |
export CONTAINER_ID=$(docker ps -aq --filter name=prod) | |
export IMAGE_ID=$(docker images -aq --filter reference='docker.pkg.github.com/sysblok/sysblokbot/sysblokbot:prod') | |
docker stop --time=30 $CONTAINER_ID | |
docker rm $CONTAINER_ID | |
docker rmi $IMAGE_ID | |
docker login docker.pkg.github.com -u $GITHUB_USERNAME -p $GITHUB_TOKEN | |
touch ${{ env.ROOT_DIR }}/sysblokbot.sqlite | |
touch ${{ env.ROOT_DIR }}/strings.sqlite | |
touch ${{ env.ROOT_DIR }}/board_credentials.json | |
docker run -dit --name sysblokbot-prod \ | |
--env APP_SOURCE="github CI" --restart unless-stopped \ | |
--env TELEGRAM_ERROR_CHAT_ID="${{ env.TELEGRAM_ERROR_CHAT_ID }}" \ | |
--env TELEGRAM_TOKEN="${{ env.TELEGRAM_TOKEN }}" \ | |
-v ${{ env.ROOT_DIR }}/config_override.json:/app/config_override.json \ | |
-v ${{ env.ROOT_DIR }}/config_gs.json:/app/config_gs.json \ | |
-v ${{ env.ROOT_DIR }}/sysblokbot.sqlite:/app/sysblokbot.sqlite \ | |
-v ${{ env.ROOT_DIR }}/strings.sqlite:/app/strings.sqlite \ | |
-v ${{ env.ROOT_DIR }}/persistent_storage.pickle:/app/persistent_storage.pickle \ | |
-v ${{ env.ROOT_DIR }}/board_credentials.json:/app/board_credentials.json \ | |
docker.pkg.github.com/sysblok/sysblokbot/${{ env.IMAGE_NAME }}:prod | |
- name: Notify us about failure | |
if: ${{ failure() }} | |
env: | |
TELEGRAM_TOKEN: ${{ secrets.TELEGRAM_PROD_TOKEN }} | |
TELEGRAM_ERROR_CHAT_ID: ${{ secrets.TELEGRAM_ERROR_CHAT_ID }} | |
run: | | |
curl -X POST \ | |
-H 'Content-Type: application/json' \ | |
-d '{"parse_mode": "markdown", "chat_id": ${{ env.TELEGRAM_ERROR_CHAT_ID }}, "text": "[github CI] deploy [failed](https://github.com/sysblok/sysblokbot/actions/runs/${{github.run_id}})"}' \ | |
https://api.telegram.org/bot${{env.TELEGRAM_TOKEN}}/sendMessage | |
# integration_tests: | |
# needs: push | |
# runs-on: ubuntu-latest | |
# steps: | |
# - uses: actions/checkout@v2 | |
# with: | |
# ref: master | |
# - name: Integration tests | |
# uses: fylein/python-pytest-github-action@v2 | |
# env: | |
# CONFIG_OVERRIDE: ${{ secrets.CONFIG_OVERRIDE_PROD }} | |
# with: | |
# args: | | |
# apt-get update && apt-get install -y git && \ | |
# pip3 install -e . && pip3 install -r requirements.txt && \ | |
# pytest tests/integration -vs |