TradingBot CI #1709
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: TradingBot CI | |
on: | |
push: | |
branches: | |
- master # Prevent building other branches to speed up PRs | |
pull_request: | |
branches: | |
- master | |
schedule: | |
- cron: '0 0 * * *' # nightly build | |
env: | |
poetry-version: '1.2.2' | |
docker-image-name: ilcardella/tradingbot | |
push-docker-image: ${{ startsWith(github.ref, 'refs/tags/') || github.ref == 'refs/heads/master' }} | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
strategy: | |
matrix: | |
python-version: ['3.8', '3.9', '3.10'] | |
name: Python ${{ matrix.python-version }} | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v3 | |
- name: Set up Python ${{ matrix.python-version }} | |
uses: actions/setup-python@v4 | |
with: | |
python-version: ${{ matrix.python-version }} | |
architecture: x64 | |
- name: Pip cache | |
uses: actions/cache@v3 | |
with: | |
# This path is specific to Ubuntu | |
path: ~/.cache/pip | |
# Look to see if there is a cache hit for the corresponding poetry.lock | |
key: ${{ runner.os }}-pip-${{ env.poetry-version }} | |
restore-keys: | | |
${{ runner.os }}-pip- | |
${{ runner.os }}- | |
- name: Install dependencies | |
run: | | |
python -m pip install --upgrade pip | |
pip install 'poetry==${{ env.poetry-version }}' | |
- name: Poetry cache | |
uses: actions/cache@v3 | |
with: | |
path: ~/.cache/pypoetry/virtualenvs | |
key: ${{ runner.os }}-poetry-${{ hashFiles('poetry.lock') }} | |
restore-keys: | | |
${{ runner.os }}-poetry- | |
- name: Run tests | |
run: make ci | |
docker: | |
needs: build | |
runs-on: ubuntu-latest | |
name: Docker build and push | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v3 | |
- name: Prepare | |
id: prepare | |
run: | | |
DOCKER_IMAGE=${{ env.docker-image-name }} | |
VERSION=latest | |
PLATFORMS=linux/amd64,linux/arm64 | |
if [[ $GITHUB_REF == refs/tags/* ]]; then | |
VERSION=${GITHUB_REF#refs/tags/v} | |
elif [[ $GITHUB_REF != refs/heads/master ]]; then | |
VERSION=${GITHUB_REF#refs/heads/} | |
VERSION=${VERSION##*/} | |
fi | |
TAGS="${DOCKER_IMAGE}:${VERSION}" | |
if [[ $VERSION =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then | |
TAGS="$TAGS,${DOCKER_IMAGE}:latest" | |
fi | |
echo ::set-output name=platforms::${PLATFORMS} | |
echo ::set-output name=tags::${TAGS} | |
- name: Set up QEMU | |
uses: docker/setup-qemu-action@master | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@master | |
- name: Cache Docker layers | |
uses: actions/cache@v3 | |
id: cache | |
with: | |
path: /tmp/.buildx-cache | |
key: ${{ runner.os }}-buildx-${{ github.sha }} | |
restore-keys: | | |
${{ runner.os }}-buildx- | |
- name: Login to DockerHub | |
if: ${{ env.push-docker-image }} | |
uses: docker/login-action@v2 | |
with: | |
username: ${{ secrets.DOCKER_USERNAME }} | |
password: ${{ secrets.DOCKER_PASSWORD }} | |
- name: Build and push | |
id: docker_build | |
uses: docker/build-push-action@v3 | |
with: | |
context: . | |
file: docker/Dockerfile | |
platforms: ${{ steps.prepare.outputs.platforms }} | |
build-args: POETRY_VERSION=${{ env.poetry-version }} | |
push: ${{ env.push-docker-image }} | |
tags: ${{ steps.prepare.outputs.tags }} | |
cache-from: type=local,src=/tmp/.buildx-cache | |
cache-to: type=local,dest=/tmp/.buildx-cache | |
- name: Clear | |
if: always() | |
run: | | |
rm -f ${HOME}/.docker/config.json |