|
| 1 | +name: Build and Publish Docker Images |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + paths: |
| 6 | + - "**" |
| 7 | + - "!docs/**" |
| 8 | + - "!deploy/**" |
| 9 | + - "!setup.py" |
| 10 | + - "!.gitignore" |
| 11 | + - "!.github/workflows/**" |
| 12 | + - ".github/workflows/docker.yml" |
| 13 | + branches: |
| 14 | + - "main" |
| 15 | + tags: |
| 16 | + - "v?[0-9]+.[0-9]+.[0-9]*" |
| 17 | + workflow_dispatch: |
| 18 | + |
| 19 | +env: |
| 20 | + REGISTRY: ghcr.io |
| 21 | + |
| 22 | +jobs: |
| 23 | + prepare: |
| 24 | + runs-on: ubuntu-latest |
| 25 | + outputs: |
| 26 | + image-name: ${{ steps.image.outputs.name }} |
| 27 | + tags: ${{ steps.meta.outputs.tags }} |
| 28 | + labels: ${{ steps.meta.outputs.labels }} |
| 29 | + package-name: ${{ steps.package.outputs.name }} |
| 30 | + steps: |
| 31 | + - name: Checkout repository |
| 32 | + uses: actions/checkout@v4 |
| 33 | + |
| 34 | + - name: Set lowercase image name |
| 35 | + id: image |
| 36 | + run: | |
| 37 | + echo "name=${GITHUB_REPOSITORY,,}" >> $GITHUB_OUTPUT |
| 38 | +
|
| 39 | + - name: Set package name |
| 40 | + id: package |
| 41 | + run: | |
| 42 | + echo "name=$(basename ${GITHUB_REPOSITORY,,})" >> $GITHUB_OUTPUT |
| 43 | +
|
| 44 | + - name: Extract metadata |
| 45 | + id: meta |
| 46 | + uses: docker/metadata-action@v5 |
| 47 | + with: |
| 48 | + images: ${{ env.REGISTRY }}/${{ steps.image.outputs.name }} |
| 49 | + tags: | |
| 50 | + type=ref,event=branch,enable={{is_not_default_branch}} |
| 51 | + type=semver,pattern={{version}} |
| 52 | + type=semver,pattern={{major}}.{{minor}} |
| 53 | + type=raw,value=nightly,enable={{is_default_branch}} |
| 54 | +
|
| 55 | + - name: Compute final tags |
| 56 | + id: final-tags |
| 57 | + run: | |
| 58 | + readarray -t tags <<< "${{ steps.meta.outputs.tags }}" |
| 59 | +
|
| 60 | + if [[ "${{ github.ref_type }}" == "tag" ]]; then |
| 61 | + tag="${{ github.ref_name }}" |
| 62 | + if [[ "$tag" =~ ^v?[0-9]+\.[0-9]+\.[0-9]+$ ]]; then |
| 63 | + full_latest="${{ env.REGISTRY }}/${{ steps.image.outputs.name }}:latest" |
| 64 | + # Check if latest is already in tags to avoid duplicates |
| 65 | + if ! printf '%s\n' "${tags[@]}" | grep -q "^$full_latest$"; then |
| 66 | + tags+=("$full_latest") |
| 67 | + fi |
| 68 | + fi |
| 69 | + fi |
| 70 | +
|
| 71 | + # Set multiline output |
| 72 | + echo "tags<<EOF" >> $GITHUB_OUTPUT |
| 73 | + printf '%s\n' "${tags[@]}" >> $GITHUB_OUTPUT |
| 74 | + echo "EOF" >> $GITHUB_OUTPUT |
| 75 | +
|
| 76 | + build: |
| 77 | + needs: prepare |
| 78 | + runs-on: ${{ matrix.runner }} |
| 79 | + permissions: |
| 80 | + contents: read |
| 81 | + packages: write |
| 82 | + strategy: |
| 83 | + matrix: |
| 84 | + include: |
| 85 | + - platform: amd64 |
| 86 | + runner: ubuntu-latest |
| 87 | + suffix: amd64 |
| 88 | + cache-scope: amd64 |
| 89 | + - platform: arm64 |
| 90 | + runner: ubuntu-24.04-arm |
| 91 | + suffix: arm64 |
| 92 | + cache-scope: arm64 |
| 93 | + steps: |
| 94 | + - name: Checkout repository |
| 95 | + uses: actions/checkout@v4 |
| 96 | + |
| 97 | + - name: Set up Docker Buildx |
| 98 | + uses: docker/setup-buildx-action@v3 |
| 99 | + |
| 100 | + - name: Log in to GitHub Container Registry |
| 101 | + uses: docker/login-action@v3 |
| 102 | + with: |
| 103 | + registry: ${{ env.REGISTRY }} |
| 104 | + username: ${{ github.actor }} |
| 105 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 106 | + |
| 107 | + - name: Compute suffixed tags |
| 108 | + id: tags |
| 109 | + run: | |
| 110 | + readarray -t tags <<< "${{ needs.prepare.outputs.tags }}" |
| 111 | + suffixed=() |
| 112 | + for t in "${tags[@]}"; do |
| 113 | + suffixed+=("$t-${{ matrix.suffix }}") |
| 114 | + done |
| 115 | + echo "tags=$(IFS=','; echo "${suffixed[*]}")" >> $GITHUB_OUTPUT |
| 116 | +
|
| 117 | + - name: Build and push Docker image |
| 118 | + uses: docker/build-push-action@v5 |
| 119 | + with: |
| 120 | + context: . |
| 121 | + file: ./Dockerfile |
| 122 | + platforms: linux/${{ matrix.platform }} |
| 123 | + push: true |
| 124 | + tags: ${{ steps.tags.outputs.tags }} |
| 125 | + labels: ${{ needs.prepare.outputs.labels }} |
| 126 | + cache-from: type=gha,scope=${{ matrix.cache-scope }} |
| 127 | + cache-to: type=gha,mode=max,scope=${{ matrix.cache-scope }} |
| 128 | + provenance: false |
| 129 | + |
| 130 | + manifest: |
| 131 | + needs: [prepare, build] |
| 132 | + runs-on: ubuntu-latest |
| 133 | + permissions: |
| 134 | + contents: read |
| 135 | + packages: write |
| 136 | + steps: |
| 137 | + - name: Log in to GitHub Container Registry |
| 138 | + uses: docker/login-action@v3 |
| 139 | + with: |
| 140 | + registry: ${{ env.REGISTRY }} |
| 141 | + username: ${{ github.actor }} |
| 142 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 143 | + |
| 144 | + - name: Create and push multi-arch manifest |
| 145 | + run: | |
| 146 | + readarray -t tag_array <<< "${{ needs.prepare.outputs.tags }}" |
| 147 | +
|
| 148 | + for tag in "${tag_array[@]}"; do |
| 149 | + docker manifest create "$tag" \ |
| 150 | + "$tag-amd64" \ |
| 151 | + "$tag-arm64" |
| 152 | + |
| 153 | + docker manifest push "$tag" |
| 154 | + done |
0 commit comments