Build and Deploy Docker Image #36
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
| # Builds and publishes multi-platform Docker images after Python Build completes successfully. | |
| # Pushes to Docker Hub on version tags (v*) or dev branch. | |
| name: Build and Deploy Docker Image | |
| on: | |
| workflow_run: | |
| workflows: ["Python CI"] | |
| branches: ['**'] | |
| types: | |
| - completed | |
| # Cancel a currently running workflow from the same PR, branch or tag when a new workflow is triggered: | |
| concurrency: | |
| group: docker-${{ github.workflow }}-${{ github.event_name }}-${{ github.event.workflow_run.id || github.ref }} | |
| cancel-in-progress: true | |
| env: | |
| # github.repository as <account>/<repo> | |
| IMAGE_NAME: acockburn/appdaemon | |
| jobs: | |
| # After building the Python package, build the Docker image | |
| build_image: | |
| name: Docker image | |
| runs-on: ubuntu-latest | |
| # Only run if tests passed and it's dev branch or a version tag | |
| if: | | |
| github.event.workflow_run.conclusion == 'success' && | |
| ( | |
| github.event.workflow_run.head_branch == 'dev' || | |
| startsWith(github.event.workflow_run.head_branch, 'v') | |
| ) | |
| permissions: | |
| contents: read | |
| packages: write | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v6 | |
| - name: Download Python package | |
| uses: actions/download-artifact@v7 | |
| with: | |
| name: python-package | |
| path: dist/ | |
| run-id: ${{ github.event.workflow_run.id }} | |
| github-token: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Setup Docker buildx | |
| uses: docker/[email protected] | |
| # Login against a Docker registry (only with a tag or push on `dev` branch) | |
| # https://github.com/docker/login-action | |
| - name: Log into Docker Hub | |
| uses: docker/[email protected] | |
| with: | |
| username: ${{ secrets.DOCKERHUB_USERNAME }} | |
| password: ${{ secrets.DOCKERHUB_PASSWORD }} | |
| # Extract metadata (tags, labels) for Docker | |
| # https://github.com/docker/metadata-action | |
| - name: Extract Docker metadata | |
| id: meta | |
| uses: docker/[email protected] | |
| with: | |
| images: ${{ env.IMAGE_NAME }} | |
| # Customize the generation of Docker `latest` tag | |
| # Tag with `latest` the git tags that do not have a "pre-release" component in the end (e.g. `3.0.0`) | |
| # Avoid tagging with `latest` the git tag that have a "pre-release" component in the end (e.g. `3.0.0b1`) | |
| # If no git tag, fallback to branch or PR name | |
| tags: | | |
| # If the git tag follows PEP440 conventions, use it as the resulting docker tag (both releases and pre-releases) | |
| type=pep440,pattern={{version}} | |
| # If the git tag does NOT have a pre-release ending (e.g. `3.0.0`), it is a release version to be tagged as `latest` | |
| type=match,value=latest,pattern=pattern=^\d\.\d+\.\d+$ | |
| # If no git tag is used, fallback to tagging with branch or PR name | |
| type=ref,event=branch | |
| type=ref,event=pr | |
| # Build and push Docker image with Buildx (push image only with a tag or push on `dev` branch) | |
| # https://github.com/docker/build-push-action | |
| - name: Build and push Docker image | |
| id: build-and-push | |
| uses: docker/[email protected] | |
| with: | |
| context: . | |
| file: Dockerfile | |
| push: true | |
| tags: ${{ steps.meta.outputs.tags }} | |
| labels: ${{ steps.meta.outputs.labels }} | |
| platforms: linux/arm64/v8, linux/amd64, linux/arm/v7, linux/arm/v6 | |
| cache-from: type=gha | |
| cache-to: type=gha,mode=max |