|
| 1 | +name: Docker Build and Publish |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_run: |
| 5 | + workflows: ["Release Please"] |
| 6 | + types: |
| 7 | + - completed |
| 8 | + push: |
| 9 | + branches: [ main ] |
| 10 | + tags: |
| 11 | + - 'v*' |
| 12 | + pull_request: |
| 13 | + branches: [ main ] |
| 14 | + |
| 15 | +env: |
| 16 | + REGISTRY: ghcr.io |
| 17 | + IMAGE_NAME: echohello-dev/prometheus-toggl-track-exporter |
| 18 | + |
| 19 | +jobs: |
| 20 | + build: |
| 21 | + runs-on: ubuntu-latest |
| 22 | + permissions: |
| 23 | + contents: read |
| 24 | + packages: write |
| 25 | + outputs: |
| 26 | + is_release: ${{ steps.check-release.outputs.release_found }} |
| 27 | + release_tag: ${{ steps.check-release.outputs.release_tag }} |
| 28 | + |
| 29 | + steps: |
| 30 | + - name: Checkout repository |
| 31 | + uses: actions/checkout@v4 |
| 32 | + with: |
| 33 | + fetch-depth: 0 |
| 34 | + |
| 35 | + - name: Check for release |
| 36 | + id: check-release |
| 37 | + run: | |
| 38 | + LATEST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "no-tag") |
| 39 | + if [[ $LATEST_TAG =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then |
| 40 | + echo "Found release tag: $LATEST_TAG" |
| 41 | + echo "release_found=true" >> $GITHUB_OUTPUT |
| 42 | + echo "release_tag=$LATEST_TAG" >> $GITHUB_OUTPUT |
| 43 | + else |
| 44 | + echo "No release tag found" |
| 45 | + echo "release_found=false" >> $GITHUB_OUTPUT |
| 46 | + fi |
| 47 | +
|
| 48 | + - name: Cache asdf |
| 49 | + uses: actions/cache@v4 |
| 50 | + id: asdf-cache |
| 51 | + with: |
| 52 | + path: ~/.asdf |
| 53 | + key: ${{ runner.os }}-asdf-${{ hashFiles('.tool-versions') }} |
| 54 | + restore-keys: | |
| 55 | + ${{ runner.os }}-asdf- |
| 56 | +
|
| 57 | + - name: Cache Poetry |
| 58 | + uses: actions/cache@v4 |
| 59 | + id: poetry-cache |
| 60 | + with: |
| 61 | + path: ~/.cache/pypoetry |
| 62 | + key: ${{ runner.os }}-poetry-${{ hashFiles('**/poetry.lock') }} |
| 63 | + restore-keys: | |
| 64 | + ${{ runner.os }}-poetry- |
| 65 | +
|
| 66 | + - name: Install asdf |
| 67 | + uses: asdf-vm/actions/install@1117842ea70e2711a0072e3a71265cbfe2c830be # v0.16 |
| 68 | + |
| 69 | + - name: Setup asdf plugins |
| 70 | + run: | |
| 71 | + asdf plugin add python |
| 72 | + asdf plugin add poetry |
| 73 | + asdf plugin add task |
| 74 | +
|
| 75 | + - name: Install tools with asdf |
| 76 | + run: | |
| 77 | + asdf install |
| 78 | + pip install poetry |
| 79 | + asdf reshim python |
| 80 | +
|
| 81 | + - name: Install dependencies |
| 82 | + run: task install |
| 83 | + |
| 84 | + - name: Log in to the Container registry |
| 85 | + uses: docker/login-action@v3 |
| 86 | + with: |
| 87 | + registry: ${{ env.REGISTRY }} |
| 88 | + username: ${{ github.actor }} |
| 89 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 90 | + |
| 91 | + - name: Set up Docker Buildx |
| 92 | + uses: docker/setup-buildx-action@v3 |
| 93 | + |
| 94 | + - name: Extract metadata for Docker |
| 95 | + id: meta |
| 96 | + uses: docker/metadata-action@v5 |
| 97 | + with: |
| 98 | + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 99 | + tags: | |
| 100 | + type=raw,value=latest,enable=${{ steps.check-release.outputs.release_found == 'true' }} |
| 101 | + type=raw,value=${{ steps.check-release.outputs.release_tag }},enable=${{ steps.check-release.outputs.release_found == 'true' }} |
| 102 | + type=ref,event=branch |
| 103 | + type=ref,event=pr |
| 104 | + type=semver,pattern={{version}} |
| 105 | + type=semver,pattern={{major}}.{{minor}} |
| 106 | + type=sha,format=long |
| 107 | +
|
| 108 | + - name: Build and push Docker image |
| 109 | + uses: docker/build-push-action@v6 |
| 110 | + with: |
| 111 | + context: . |
| 112 | + push: ${{ github.event_name != 'pull_request' }} |
| 113 | + tags: ${{ steps.meta.outputs.tags }} |
| 114 | + labels: ${{ steps.meta.outputs.labels }} |
| 115 | + cache-from: type=gha |
| 116 | + cache-to: type=gha,mode=max |
0 commit comments