ci: update container and build files #70
This file contains hidden or 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: Publish Docker images | |
| on: | |
| push: | |
| branches: | |
| - 'main' | |
| - 'master' | |
| tags: | |
| - '*.*.*' | |
| paths: | |
| - 'Dockerfile*' | |
| - 'pyproject.toml' | |
| - 'poetry.lock' | |
| - 'requirements.txt' | |
| - '**.py' | |
| - '**.sh' | |
| - '.dockerignore' | |
| - '.env.example' | |
| - '.github/workflows/**' | |
| workflow_dispatch: | |
| # workflow_run: | |
| # workflows: ["Run pytest"] | |
| # types: | |
| # - completed | |
| env: | |
| REGISTRY_URL: ${{ vars.REGISTRY_URL }} | |
| REGISTRY_USER: ${{ vars.REGISTRY_USER }} | |
| jobs: | |
| push_to_registry: | |
| name: Push Docker image to container registry | |
| runs-on: ubuntu-latest | |
| strategy: | |
| fail-fast: true | |
| matrix: | |
| dockerfile: [Dockerfile] | |
| concurrency: | |
| # group: ${{ github.workflow }}-${{ matrix.dockerfile }}-${{ github.event.workflow_run.head_branch || github.ref }} | |
| group: ${{ github.workflow }}-${{ matrix.dockerfile }}-${{ github.head_ref || github.ref }} | |
| cancel-in-progress: true | |
| permissions: | |
| packages: write | |
| contents: read | |
| actions: read | |
| id-token: write | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Set password by container registry | |
| run: | | |
| case "${{ env.REGISTRY_URL }}" in | |
| "ghcr.io") | |
| echo "REGISTRY_PASS=${{ secrets.GITHUB_TOKEN }}" >> $GITHUB_ENV | |
| ;; | |
| *) | |
| if [ -n "${{ secrets.REGISTRY_PASS }}" ]; then | |
| echo "REGISTRY_PASS=${{ secrets.REGISTRY_PASS }}" >> $GITHUB_ENV | |
| else | |
| echo "REGISTRY_PASS secret is not set and registry is not recognized. Exiting..." | |
| exit 1 | |
| fi | |
| ;; | |
| esac | |
| - name: Log into container registry | |
| if: github.event_name != 'pull_request' | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ${{ env.REGISTRY_URL }} | |
| username: ${{ env.REGISTRY_USER }} | |
| password: ${{ env.REGISTRY_PASS }} | |
| - name: Set image name | |
| id: image_name | |
| run: | | |
| if [ -n "${{ env.IMAGE }}" ]; then | |
| IMAGE="${{ env.IMAGE }}" | |
| else | |
| IMAGE=$(grep "LABEL org.opencontainers.image.title" Dockerfile | cut -d'"' -f2) | |
| fi | |
| echo "IMAGE=$IMAGE" >> $GITHUB_OUTPUT | |
| echo "IMAGE=$IMAGE" >> $GITHUB_ENV | |
| - name: Docker meta | |
| id: meta | |
| uses: docker/metadata-action@v5 | |
| with: | |
| images: | | |
| ${{ env.REGISTRY_URL }}/${{ env.REGISTRY_USER }}/${{ steps.image_name.outputs.IMAGE }} | |
| tags: | | |
| type=schedule | |
| type=ref,event=branch | |
| type=ref,event=pr | |
| type=semver,pattern={{version}} | |
| type=semver,pattern={{major}}.{{minor}} | |
| type=semver,pattern={{major}} | |
| type=sha | |
| type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', github.event.repository.default_branch || 'main') }} | |
| - name: Setup QEMU | |
| uses: docker/setup-qemu-action@v3 | |
| - name: Setup Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Build and push | |
| uses: docker/build-push-action@v6 | |
| with: | |
| context: . | |
| file: ./${{ matrix.dockerfile }} | |
| push: ${{ github.event_name != 'pull_request' }} | |
| tags: ${{ steps.meta.outputs.tags }} | |
| labels: ${{ steps.meta.outputs.labels }} | |
| cache-from: type=registry,ref=${{ env.REGISTRY_URL }}/${{ env.REGISTRY_USER }}/${{ steps.image_name.outputs.IMAGE }}:buildcache | |
| cache-to: type=registry,ref=${{ env.REGISTRY_URL }}/${{ env.REGISTRY_USER }}/${{ steps.image_name.outputs.IMAGE }}:buildcache,mode=max |