Updated GitHub workflow with managed concurrency #2
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 Build and Test | |
on: | |
pull_request: | |
branches: [ master ] | |
push: | |
branches: [ master ] | |
release: | |
types: [ published ] | |
# Add top-level concurrency to cancel any in-progress workflow runs for PRs | |
concurrency: | |
group: ${{ github.workflow }}-${{ github.ref }} | |
cancel-in-progress: true | |
jobs: | |
test: | |
runs-on: ubuntu-latest | |
# Add job-level concurrency for more granular control | |
concurrency: | |
group: test-${{ github.ref }} | |
cancel-in-progress: true | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Set up Python | |
uses: actions/setup-python@v4 | |
with: | |
python-version: '3.10' | |
- name: Install dependencies | |
run: | | |
python -m pip install --upgrade pip | |
pip install -r requirements-dev.txt | |
- name: Run tests | |
run: | | |
pytest --cov=app app/tests -v | |
build: | |
needs: test | |
runs-on: ubuntu-latest | |
# Add job-level concurrency for the build job | |
concurrency: | |
group: build-${{ github.ref }} | |
cancel-in-progress: true | |
permissions: | |
contents: read | |
packages: write | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v2 | |
- name: Login to GitHub Container Registry | |
if: github.event_name == 'release' | |
uses: docker/login-action@v2 | |
with: | |
registry: ghcr.io | |
username: ${{ github.actor }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- name: Extract metadata | |
id: meta | |
uses: docker/metadata-action@v4 | |
with: | |
images: ghcr.io/${{ github.repository }} | |
tags: | | |
type=ref,event=pr | |
type=ref,event=branch | |
type=semver,pattern={{version}},event=release | |
- name: Build and push | |
uses: docker/build-push-action@v4 | |
with: | |
context: . | |
push: ${{ github.event_name == 'release' }} | |
tags: ${{ steps.meta.outputs.tags }} | |
labels: ${{ steps.meta.outputs.labels }} | |
cache-from: type=gha | |
cache-to: type=gha,mode=max |