add github actions to deploy docker on ghcr (2nd try) #50
Workflow file for this run
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: cicd_deploy | |
on: | |
pull_request: | |
# Run deployment tests on every new version tag and every push to main and dev: | |
# Run actual deploymment only on push to main and tags | |
branches: [ "main", "dev" ] | |
tags: | |
- v*.*.* | |
env: | |
IMAGE_NAME: ${{ github.repository }} | |
REGISTRY: ghcr.io | |
TEST_TAG: ${{ github.repository }}:test | |
jobs: | |
deploy_docker: | |
runs-on: ubuntu-latest | |
permissions: | |
contents: read | |
packages: write | |
steps: | |
# cf https://samuelsson.dev/access-private-submodules-in-github-actions/ for submodule fetching | |
- name: Checkout branch | |
uses: actions/checkout@v4 | |
with: | |
ssh-key: ${{ secrets.SUBMODULE_PULL_KEY }} | |
submodules: 'recursive' | |
# build the image | |
- name: Build Docker image for tests | |
id: build | |
uses: docker/build-push-action@v5 | |
with: | |
context: . # cf https://github.com/docker/build-push-action/issues/638 | |
load: true | |
tags: ${{ env.TEST_TAG }} | |
- name: Run tests in docker | |
run: > | |
docker run -v $(pwd)/data:/lidro/data --ipc=host ${{ env.TEST_TAG }} | |
python -m pytest test -m "not returnfile and not bduni" -s --log-cli-level INFO | |
- name: Set version number | |
run: | | |
echo "VERSION=v$(python -m lidro.version)" >> $GITHUB_ENV | |
- name: Check tag and version number consistency | |
if: ${{ github.event_name == 'tag' }} | |
run: | | |
if [[ ${{ github.ref_name }} == ${{ env.VERSION }} ]] | |
then | |
echo "OK: Tag name and VERSION.md (${{ env.VERSION }}) version number (${{ github.ref_name }}) match" | |
else | |
echo "NOK: Tag name and VERSION.md (${{ env.VERSION }}) version number (${{ github.ref_name }}) don't match" | |
exit 1 | |
fi | |
# Login against a Docker registry except on PR | |
# https://github.com/docker/login-action | |
- name: Log into registry ${{ env.REGISTRY }} | |
uses: docker/login-action@v2 | |
if: ${{ github.ref_name != 'dev' }} | |
with: | |
registry: ${{ env.REGISTRY }} | |
username: ${{ github.actor }} | |
password: ${{ secrets.GHCR_TOKEN }} | |
# Extract metadata (tags, labels) for Docker | |
# https://github.com/docker/metadata-action | |
- name: Extract Docker metadata | |
id: metadata | |
if: ${{ github.ref_name != 'dev' }} | |
uses: docker/metadata-action@v5 | |
with: | |
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} | |
# Build and push Docker image with Buildx (don't on PR) | |
# https://github.com/docker/build-push-action | |
- name: Build and push Docker image | |
id: build-and-push | |
if: ${{ github.ref_name != 'dev' }} | |
uses: docker/build-push-action@v5 | |
with: | |
context: . | |
push: true | |
tags: ${{ steps.meta.outputs.tags }} | |
labels: ${{ steps.meta.outputs.labels }} |