|
| 1 | +name: Build Service-RS Image |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + inputs: |
| 6 | + module: |
| 7 | + description: "Module name (e.g., httpgate)" |
| 8 | + required: true |
| 9 | + type: string |
| 10 | + push_image: |
| 11 | + description: "Push image" |
| 12 | + required: false |
| 13 | + type: boolean |
| 14 | + default: false |
| 15 | + push_image_tag: |
| 16 | + description: "Push image tag" |
| 17 | + default: "latest" |
| 18 | + required: false |
| 19 | + type: string |
| 20 | + |
| 21 | +env: |
| 22 | + DEFAULT_OWNER: "labring" |
| 23 | + ALIYUN_REGISTRY: ${{ secrets.ALIYUN_REGISTRY }} |
| 24 | + ALIYUN_REPO_PREFIX: ${{ secrets.ALIYUN_REPO_PREFIX && secrets.ALIYUN_REPO_PREFIX || secrets.ALIYUN_USERNAME && format('{0}/{1}', secrets.ALIYUN_REGISTRY, secrets.ALIYUN_USERNAME) || '' }} |
| 25 | + |
| 26 | +jobs: |
| 27 | + image-build: |
| 28 | + strategy: |
| 29 | + matrix: |
| 30 | + include: |
| 31 | + - arch: amd64 |
| 32 | + - arch: arm64 |
| 33 | + runs-on: ubuntu-24.04-arm |
| 34 | + runs-on: ${{ matrix.runs-on || 'ubuntu-24.04' }} |
| 35 | + permissions: |
| 36 | + contents: read |
| 37 | + packages: write |
| 38 | + steps: |
| 39 | + - name: Checkout |
| 40 | + uses: actions/checkout@v4 |
| 41 | + with: |
| 42 | + fetch-depth: 0 |
| 43 | + |
| 44 | + - name: Set image repo |
| 45 | + id: set_repo |
| 46 | + env: |
| 47 | + MODULE: ${{ inputs.module }} |
| 48 | + REPOSITORY_OWNER: ${{ github.repository_owner }} |
| 49 | + run: | |
| 50 | + echo "GHCR_REPO=ghcr.io/${REPOSITORY_OWNER}/sealos-${MODULE}-service" >> $GITHUB_ENV |
| 51 | + if [[ -n "${{ env.ALIYUN_REPO_PREFIX }}" ]]; then |
| 52 | + echo "ALIYUN_REPO=${{ env.ALIYUN_REPO_PREFIX }}/sealos-${MODULE}-service" >> $GITHUB_ENV |
| 53 | + fi |
| 54 | +
|
| 55 | + - name: Docker meta |
| 56 | + id: meta |
| 57 | + uses: docker/metadata-action@v5 |
| 58 | + with: |
| 59 | + images: | |
| 60 | + ${{ env.GHCR_REPO }} |
| 61 | + ${{ env.ALIYUN_REPO }} |
| 62 | + labels: | |
| 63 | + org.opencontainers.image.source=https://github.com/${{ github.repository }} |
| 64 | +
|
| 65 | + - name: Set up Docker Buildx |
| 66 | + uses: docker/setup-buildx-action@v3 |
| 67 | + |
| 68 | + - name: Login to Github Container Hub |
| 69 | + if: ${{ inputs.push_image }} |
| 70 | + uses: docker/login-action@v3 |
| 71 | + with: |
| 72 | + registry: ghcr.io |
| 73 | + username: ${{ github.repository_owner }} |
| 74 | + password: ${{ secrets.GHCR_TOKEN || secrets.GITHUB_TOKEN }} |
| 75 | + |
| 76 | + - name: Login to Aliyun Registry |
| 77 | + if: ${{ inputs.push_image && env.ALIYUN_REGISTRY }} |
| 78 | + uses: docker/login-action@v3 |
| 79 | + with: |
| 80 | + registry: ${{ env.ALIYUN_REGISTRY }} |
| 81 | + username: ${{ secrets.ALIYUN_USERNAME }} |
| 82 | + password: ${{ secrets.ALIYUN_PASSWORD }} |
| 83 | + |
| 84 | + - name: Build |
| 85 | + id: build |
| 86 | + uses: docker/build-push-action@v6 |
| 87 | + with: |
| 88 | + context: ./service-rs |
| 89 | + file: ./service-rs/${{ inputs.module }}/Dockerfile |
| 90 | + platforms: linux/${{ matrix.arch }} |
| 91 | + labels: ${{ steps.meta.outputs.labels }} |
| 92 | + outputs: type=image,"name=${{ env.GHCR_REPO }}${{ env.ALIYUN_REPO && format(',{0}', env.ALIYUN_REPO) || '' }}",name-canonical=true,push-by-digest=${{ inputs.push_image }},push=${{ inputs.push_image }} |
| 93 | + |
| 94 | + - name: Export digest |
| 95 | + env: |
| 96 | + TEMP_DIR: ${{ runner.temp }} |
| 97 | + DIGEST: ${{ steps.build.outputs.digest }} |
| 98 | + run: | |
| 99 | + mkdir -p "${TEMP_DIR}/digests" |
| 100 | + touch "${TEMP_DIR}/digests/${DIGEST#sha256:}" |
| 101 | +
|
| 102 | + - name: Upload digest |
| 103 | + uses: actions/upload-artifact@v4 |
| 104 | + with: |
| 105 | + name: digests-${{ inputs.module }}-${{ matrix.arch }} |
| 106 | + path: ${{ runner.temp }}/digests/* |
| 107 | + if-no-files-found: error |
| 108 | + retention-days: 1 |
| 109 | + |
| 110 | + image-release: |
| 111 | + name: Push Docker Images |
| 112 | + needs: image-build |
| 113 | + runs-on: ubuntu-24.04 |
| 114 | + permissions: |
| 115 | + contents: read |
| 116 | + packages: write |
| 117 | + if: ${{ inputs.push_image }} |
| 118 | + steps: |
| 119 | + - name: Checkout |
| 120 | + uses: actions/checkout@v4 |
| 121 | + with: |
| 122 | + fetch-depth: 0 |
| 123 | + |
| 124 | + - name: Login to Github Container Hub |
| 125 | + uses: docker/login-action@v3 |
| 126 | + with: |
| 127 | + registry: ghcr.io |
| 128 | + username: ${{ github.repository_owner }} |
| 129 | + password: ${{ secrets.GHCR_TOKEN || secrets.GITHUB_TOKEN }} |
| 130 | + |
| 131 | + - name: Login to Aliyun Registry |
| 132 | + if: ${{ env.ALIYUN_REGISTRY }} |
| 133 | + uses: docker/login-action@v3 |
| 134 | + with: |
| 135 | + registry: ${{ env.ALIYUN_REGISTRY }} |
| 136 | + username: ${{ secrets.ALIYUN_USERNAME }} |
| 137 | + password: ${{ secrets.ALIYUN_PASSWORD }} |
| 138 | + |
| 139 | + - name: Set image repo |
| 140 | + id: set_repo |
| 141 | + env: |
| 142 | + MODULE: ${{ inputs.module }} |
| 143 | + REPOSITORY_OWNER: ${{ github.repository_owner }} |
| 144 | + run: | |
| 145 | + echo "GHCR_REPO=ghcr.io/${REPOSITORY_OWNER}/sealos-${MODULE}-service" >> $GITHUB_ENV |
| 146 | + if [[ -n "${{ env.ALIYUN_REPO_PREFIX }}" ]]; then |
| 147 | + echo "ALIYUN_REPO=${{ env.ALIYUN_REPO_PREFIX }}/sealos-${MODULE}-service" >> $GITHUB_ENV |
| 148 | + fi |
| 149 | +
|
| 150 | + - name: Download digests |
| 151 | + uses: actions/download-artifact@v4 |
| 152 | + with: |
| 153 | + path: ${{ runner.temp }}/digests |
| 154 | + pattern: digests-${{ inputs.module }}-* |
| 155 | + merge-multiple: true |
| 156 | + |
| 157 | + - name: Set up Docker Buildx |
| 158 | + uses: docker/setup-buildx-action@v3 |
| 159 | + |
| 160 | + - name: Docker meta |
| 161 | + id: meta |
| 162 | + uses: docker/metadata-action@v5 |
| 163 | + with: |
| 164 | + images: | |
| 165 | + ${{ env.GHCR_REPO }} |
| 166 | + ${{ env.ALIYUN_REPO }} |
| 167 | + tags: | |
| 168 | + type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', 'main') }} |
| 169 | + type=raw,value=${{ inputs.push_image_tag }},enable=${{ inputs.push_image_tag != '' && inputs.push_image_tag != 'latest' }} |
| 170 | + type=ref,event=branch |
| 171 | + type=ref,event=tag |
| 172 | + type=sha |
| 173 | + labels: | |
| 174 | + org.opencontainers.image.source=https://github.com/${{ github.repository }} |
| 175 | + env: |
| 176 | + DOCKER_METADATA_SHORT_SHA_LENGTH: 9 |
| 177 | + |
| 178 | + - name: Create manifest list and push |
| 179 | + working-directory: ${{ runner.temp }}/digests |
| 180 | + env: |
| 181 | + GHCR_REPO: ${{ env.GHCR_REPO }} |
| 182 | + IMAGE_SOURCE: https://github.com/${{ github.repository }} |
| 183 | + run: | |
| 184 | + for TAG in $DOCKER_METADATA_OUTPUT_TAGS; do |
| 185 | + docker buildx imagetools create \ |
| 186 | + --annotation "index:org.opencontainers.image.source=${IMAGE_SOURCE}" \ |
| 187 | + -t $TAG \ |
| 188 | + $(printf "${GHCR_REPO}@sha256:%s " *) |
| 189 | + sleep 5 |
| 190 | + done |
| 191 | +
|
| 192 | + - name: Inspect image |
| 193 | + env: |
| 194 | + GHCR_REPO: ${{ env.GHCR_REPO }} |
| 195 | + IMAGE_VERSION: ${{ steps.meta.outputs.version }} |
| 196 | + run: | |
| 197 | + docker buildx imagetools inspect "${GHCR_REPO}:${IMAGE_VERSION}" |
| 198 | +
|
| 199 | + cluster-image-build: |
| 200 | + needs: |
| 201 | + - image-release |
| 202 | + runs-on: ubuntu-24.04 |
| 203 | + if: ${{ (github.event_name == 'push') || (github.event_name == 'create') || (inputs.push_image == true) }} |
| 204 | + permissions: |
| 205 | + contents: read |
| 206 | + packages: write |
| 207 | + steps: |
| 208 | + - name: Checkout |
| 209 | + uses: actions/checkout@v4 |
| 210 | + with: |
| 211 | + fetch-depth: 0 |
| 212 | + |
| 213 | + - name: Set image repo |
| 214 | + env: |
| 215 | + MODULE: ${{ inputs.module }} |
| 216 | + REPOSITORY_OWNER: ${{ github.repository_owner }} |
| 217 | + run: | |
| 218 | + echo "MODULE_NAME=${MODULE}" >> $GITHUB_ENV |
| 219 | + # Docker image repo (always use GHCR for values.yaml to avoid Aliyun bandwidth costs) |
| 220 | + echo "GHCR_DOCKER_REPO=ghcr.io/${REPOSITORY_OWNER}/sealos-${MODULE}-service" >> $GITHUB_ENV |
| 221 | + # Cluster image repos |
| 222 | + echo "GHCR_CLUSTER_REPO=ghcr.io/${REPOSITORY_OWNER}/sealos-cloud-${MODULE}-service" >> $GITHUB_ENV |
| 223 | + if [[ -n "${{ env.ALIYUN_REPO_PREFIX }}" ]]; then |
| 224 | + echo "ALIYUN_CLUSTER_REPO=${{ env.ALIYUN_REPO_PREFIX }}/sealos-cloud-${MODULE}-service" >> $GITHUB_ENV |
| 225 | + fi |
| 226 | +
|
| 227 | + - name: Docker meta for cluster image |
| 228 | + id: meta |
| 229 | + uses: docker/metadata-action@v5 |
| 230 | + with: |
| 231 | + images: | |
| 232 | + ${{ env.GHCR_CLUSTER_REPO }} |
| 233 | + ${{ env.ALIYUN_CLUSTER_REPO }} |
| 234 | + tags: | |
| 235 | + type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', 'main') }} |
| 236 | + type=raw,value=${{ inputs.push_image_tag }},enable=${{ inputs.push_image_tag != '' && inputs.push_image_tag != 'latest' }} |
| 237 | + type=ref,event=branch |
| 238 | + type=ref,event=tag |
| 239 | + type=sha |
| 240 | + env: |
| 241 | + DOCKER_METADATA_SHORT_SHA_LENGTH: 9 |
| 242 | + |
| 243 | + - name: Install sealos |
| 244 | + run: | |
| 245 | + sudo bash ./.github/scripts/install.sh |
| 246 | +
|
| 247 | + - name: Build ${{ env.MODULE_NAME }} cluster image |
| 248 | + working-directory: service-rs/${{ inputs.module }}/deploy |
| 249 | + env: |
| 250 | + MODULE_NAME: ${{ env.MODULE_NAME }} |
| 251 | + GHCR_DOCKER_REPO: ${{ env.GHCR_DOCKER_REPO }} |
| 252 | + IMAGE_SOURCE: https://github.com/${{ github.repository }} |
| 253 | + run: | |
| 254 | + # Build cluster images for each tag (amd64) |
| 255 | + for TAG in $DOCKER_METADATA_OUTPUT_TAGS; do |
| 256 | + # Update image in values.yaml - always use GHCR to avoid Aliyun bandwidth costs |
| 257 | + if [[ -f charts/${MODULE_NAME}/values.yaml ]]; then |
| 258 | + IMAGE_TAG="${TAG##*:}" |
| 259 | + echo "Updating image in charts/${MODULE_NAME}/values.yaml to ${GHCR_DOCKER_REPO}:${IMAGE_TAG}" |
| 260 | + sed -i "s|repository:.*|repository: ${GHCR_DOCKER_REPO}|" charts/${MODULE_NAME}/values.yaml |
| 261 | + sed -i "s|tag:.*|tag: \"${IMAGE_TAG}\"|" charts/${MODULE_NAME}/values.yaml |
| 262 | + fi |
| 263 | + sudo rm -rf registry |
| 264 | + echo "Building ${TAG}-amd64" |
| 265 | + sudo sealos build -t "${TAG}-amd64" --platform linux/amd64 --label "org.opencontainers.image.source=${IMAGE_SOURCE}" -f Kubefile |
| 266 | + done |
| 267 | +
|
| 268 | + # Build cluster images for each tag (arm64) |
| 269 | + for TAG in $DOCKER_METADATA_OUTPUT_TAGS; do |
| 270 | + if [[ -f charts/${MODULE_NAME}/values.yaml ]]; then |
| 271 | + IMAGE_TAG="${TAG##*:}" |
| 272 | + sed -i "s|repository:.*|repository: ${GHCR_DOCKER_REPO}|" charts/${MODULE_NAME}/values.yaml |
| 273 | + sed -i "s|tag:.*|tag: \"${IMAGE_TAG}\"|" charts/${MODULE_NAME}/values.yaml |
| 274 | + fi |
| 275 | + sudo rm -rf registry |
| 276 | + echo "Building ${TAG}-arm64" |
| 277 | + sudo sealos build -t "${TAG}-arm64" --platform linux/arm64 --label "org.opencontainers.image.source=${IMAGE_SOURCE}" -f Kubefile |
| 278 | + done |
| 279 | +
|
| 280 | + - name: Sealos login to ghcr.io |
| 281 | + env: |
| 282 | + REPOSITORY_OWNER: ${{ github.repository_owner }} |
| 283 | + GH_PAT: ${{ secrets.GHCR_TOKEN || secrets.GITHUB_TOKEN }} |
| 284 | + run: | |
| 285 | + sudo sealos login -u "$REPOSITORY_OWNER" -p "$GH_PAT" --debug ghcr.io |
| 286 | +
|
| 287 | + - name: Sealos login to Aliyun Registry |
| 288 | + if: ${{ env.ALIYUN_REGISTRY }} |
| 289 | + env: |
| 290 | + ALIYUN_USERNAME: ${{ secrets.ALIYUN_USERNAME }} |
| 291 | + ALIYUN_PASSWORD: ${{ secrets.ALIYUN_PASSWORD }} |
| 292 | + run: | |
| 293 | + sudo sealos login -u "$ALIYUN_USERNAME" -p "$ALIYUN_PASSWORD" --debug ${{ env.ALIYUN_REGISTRY }} |
| 294 | +
|
| 295 | + - name: Manifest Cluster Images |
| 296 | + run: | |
| 297 | + sudo sealos images |
| 298 | + for TAG in $DOCKER_METADATA_OUTPUT_TAGS; do |
| 299 | + echo "Creating manifest for ${TAG}" |
| 300 | + bash scripts/manifest-cluster-images.sh "$TAG" |
| 301 | + done |
0 commit comments