From eefba8d47c6d39a82b982cc6755e52b23f029f1d Mon Sep 17 00:00:00 2001 From: zsnmwy Date: Tue, 9 Aug 2022 01:10:14 +0800 Subject: [PATCH] ci: add build-images-branch Signed-off-by: zsnmwy --- .github/workflows/build-images.yml | 325 ++++++++++++++++++++++++----- Dockerfile | 6 +- Makefile | 6 +- 3 files changed, 281 insertions(+), 56 deletions(-) diff --git a/.github/workflows/build-images.yml b/.github/workflows/build-images.yml index 64d9ff5c5..22bf7f900 100644 --- a/.github/workflows/build-images.yml +++ b/.github/workflows/build-images.yml @@ -8,6 +8,15 @@ on: - 'pkg/**' - '**.go' - 'go.*' + - '**.yml' + push: + paths: + - 'cmd/**' + - 'pkg/**' + - '**.go' + - 'go.*' + - '**.yml' + workflow_dispatch: jobs: build-craned: @@ -21,18 +30,32 @@ jobs: - id: git-versions run: | echo "::set-output name=git-version::$(git describe --tags --always)" + - id: git-branch + run: | + echo "::set-output name=git-branch::$(echo ${GITHUB_REF##*/} | tr '[A-Z]' '[a-z]')" - id: build-name-image run: | echo "::set-output name=build-name-image::craned" - id: build-name-file run: | echo "::set-output name=build-name-file::$(echo "${{steps.build-name-image.outputs.build-name-image}}" | tr '/' '-')" + - name: Set up QEMU + uses: docker/setup-qemu-action@v2 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v1 - - - name: Build and export + - name: Inspect builder run: | - make image-craned + echo "Name: ${{ steps.buildx.outputs.name }}" + echo "Endpoint: ${{ steps.buildx.outputs.endpoint }}" + echo "Status: ${{ steps.buildx.outputs.status }}" + echo "Flags: ${{ steps.buildx.outputs.flags }}" + echo "Platforms: ${{ steps.buildx.outputs.platforms }}" + + - name: Login to Docker Hub + uses: docker/login-action@v2 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} - name: Login to Coding Container Registry uses: docker/login-action@v1 @@ -41,18 +64,51 @@ jobs: username: ${{ secrets.CODING_USERNAME }} password: ${{ secrets.CODING_PASSWORD }} - - name: Push image + - name: Login to GitHub Container Registry + uses: docker/login-action@v2 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - id: build-ldflags run: | - IMAGE_NAME=${{steps.build-name-image.outputs.build-name-image}} - IMAGE_ID=finops-docker.pkg.coding.net/gocrane/crane/$IMAGE_NAME + echo "::set-output name=build-ldflags::$(make echoLDFLAGS)" - # Change all uppercase to lowercase - IMAGE_ID=$(echo $IMAGE_ID | tr '[A-Z]' '[a-z]') - VERSION=${{steps.git-versions.outputs.git-version}} - echo IMAGE_ID=$IMAGE_ID - echo VERSION=$VERSION - docker tag gocrane/$IMAGE_NAME:$VERSION $IMAGE_ID:pr-${{github.event.number}}-$VERSION - docker push $IMAGE_ID:pr-${{github.event.number}}-$VERSION + - name: Build craned and push - push + if: ${{ github.event_name == 'push' }} + uses: docker/build-push-action@v3 + with: + context: . + platforms: linux/amd64,linux/arm64 + push: true + build-args: | + PKGNAME=craned + LDFLAGS=${{steps.build-ldflags.outputs.build-ldflags}} + BUILD=CI + tags: | + gocrane/${{steps.build-name-image.outputs.build-name-image}}:${{steps.git-branch.outputs.git-branch}}-${{steps.git-versions.outputs.git-version}} + gocrane/${{steps.build-name-image.outputs.build-name-image}}:${{steps.git-branch.outputs.git-branch}} + ghcr.io/gocrane/crane/${{steps.build-name-image.outputs.build-name-image}}:${{steps.git-branch.outputs.git-branch}}-${{steps.git-versions.outputs.git-version}} + ghcr.io/gocrane/crane/${{steps.build-name-image.outputs.build-name-image}}:${{steps.git-branch.outputs.git-branch}} + finops-docker.pkg.coding.net/gocrane/crane/${{steps.build-name-image.outputs.build-name-image}}:${{steps.git-branch.outputs.git-branch}}-${{steps.git-versions.outputs.git-version}} + finops-docker.pkg.coding.net/gocrane/crane/${{steps.build-name-image.outputs.build-name-image}}:${{steps.git-branch.outputs.git-branch}} + + - name: Build craned and push - pull_request_target + if: ${{ github.event_name == 'pull_request_target' }} + uses: docker/build-push-action@v3 + with: + context: . + platforms: linux/amd64,linux/arm64 + push: true + build-args: | + PKGNAME=craned + LDFLAGS=${{steps.build-ldflags.outputs.build-ldflags}} + BUILD=CI + tags: | + gocrane/${{steps.build-name-image.outputs.build-name-image}}:pr-${{github.event.number}}-${{steps.git-versions.outputs.git-version}} + ghcr.io/gocrane/crane/${{steps.build-name-image.outputs.build-name-image}}:pr-${{github.event.number}}-${{steps.git-versions.outputs.git-version}} + finops-docker.pkg.coding.net/gocrane/crane/${{steps.build-name-image.outputs.build-name-image}}:pr-${{github.event.number}}-${{steps.git-versions.outputs.git-version}} build-metric-adapter: runs-on: ubuntu-latest @@ -65,18 +121,32 @@ jobs: - id: git-versions run: | echo "::set-output name=git-version::$(git describe --tags --always)" + - id: git-branch + run: | + echo "::set-output name=git-branch::$(echo ${GITHUB_REF##*/} | tr '[A-Z]' '[a-z]')" - id: build-name-image run: | echo "::set-output name=build-name-image::metric-adapter" - id: build-name-file run: | echo "::set-output name=build-name-file::$(echo "${{steps.build-name-image.outputs.build-name-image}}" | tr '/' '-')" + - name: Set up QEMU + uses: docker/setup-qemu-action@v2 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v1 - - - name: Build and export + - name: Inspect builder run: | - make image-metric-adapter + echo "Name: ${{ steps.buildx.outputs.name }}" + echo "Endpoint: ${{ steps.buildx.outputs.endpoint }}" + echo "Status: ${{ steps.buildx.outputs.status }}" + echo "Flags: ${{ steps.buildx.outputs.flags }}" + echo "Platforms: ${{ steps.buildx.outputs.platforms }}" + + - name: Login to Docker Hub + uses: docker/login-action@v2 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} - name: Login to Coding Container Registry uses: docker/login-action@v1 @@ -85,18 +155,51 @@ jobs: username: ${{ secrets.CODING_USERNAME }} password: ${{ secrets.CODING_PASSWORD }} - - name: Push image + - name: Login to GitHub Container Registry + uses: docker/login-action@v2 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - id: build-ldflags run: | - IMAGE_NAME=${{steps.build-name-image.outputs.build-name-image}} - IMAGE_ID=finops-docker.pkg.coding.net/gocrane/crane/$IMAGE_NAME + echo "::set-output name=build-ldflags::$(make echoLDFLAGS)" - # Change all uppercase to lowercase - IMAGE_ID=$(echo $IMAGE_ID | tr '[A-Z]' '[a-z]') - VERSION=${{steps.git-versions.outputs.git-version}} - echo IMAGE_ID=$IMAGE_ID - echo VERSION=$VERSION - docker tag gocrane/$IMAGE_NAME:$VERSION $IMAGE_ID:pr-${{github.event.number}}-$VERSION - docker push $IMAGE_ID:pr-${{github.event.number}}-$VERSION + - name: Build crane-agent and push - push + if: ${{ github.event_name == 'push' }} + uses: docker/build-push-action@v3 + with: + context: . + platforms: linux/amd64,linux/arm64 + push: true + build-args: | + PKGNAME=metric-adapter + LDFLAGS=${{steps.build-ldflags.outputs.build-ldflags}} + BUILD=CI + tags: | + gocrane/${{steps.build-name-image.outputs.build-name-image}}:${{steps.git-branch.outputs.git-branch}}-${{steps.git-versions.outputs.git-version}} + gocrane/${{steps.build-name-image.outputs.build-name-image}}:${{steps.git-branch.outputs.git-branch}} + ghcr.io/gocrane/crane/${{steps.build-name-image.outputs.build-name-image}}:${{steps.git-branch.outputs.git-branch}}-${{steps.git-versions.outputs.git-version}} + ghcr.io/gocrane/crane/${{steps.build-name-image.outputs.build-name-image}}:${{steps.git-branch.outputs.git-branch}} + finops-docker.pkg.coding.net/gocrane/crane/${{steps.build-name-image.outputs.build-name-image}}:${{steps.git-branch.outputs.git-branch}}-${{steps.git-versions.outputs.git-version}} + finops-docker.pkg.coding.net/gocrane/crane/${{steps.build-name-image.outputs.build-name-image}}:${{steps.git-branch.outputs.git-branch}} + + - name: Build crane-agent and push - pull_request_target + if: ${{ github.event_name == 'pull_request_target' }} + uses: docker/build-push-action@v3 + with: + context: . + platforms: linux/amd64,linux/arm64 + push: true + build-args: | + PKGNAME=metric-adapter + LDFLAGS=${{steps.build-ldflags.outputs.build-ldflags}} + BUILD=CI + tags: | + gocrane/${{steps.build-name-image.outputs.build-name-image}}:pr-${{github.event.number}}-${{steps.git-versions.outputs.git-version}} + ghcr.io/gocrane/crane/${{steps.build-name-image.outputs.build-name-image}}:pr-${{github.event.number}}-${{steps.git-versions.outputs.git-version}} + finops-docker.pkg.coding.net/gocrane/crane/${{steps.build-name-image.outputs.build-name-image}}:pr-${{github.event.number}}-${{steps.git-versions.outputs.git-version}} build-crane-agent: runs-on: ubuntu-latest @@ -109,18 +212,32 @@ jobs: - id: git-versions run: | echo "::set-output name=git-version::$(git describe --tags --always)" + - id: git-branch + run: | + echo "::set-output name=git-branch::$(echo ${GITHUB_REF##*/} | tr '[A-Z]' '[a-z]')" - id: build-name-image run: | echo "::set-output name=build-name-image::crane-agent" - id: build-name-file run: | echo "::set-output name=build-name-file::$(echo "${{steps.build-name-image.outputs.build-name-image}}" | tr '/' '-')" + - name: Set up QEMU + uses: docker/setup-qemu-action@v2 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v1 - - - name: Build and export + - name: Inspect builder run: | - make image-crane-agent + echo "Name: ${{ steps.buildx.outputs.name }}" + echo "Endpoint: ${{ steps.buildx.outputs.endpoint }}" + echo "Status: ${{ steps.buildx.outputs.status }}" + echo "Flags: ${{ steps.buildx.outputs.flags }}" + echo "Platforms: ${{ steps.buildx.outputs.platforms }}" + + - name: Login to Docker Hub + uses: docker/login-action@v2 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} - name: Login to Coding Container Registry uses: docker/login-action@v1 @@ -129,18 +246,51 @@ jobs: username: ${{ secrets.CODING_USERNAME }} password: ${{ secrets.CODING_PASSWORD }} - - name: Push image + - name: Login to GitHub Container Registry + uses: docker/login-action@v2 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - id: build-ldflags run: | - IMAGE_NAME=${{steps.build-name-image.outputs.build-name-image}} - IMAGE_ID=finops-docker.pkg.coding.net/gocrane/crane/$IMAGE_NAME + echo "::set-output name=build-ldflags::$(make echoLDFLAGS)" - # Change all uppercase to lowercase - IMAGE_ID=$(echo $IMAGE_ID | tr '[A-Z]' '[a-z]') - VERSION=${{steps.git-versions.outputs.git-version}} - echo IMAGE_ID=$IMAGE_ID - echo VERSION=$VERSION - docker tag gocrane/$IMAGE_NAME:$VERSION $IMAGE_ID:pr-${{github.event.number}}-$VERSION - docker push $IMAGE_ID:pr-${{github.event.number}}-$VERSION + - name: Build crane-agent and push - push + uses: docker/build-push-action@v3 + if: ${{ github.event_name == 'push' }} + with: + context: . + platforms: linux/amd64,linux/arm64 + push: true + build-args: | + PKGNAME=crane-agent + LDFLAGS=${{steps.build-ldflags.outputs.build-ldflags}} + BUILD=CI + tags: | + gocrane/${{steps.build-name-image.outputs.build-name-image}}:${{steps.git-branch.outputs.git-branch}}-${{steps.git-versions.outputs.git-version}} + gocrane/${{steps.build-name-image.outputs.build-name-image}}:${{steps.git-branch.outputs.git-branch}} + ghcr.io/gocrane/crane/${{steps.build-name-image.outputs.build-name-image}}:${{steps.git-branch.outputs.git-branch}}-${{steps.git-versions.outputs.git-version}} + ghcr.io/gocrane/crane/${{steps.build-name-image.outputs.build-name-image}}:${{steps.git-branch.outputs.git-branch}} + finops-docker.pkg.coding.net/gocrane/crane/${{steps.build-name-image.outputs.build-name-image}}:${{steps.git-branch.outputs.git-branch}}-${{steps.git-versions.outputs.git-version}} + finops-docker.pkg.coding.net/gocrane/crane/${{steps.build-name-image.outputs.build-name-image}}:${{steps.git-branch.outputs.git-branch}} + + - name: Build crane-agent and push - pull_request_target + uses: docker/build-push-action@v3 + if: ${{ github.event_name == 'pull_request_target' }} + with: + context: . + platforms: linux/amd64,linux/arm64 + push: true + build-args: | + PKGNAME=crane-agent + LDFLAGS=${{steps.build-ldflags.outputs.build-ldflags}} + BUILD=CI + tags: | + gocrane/${{steps.build-name-image.outputs.build-name-image}}:pr-${{github.event.number}}-${{steps.git-versions.outputs.git-version}} + ghcr.io/gocrane/crane/${{steps.build-name-image.outputs.build-name-image}}:pr-${{github.event.number}}-${{steps.git-versions.outputs.git-version}} + finops-docker.pkg.coding.net/gocrane/crane/${{steps.build-name-image.outputs.build-name-image}}:pr-${{github.event.number}}-${{steps.git-versions.outputs.git-version}} build-dashboard: runs-on: ubuntu-latest @@ -153,18 +303,32 @@ jobs: - id: git-versions run: | echo "::set-output name=git-version::$(git describe --tags --always)" + - id: git-branch + run: | + echo "::set-output name=git-branch::$(echo ${GITHUB_REF##*/} | tr '[A-Z]' '[a-z]')" - id: build-name-image run: | echo "::set-output name=build-name-image::dashboard" - id: build-name-file run: | echo "::set-output name=build-name-file::$(echo "${{steps.build-name-image.outputs.build-name-image}}" | tr '/' '-')" + - name: Set up QEMU + uses: docker/setup-qemu-action@v2 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v1 - - - name: Build and export + - name: Inspect builder run: | - make image-dashboard + echo "Name: ${{ steps.buildx.outputs.name }}" + echo "Endpoint: ${{ steps.buildx.outputs.endpoint }}" + echo "Status: ${{ steps.buildx.outputs.status }}" + echo "Flags: ${{ steps.buildx.outputs.flags }}" + echo "Platforms: ${{ steps.buildx.outputs.platforms }}" + + - name: Login to Docker Hub + uses: docker/login-action@v2 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} - name: Login to Coding Container Registry uses: docker/login-action@v1 @@ -173,21 +337,42 @@ jobs: username: ${{ secrets.CODING_USERNAME }} password: ${{ secrets.CODING_PASSWORD }} - - name: Push image - run: | - IMAGE_NAME=${{steps.build-name-image.outputs.build-name-image}} - IMAGE_ID=finops-docker.pkg.coding.net/gocrane/crane/$IMAGE_NAME + - name: Login to GitHub Container Registry + uses: docker/login-action@v2 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} - # Change all uppercase to lowercase - IMAGE_ID=$(echo $IMAGE_ID | tr '[A-Z]' '[a-z]') - VERSION=${{steps.git-versions.outputs.git-version}} - echo IMAGE_ID=$IMAGE_ID - echo VERSION=$VERSION - docker tag gocrane/$IMAGE_NAME:$VERSION $IMAGE_ID:pr-${{github.event.number}}-$VERSION - docker push $IMAGE_ID:pr-${{github.event.number}}-$VERSION + - name: Build Dashboard and push - push to branch + if: ${{ github.event_name == 'push' }} + uses: docker/build-push-action@v3 + with: + context: ./pkg/web + platforms: linux/amd64,linux/arm64 + push: true + tags: | + gocrane/${{steps.build-name-image.outputs.build-name-image}}:${{steps.git-branch.outputs.git-branch}}-${{steps.git-versions.outputs.git-version}} + gocrane/${{steps.build-name-image.outputs.build-name-image}}:${{steps.git-branch.outputs.git-branch}} + ghcr.io/gocrane/crane/${{steps.build-name-image.outputs.build-name-image}}:${{steps.git-branch.outputs.git-branch}}-${{steps.git-versions.outputs.git-version}} + ghcr.io/gocrane/crane/${{steps.build-name-image.outputs.build-name-image}}:${{steps.git-branch.outputs.git-branch}} + finops-docker.pkg.coding.net/gocrane/crane/${{steps.build-name-image.outputs.build-name-image}}:${{steps.git-branch.outputs.git-branch}}-${{steps.git-versions.outputs.git-version}} + finops-docker.pkg.coding.net/gocrane/crane/${{steps.build-name-image.outputs.build-name-image}}:${{steps.git-branch.outputs.git-branch}} + - name: Build Dashboard and push - pull_request_target + if: ${{ github.event_name == 'pull_request_target' }} + uses: docker/build-push-action@v3 + with: + context: ./pkg/web + platforms: linux/amd64,linux/arm64 + push: true + tags: | + gocrane/${{steps.build-name-image.outputs.build-name-image}}:pr-${{github.event.number}}-${{steps.git-versions.outputs.git-version}} + ghcr.io/gocrane/crane/${{steps.build-name-image.outputs.build-name-image}}:pr-${{github.event.number}}-${{steps.git-versions.outputs.git-version}} + finops-docker.pkg.coding.net/gocrane/crane/${{steps.build-name-image.outputs.build-name-image}}:pr-${{github.event.number}}-${{steps.git-versions.outputs.git-version}} post-comment: runs-on: ubuntu-latest + if: ${{ github.event_name == 'pull_request_target' }} needs: - build-craned - build-metric-adapter @@ -205,12 +390,33 @@ jobs: - id: image-tags run: | echo "::set-output name=image-tags::pr-${{github.event.number}}-${{steps.git-versions.outputs.git-version}}" + - name: Get current date + id: date + run: echo "::set-output name=date::$(TZ='Asia/Shanghai' date +'%Y-%m-%d %H:%M')" - name: maintain-comment uses: actions-cool/maintain-one-comment@v2 with: token: ${{ secrets.GITHUB_TOKEN }} body: | 🎉 Successfully Build Images. + **Now Support ARM Platforms.** + Comment Post Time: ${{ steps.date.outputs.date }} + Git Version: ${{steps.git-versions.outputs.git-version}} + + #### Docker Registry + + Overview: https://hub.docker.com/u/gocrane + + | Image | Pull Command | + | --------------------------------------------- | --------------------------------------------------------- | + | crane-agent:${{steps.image-tags.outputs.image-tags}} | docker pull gocrane/crane-agent:${{steps.image-tags.outputs.image-tags}} | + | dashboard:${{steps.image-tags.outputs.image-tags}} | docker pull gocrane/dashboard:${{steps.image-tags.outputs.image-tags}} | + | metric-adapter:${{steps.image-tags.outputs.image-tags}} | docker pull gocrane/metric-adapter:${{steps.image-tags.outputs.image-tags}} | + | craned:${{steps.image-tags.outputs.image-tags}} | docker pull gocrane/craned:${{steps.image-tags.outputs.image-tags}} | + + --- + + #### Coding Registry Overview: https://finops.coding.net/public-artifacts/gocrane/crane/packages @@ -220,5 +426,18 @@ jobs: | dashboard:${{steps.image-tags.outputs.image-tags}} | docker pull finops-docker.pkg.coding.net/gocrane/crane/dashboard:${{steps.image-tags.outputs.image-tags}} | | metric-adapter:${{steps.image-tags.outputs.image-tags}} | docker pull finops-docker.pkg.coding.net/gocrane/crane/metric-adapter:${{steps.image-tags.outputs.image-tags}} | | craned:${{steps.image-tags.outputs.image-tags}} | docker pull finops-docker.pkg.coding.net/gocrane/crane/craned:${{steps.image-tags.outputs.image-tags}} | + + --- + + #### Ghcr Registry + + Overview: https://github.com/orgs/gocrane/packages?repo_name=crane + + | Image | Pull Command | + | --------------------------------------------- | --------------------------------------------------------- | + | crane-agent:${{steps.image-tags.outputs.image-tags}} | docker pull ghcr.io/gocrane/crane/crane-agent:${{steps.image-tags.outputs.image-tags}} | + | dashboard:${{steps.image-tags.outputs.image-tags}} | docker pull ghcr.io/gocrane/crane/dashboard:${{steps.image-tags.outputs.image-tags}} | + | metric-adapter:${{steps.image-tags.outputs.image-tags}} | docker pull ghcr.io/gocrane/crane/metric-adapter:${{steps.image-tags.outputs.image-tags}} | + | craned:${{steps.image-tags.outputs.image-tags}} | docker pull ghcr.io/gocrane/crane/craned:${{steps.image-tags.outputs.image-tags}} | body-include: '' diff --git a/Dockerfile b/Dockerfile index 46a1ce05c..00f6a4602 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,6 +5,7 @@ FROM golang:1.17.2-alpine as builder ARG LDFLAGS ARG PKGNAME +ARG BUILD WORKDIR /go/src/github.com/gocrane/crane # Copy the Go Modules manifests @@ -12,7 +13,8 @@ COPY go.mod go.mod COPY go.sum go.sum # cache deps before building and copying source so that we don't need to re-download as much # and so that source changes don't invalidate our downloaded layer -RUN go env -w GOPROXY=https://goproxy.io,direct +RUN if [[ "${BUILD}" != "CI" ]]; then go env -w GOPROXY=https://goproxy.io,direct; fi +RUN go env RUN go mod download # Copy the go source @@ -25,4 +27,4 @@ FROM alpine:3.13.5 RUN apk add --no-cache tzdata WORKDIR / ARG PKGNAME -COPY --from=builder /go/src/github.com/gocrane/crane/${PKGNAME} . \ No newline at end of file +COPY --from=builder /go/src/github.com/gocrane/crane/${PKGNAME} . diff --git a/Makefile b/Makefile index f5e303822..2ca673c9d 100644 --- a/Makefile +++ b/Makefile @@ -93,6 +93,10 @@ lint: golangci-lint ## Run golang lint against code test: fmt vet lint ## Run tests. go test -coverprofile coverage.out -covermode=atomic ./... +.PHONY: echoLDFLAGS +echoLDFLAGS: + @echo $(LDFLAGS) + .PHONY: build build: craned crane-agent metric-adapter @@ -236,4 +240,4 @@ ifeq (, $(shell which mockgen)) GO_MOCKGEN=$(shell go env GOPATH)/bin/mockgen else GO_MOCKGEN=$(shell which mockgen) -endif \ No newline at end of file +endif