flask/.github/workflows/flaskr-ci.yaml
2023-02-21 21:13:13 -05:00

87 lines
No EOL
3.4 KiB
YAML

name: Build Flaskr App
on: [push]
jobs:
tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python 3.11
uses: actions/setup-python@v4
with:
python-version: '3.11.x'
architecture: 'x64'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8 pytest
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
# Install project file for flaskr app
pip install -e examples/tutorial
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Test with pytest
run: |
pwd
cd examples/tutorial
pytest
# Build Docker image in a separate job so that it's not taited by cached python build/test files
build-image:
needs: tests
permissions:
# Required permission to push images with the built-in GITHUB_TOKEN
packages: write
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Job vars and prep
# Use bash string manipulation to create vars
# Note runner context isn't available in job.env, so use it here
# https://docs.github.com/en/actions/reference/context-and-expression-syntax-for-github-actions#context-availability
run: |
echo "SHA_SHORT=${GITHUB_SHA:0:10}" >> ${GITHUB_ENV}
echo "REPO_LOWER_CASE=${GITHUB_REPOSITORY,,}" >> ${GITHUB_ENV}
CACHE="${{ runner.temp }}/.buildx-cache"
echo "CACHE=${CACHE}" >> ${GITHUB_ENV}
echo "NEW_CACHE=${CACHE}-new" >> ${GITHUB_ENV}
mkdir -p ${CACHE}
- name: Setup Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Setup cache for Docker Buildx
id: setup-cache
uses: actions/cache@v2
with:
path: ${{ env.CACHE }}
key: flaskr-buildx-${{ github.ref_name }}
restore-keys: |
flaskr-buildx-main
- name: Login to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build Docker image and push to GHCR
uses: docker/build-push-action@v4
with:
context: examples/tutorial
push: true
tags: "ghcr.io/${{ env.REPO_LOWER_CASE }}:${{ env.SHA_SHORT }}"
cache-from: type=local,src=${{ env.CACHE }}
cache-to: type=local,dest=${{ env.NEW_CACHE }}
# Only save the latest Docker build layers to the cache, otherwise
# the cache will keep growing indefinitely, past the 10GB max
# https://github.com/docker/build-push-action/issues/252
# https://github.com/moby/buildkit/issues/1896
# - name: Only persist the new cache layers
# run: |
# rm -rf ${CACHE}
# mv ${NEW_CACHE} ${CACHE}
- name: Only persist the new cache layers
uses: actions/cache/save@v3
with:
path: ${{ env.NEW_CACHE }}
key: ${{ steps.setup-cache.outputs.cache-primary-key }}