From e7880c0b16569703a6f88aac8fbe270f7b57af13 Mon Sep 17 00:00:00 2001 From: zsnmwy <35299017+zsnmwy@users.noreply.github.com> Date: Fri, 22 Sep 2023 12:00:26 +0000 Subject: [PATCH] feat: support build arm images --- .github/workflows/build-images.yml | 173 +++++++++++++++++++++++++++++ Dockerfile | 2 +- Makefile | 4 + 3 files changed, 178 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/build-images.yml diff --git a/.github/workflows/build-images.yml b/.github/workflows/build-images.yml new file mode 100644 index 0000000..098ba4f --- /dev/null +++ b/.github/workflows/build-images.yml @@ -0,0 +1,173 @@ +name: build-images + +on: + pull_request_target: + types: [ opened, synchronize, reopened ] + paths: + - 'cmd/**' + - 'pkg/**' + - '**.go' + - 'go.*' + - '**.yml' + push: + paths: + - 'cmd/**' + - 'pkg/**' + - '**.go' + - 'go.*' + - '**.yml' + workflow_dispatch: + +jobs: + build-fadvisor: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v2 + with: + ref: ${{github.event.pull_request.head.ref}} + repository: ${{github.event.pull_request.head.repo.full_name}} + - 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::fadvisor" + - 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: Inspect builder + run: | + 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 + with: + registry: finops-docker.pkg.coding.net + username: ${{ secrets.CODING_USERNAME }} + password: ${{ secrets.CODING_PASSWORD }} + + - 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: | + echo "::set-output name=build-ldflags::$(make echoLDFLAGS)" + + - name: Build fadvisor 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=fadvisor + 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 fadvisor 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=fadvisor + 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}} + + + post-comment: + runs-on: ubuntu-latest + if: ${{ github.event_name == 'pull_request_target' }} + needs: + - build-fadvisor + steps: + - name: Checkout + uses: actions/checkout@v2 + with: + ref: ${{github.event.pull_request.head.ref}} + repository: ${{github.event.pull_request.head.repo.full_name}} + - id: git-versions + run: | + echo "::set-output name=git-version::$(git describe --tags --always)" + - 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 | + | --------------------------------------------- | --------------------------------------------------------- | + | fadvisor:${{steps.image-tags.outputs.image-tags}} | docker pull gocrane/fadvisor:${{steps.image-tags.outputs.image-tags}}| + + --- + + #### Coding Registry + + Overview: https://finops.coding.net/public-artifacts/gocrane/crane/packages + + | Image | Pull Command | + | --------------------------------------------- | --------------------------------------------------------- | + | fadvisor:${{steps.image-tags.outputs.image-tags}} | docker pull finops-docker.pkg.coding.net/gocrane/crane/fadvisor:${{steps.image-tags.outputs.image-tags}} | + + --- + + #### Ghcr Registry + + Overview: https://github.com/orgs/gocrane/packages?repo_name=crane + + | Image | Pull Command | + | --------------------------------------------- | --------------------------------------------------------- | + | fadvisor:${{steps.image-tags.outputs.image-tags}} | docker pull ghcr.io/gocrane/crane/fadvisor:${{steps.image-tags.outputs.image-tags}} | + + + body-include: '' \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 4ee2c9c..47c0080 100644 --- a/Dockerfile +++ b/Dockerfile @@ -22,7 +22,7 @@ COPY staging staging/ RUN go mod download # Build -RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="${LDFLAGS}" -a -o ${PKGNAME} /go/src/github.com/gocrane/fadvisor/cmd/${PKGNAME}/main.go +RUN go build -ldflags="${LDFLAGS}" -a -o ${PKGNAME} /go/src/github.com/gocrane/fadvisor/cmd/${PKGNAME}/main.go FROM alpine:3.13.5 WORKDIR / diff --git a/Makefile b/Makefile index 5e25147..a792a6b 100644 --- a/Makefile +++ b/Makefile @@ -56,6 +56,10 @@ help: ## Display this help. ##@ Development +.PHONY: echoLDFLAGS +echoLDFLAGS: + @echo $(LDFLAGS) + .PHONY: fmt fmt: ## Run go fmt against code. go fmt ./...