|
| 1 | +# Builds and publishes multi-platform Docker images after Python Build completes successfully. |
| 2 | +# Pushes to Docker Hub on version tags (v*) or dev branch. |
| 3 | +name: Build and Deploy Docker Image |
| 4 | + |
| 5 | +on: |
| 6 | + workflow_run: |
| 7 | + workflows: ["Python CI"] |
| 8 | + branches: ['**'] |
| 9 | + types: |
| 10 | + - completed |
| 11 | + |
| 12 | +# Cancel a currently running workflow from the same PR, branch or tag when a new workflow is triggered: |
| 13 | +concurrency: |
| 14 | + group: docker-${{ github.workflow }}-${{ github.event_name }}-${{ github.event.workflow_run.id || github.ref }} |
| 15 | + cancel-in-progress: true |
| 16 | + |
| 17 | +env: |
| 18 | + # github.repository as <account>/<repo> |
| 19 | + IMAGE_NAME: acockburn/appdaemon |
| 20 | + |
| 21 | +jobs: |
| 22 | + # After building the Python package, build the Docker image |
| 23 | + build_image: |
| 24 | + name: Docker image |
| 25 | + runs-on: ubuntu-latest |
| 26 | + # Only run if tests passed and it's dev branch or a version tag |
| 27 | + if: | |
| 28 | + github.event.workflow_run.conclusion == 'success' && |
| 29 | + ( |
| 30 | + github.event.workflow_run.head_branch == 'dev' || |
| 31 | + startsWith(github.event.workflow_run.head_branch, 'v') |
| 32 | + ) |
| 33 | + permissions: |
| 34 | + contents: read |
| 35 | + packages: write |
| 36 | + |
| 37 | + steps: |
| 38 | + - name: Checkout repository |
| 39 | + uses: actions/checkout@v6 |
| 40 | + - name: Download Python package |
| 41 | + uses: actions/download-artifact@v7 |
| 42 | + with: |
| 43 | + name: python-package |
| 44 | + path: dist/ |
| 45 | + run-id: ${{ github.event.workflow_run.id }} |
| 46 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 47 | + - name: Setup Docker buildx |
| 48 | + uses: docker/setup-buildx-action@v3.12.0 |
| 49 | + # Login against a Docker registry (only with a tag or push on `dev` branch) |
| 50 | + # https://github.com/docker/login-action |
| 51 | + - name: Log into Docker Hub |
| 52 | + uses: docker/login-action@v3.6.0 |
| 53 | + with: |
| 54 | + username: ${{ secrets.DOCKERHUB_USERNAME }} |
| 55 | + password: ${{ secrets.DOCKERHUB_PASSWORD }} |
| 56 | + # Extract metadata (tags, labels) for Docker |
| 57 | + # https://github.com/docker/metadata-action |
| 58 | + - name: Extract Docker metadata |
| 59 | + id: meta |
| 60 | + uses: docker/metadata-action@v5.10.0 |
| 61 | + with: |
| 62 | + images: ${{ env.IMAGE_NAME }} |
| 63 | + # Customize the generation of Docker `latest` tag |
| 64 | + # Tag with `latest` the git tags that do not have a "pre-release" component in the end (e.g. `3.0.0`) |
| 65 | + # Avoid tagging with `latest` the git tag that have a "pre-release" component in the end (e.g. `3.0.0b1`) |
| 66 | + # If no git tag, fallback to branch or PR name |
| 67 | + tags: | |
| 68 | + # If the git tag follows PEP440 conventions, use it as the resulting docker tag (both releases and pre-releases) |
| 69 | + type=pep440,pattern={{version}} |
| 70 | +
|
| 71 | + # 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` |
| 72 | + type=match,value=latest,pattern=pattern=^\d\.\d+\.\d+$ |
| 73 | +
|
| 74 | + # If no git tag is used, fallback to tagging with branch or PR name |
| 75 | + type=ref,event=branch |
| 76 | + type=ref,event=pr |
| 77 | +
|
| 78 | + # Build and push Docker image with Buildx (push image only with a tag or push on `dev` branch) |
| 79 | + # https://github.com/docker/build-push-action |
| 80 | + - name: Build and push Docker image |
| 81 | + id: build-and-push |
| 82 | + uses: docker/build-push-action@v6.18.0 |
| 83 | + with: |
| 84 | + context: . |
| 85 | + file: Dockerfile |
| 86 | + push: true |
| 87 | + tags: ${{ steps.meta.outputs.tags }} |
| 88 | + labels: ${{ steps.meta.outputs.labels }} |
| 89 | + platforms: linux/arm64/v8, linux/amd64, linux/arm/v7, linux/arm/v6 |
| 90 | + cache-from: type=gha |
| 91 | + cache-to: type=gha,mode=max |
0 commit comments