|
| 1 | +name: Publish Fluentd image |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + |
| 6 | +env: |
| 7 | + GHCR_REPO: 'ghcr.io/${{ github.repository }}/fluentd' |
| 8 | + DOCKERHUB_REPO: 'kubesphere/fluentd' |
| 9 | + |
| 10 | +permissions: |
| 11 | + contents: read |
| 12 | + packages: write |
| 13 | + |
| 14 | +jobs: |
| 15 | + get-version: |
| 16 | + name: Fetch fluentd version |
| 17 | + runs-on: ubuntu-latest |
| 18 | + outputs: |
| 19 | + VERSION: ${{ steps.get-version.outputs.VERSION }} |
| 20 | + steps: |
| 21 | + - name: Checkout code |
| 22 | + uses: actions/checkout@v4 |
| 23 | + with: |
| 24 | + ref: ${{ github.ref }} |
| 25 | + |
| 26 | + - name: Read fluentd version from VERSION file |
| 27 | + id: get-version |
| 28 | + run: | |
| 29 | + VERSION=$(cat cmd/fluent-watcher/fluentd/VERSION) |
| 30 | + echo "VERSION=${VERSION}" >> $GITHUB_OUTPUT |
| 31 | +
|
| 32 | + determine-tags: |
| 33 | + needs: [get-version] |
| 34 | + runs-on: ubuntu-latest |
| 35 | + name: Determine image tags |
| 36 | + outputs: |
| 37 | + IMAGE_BASE_TAG: ${{ steps.determine-tags.outputs.IMAGE_BASE_TAG }} |
| 38 | + IMAGE_MAJOR_MINOR: ${{ steps.determine-tags.outputs.IMAGE_MAJOR_MINOR }} |
| 39 | + |
| 40 | + steps: |
| 41 | + - name: Determine image version tag |
| 42 | + id: determine-tags |
| 43 | + run: | |
| 44 | + VERSION=${{ needs.get-version.outputs.VERSION }} |
| 45 | + VERSION_WITHOUT_V=${VERSION#v} |
| 46 | + MAJOR_MINOR=$(echo $VERSION_WITHOUT_V | cut -d. -f1-2) |
| 47 | +
|
| 48 | + if skopeo inspect docker://${{ env.GHCR_REPO }}:${VERSION}; then |
| 49 | + echo "${VERSION} tag already exists, assuming we're building a patch release!" |
| 50 | + LATEST_PATCH_VERSION=$(skopeo list-tags docker://${{ env.GHCR_REPO }} | grep -E "${VERSION}-[0-9]+" | sort | uniq | tail -1 | tr -d \" | cut -d'-' -f2 | tr -d ',') |
| 51 | + NEW_PATCH_VERSION=$((LATEST_PATCH_VERSION + 1)) |
| 52 | + IMAGE_BASE_TAG="${VERSION}-${NEW_PATCH_VERSION}" |
| 53 | + echo "Building patch release ${IMAGE_BASE_TAG}!" |
| 54 | + else |
| 55 | + echo "${VERSION} tag does not exist, assuming we're building a new release!" |
| 56 | + IMAGE_BASE_TAG="${VERSION}" |
| 57 | + fi |
| 58 | + |
| 59 | + echo "IMAGE_BASE_TAG=$IMAGE_BASE_TAG" >> $GITHUB_OUTPUT |
| 60 | + echo "IMAGE_MAJOR_MINOR=$MAJOR_MINOR" >> $GITHUB_OUTPUT |
| 61 | + |
| 62 | + build: |
| 63 | + name: Build/push image (${{ matrix.platform }}) |
| 64 | + runs-on: ${{ matrix.runs-on }} |
| 65 | + needs: [get-version,determine-tags] |
| 66 | + permissions: |
| 67 | + actions: read |
| 68 | + packages: write |
| 69 | + outputs: |
| 70 | + digest_amd64: ${{ steps.output-digests.outputs.amd64 }} |
| 71 | + digest_arm64: ${{ steps.output-digests.outputs.arm64 }} |
| 72 | + tags: ${{ steps.image-metadata.outputs.tags }} |
| 73 | + strategy: |
| 74 | + fail-fast: false |
| 75 | + matrix: |
| 76 | + platform: |
| 77 | + - linux/amd64 |
| 78 | + - linux/arm64 |
| 79 | + include: |
| 80 | + - runs-on: ubuntu-latest |
| 81 | + - runs-on: ubuntu-24.04-arm # Builds arm64 on arm64 hosts |
| 82 | + platform: linux/arm64 |
| 83 | + steps: |
| 84 | + - uses: actions/checkout@v4 |
| 85 | + |
| 86 | + - name: Set up Docker Buildx |
| 87 | + uses: docker/setup-buildx-action@v3 |
| 88 | + with: |
| 89 | + platforms: linux/amd64,linux/arm64 |
| 90 | + |
| 91 | + - name: Login to GHCR |
| 92 | + uses: docker/login-action@v3 |
| 93 | + with: |
| 94 | + registry: ghcr.io |
| 95 | + username: ${{ github.actor }} |
| 96 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 97 | + |
| 98 | + - name: Login to Docker Hub |
| 99 | + uses: docker/login-action@v3 |
| 100 | + with: |
| 101 | + registry: docker.io |
| 102 | + username: ${{ secrets.REGISTRY_USER }} |
| 103 | + password: ${{ secrets.REGISTRY_PASSWORD }} |
| 104 | + |
| 105 | + - name: Configure image tags |
| 106 | + id: image-metadata |
| 107 | + uses: docker/metadata-action@v5 |
| 108 | + with: |
| 109 | + images: | |
| 110 | + ${{ env.GHCR_REPO }} |
| 111 | + ${{ env.DOCKERHUB_REPO }} |
| 112 | + flavor: |
| 113 | + latest=false |
| 114 | + tags: | |
| 115 | + type=raw,value=latest |
| 116 | + type=raw,value=${{ needs.determine-tags.outputs.IMAGE_BASE_TAG }} |
| 117 | + type=raw,value=v${{ needs.determine-tags.outputs.IMAGE_BASE_TAG }} |
| 118 | + type=raw,value=${{ needs.determine-tags.outputs.IMAGE_MAJOR_MINOR }} |
| 119 | + type=raw,value=v${{ needs.determine-tags.outputs.IMAGE_MAJOR_MINOR }} |
| 120 | +
|
| 121 | + - name: Build and push image |
| 122 | + id: build |
| 123 | + uses: docker/build-push-action@v6 |
| 124 | + with: |
| 125 | + context: . |
| 126 | + file: cmd/fluent-watcher/fluentd/Dockerfile |
| 127 | + platforms: ${{ matrix.platform }} |
| 128 | + labels: ${{ steps.image-metadata.outputs.labels }} |
| 129 | + provenance: false |
| 130 | + build-args: |
| 131 | + FLUENTD_BASE_VERSION=${{ needs.get-version.outputs.VERSION }} |
| 132 | + outputs: type=image,"name=${{ env.GHCR_REPO }},${{ env.DOCKERHUB_REPO }}",push-by-digest=true,name-canonical=true,push=true |
| 133 | + |
| 134 | + - name: Output image digests |
| 135 | + id: output-digests |
| 136 | + run: | |
| 137 | + platform="${{ matrix.platform }}" |
| 138 | + # Convert "linux/amd64" to just amd64 for the output variable name |
| 139 | + arch=${platform#linux/} |
| 140 | + echo "${arch}=${{ steps.build.outputs.digest }}" >> $GITHUB_OUTPUT |
| 141 | +
|
| 142 | + manifest: |
| 143 | + name: Publish image manifest |
| 144 | + runs-on: ubuntu-latest |
| 145 | + needs: [build, determine-tags] |
| 146 | + steps: |
| 147 | + - name: Login to GHCR |
| 148 | + uses: docker/login-action@v3 |
| 149 | + with: |
| 150 | + registry: ghcr.io |
| 151 | + username: ${{ github.actor }} |
| 152 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 153 | + |
| 154 | + - name: Login to Docker Hub |
| 155 | + uses: docker/login-action@v3 |
| 156 | + with: |
| 157 | + registry: docker.io |
| 158 | + username: ${{ secrets.REGISTRY_USER }} |
| 159 | + password: ${{ secrets.REGISTRY_PASSWORD }} |
| 160 | + |
| 161 | + - name: Create image manifest |
| 162 | + uses: int128/docker-manifest-create-action@v2 |
| 163 | + with: |
| 164 | + push: true |
| 165 | + tags: ${{ needs.build.outputs.tags }} # Includes GHCR and Docker Hub |
| 166 | + sources: | |
| 167 | + ${{ env.GHCR_REPO }}@${{ needs.build.outputs.digest_amd64 }} |
| 168 | + ${{ env.GHCR_REPO }}@${{ needs.build.outputs.digest_arm64 }} |
0 commit comments