From 0628775de7840f6780bb5dc2b2238e1c66581b7b Mon Sep 17 00:00:00 2001 From: Gaurav Mehta Date: Mon, 22 Apr 2024 13:39:48 +1000 Subject: [PATCH] migration ci from drone to gh actions fixed up build issues include ci feedback remove copy to dist/artifacts --- .drone.yml | 73 ---------------------------- .github/workflows/master.yml | 15 ++++++ .github/workflows/pull-request.yml | 12 +++++ .github/workflows/tag.yml | 14 ++++++ .github/workflows/template-build.yml | 55 +++++++++++++++++++++ Dockerfile.dapper | 5 ++ package/Dockerfile | 14 +++++- scripts/build | 5 +- scripts/package | 4 +- 9 files changed, 120 insertions(+), 77 deletions(-) delete mode 100644 .drone.yml create mode 100644 .github/workflows/master.yml create mode 100644 .github/workflows/pull-request.yml create mode 100644 .github/workflows/tag.yml create mode 100644 .github/workflows/template-build.yml diff --git a/.drone.yml b/.drone.yml deleted file mode 100644 index f66f326..0000000 --- a/.drone.yml +++ /dev/null @@ -1,73 +0,0 @@ ---- -kind: pipeline -name: harvester-vm-import-controller - -platform: - os: linux - arch: amd64 - -steps: -- name: fetch - image: alpine/git - commands: - - git fetch --tags - when: - instance: - - drone-publish.rancher.io - ref: - - refs/head/master - - refs/tags/* - event: - - tag - -- name: build - image: rancher/dapper:v0.6.0 - commands: - - dapper ci - volumes: - - name: docker - path: /var/run/docker.sock - -- name: docker-publish-master - image: plugins/docker - settings: - build_args: - - VERSION=${DRONE_BRANCH}-${DRONE_COMMIT_SHA:0:8}-head - dockerfile: package/Dockerfile - password: - from_secret: docker_password - repo: "rancher/harvester-vm-import-controller" - tag: ${DRONE_BRANCH}-head - username: - from_secret: docker_username - when: - branch: main - event: - - push - -- name: docker-publish - image: plugins/docker - settings: - build_args: - - "VERSION=${DRONE_TAG}" - custom_dns: 1.1.1.1 - dockerfile: package/Dockerfile - password: - from_secret: docker_password - repo: "rancher/harvester-vm-import-controller" - tag: "${DRONE_TAG}" - username: - from_secret: docker_username - when: - instance: - - drone-publish.rancher.io - ref: - - refs/head/main - - refs/tags/* - event: - - tag - -volumes: -- name: docker - host: - path: /var/run/docker.sock \ No newline at end of file diff --git a/.github/workflows/master.yml b/.github/workflows/master.yml new file mode 100644 index 0000000..d5992ff --- /dev/null +++ b/.github/workflows/master.yml @@ -0,0 +1,15 @@ +name: Master Build and Publish + +on: + push: + branches: + - main + - release/v* + +jobs: + build-master: + uses: ./.github/workflows/template-build.yml + with: + release-tag-name: ${{ github.ref_name }}-head + push: true + secrets: inherit diff --git a/.github/workflows/pull-request.yml b/.github/workflows/pull-request.yml new file mode 100644 index 0000000..7d60020 --- /dev/null +++ b/.github/workflows/pull-request.yml @@ -0,0 +1,12 @@ +name: Pull Request Build + +on: + pull_request: + +jobs: + build-pr: + uses: ./.github/workflows/template-build.yml + with: + release-tag-name: pull-${{ github.event.number }} + push: false + secrets: inherit diff --git a/.github/workflows/tag.yml b/.github/workflows/tag.yml new file mode 100644 index 0000000..81a7ec0 --- /dev/null +++ b/.github/workflows/tag.yml @@ -0,0 +1,14 @@ +name: Tag Build and Publish + +on: + push: + tags: + - v* + +jobs: + build-tag: + uses: ./.github/workflows/template-build.yml + with: + release-tag-name: ${{ github.ref_name }} + push: true + secrets: inherit diff --git a/.github/workflows/template-build.yml b/.github/workflows/template-build.yml new file mode 100644 index 0000000..8c8cf14 --- /dev/null +++ b/.github/workflows/template-build.yml @@ -0,0 +1,55 @@ +on: + workflow_call: + inputs: + release-tag-name: + required: true + type: string + push: + required: true + type: boolean +env: + imageName: "rancher/harvester-vm-import-controller" + +jobs: + dapper-build: + runs-on: ubuntu-latest + permissions: + contents: read + id-token: write + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Run dapper + run: make ci + + - name: Read some Secrets + uses: rancher-eio/read-vault-secrets@main + if: ${{ inputs.push == true }} + with: + secrets: | + secret/data/github/repo/${{ github.repository }}/dockerhub/rancher/credentials username | DOCKER_USERNAME ; + secret/data/github/repo/${{ github.repository }}/dockerhub/rancher/credentials password | DOCKER_PASSWORD + + - name: Login to Docker Hub + uses: docker/login-action@v3 + if: ${{ inputs.push == true }} + with: + username: ${{ env.DOCKER_USERNAME }} + password: ${{ env.DOCKER_PASSWORD }} + + - name: Docker Build + uses: docker/build-push-action@v5 + with: + provenance: false + context: . + platforms: linux/amd64,linux/arm64 + file: package/Dockerfile + push: ${{ inputs.push }} + tags: ${{env.imageName}}:${{ inputs.release-tag-name }} \ No newline at end of file diff --git a/Dockerfile.dapper b/Dockerfile.dapper index db38564..dc1773d 100644 --- a/Dockerfile.dapper +++ b/Dockerfile.dapper @@ -16,6 +16,11 @@ RUN export K8S_VERSION=1.24.2 && \ RUN curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s v1.57.1 +# The docker version in dapper is too old to have buildx. Install it manually. +RUN curl -sSfL https://github.com/docker/buildx/releases/download/v0.13.1/buildx-v0.13.1.linux-${ARCH} -o buildx-v0.13.1.linux-${ARCH} && \ + chmod +x buildx-v0.13.1.linux-${ARCH} && \ + mv buildx-v0.13.1.linux-${ARCH} /usr/local/bin/buildx + ENV DAPPER_ENV REPO TAG DRONE_TAG CROSS ENV DAPPER_SOURCE /go/src/github.com/harvester/vm-import-controller ENV DAPPER_OUTPUT ./bin diff --git a/package/Dockerfile b/package/Dockerfile index 3a72f75..1cf1d7b 100644 --- a/package/Dockerfile +++ b/package/Dockerfile @@ -1,7 +1,19 @@ +# syntax=docker/dockerfile:1.7.0 + FROM registry.suse.com/bci/bci-base:15.5 RUN zypper -n rm container-suseconnect && \ zypper -n in qemu-tools + +ARG TARGETPLATFORM + +RUN if [ "$TARGETPLATFORM" != "linux/amd64" ] && [ "$TARGETPLATFORM" != "linux/arm64" ]; then \ + echo "Error: Unsupported TARGETPLATFORM: $TARGETPLATFORM" && \ + exit 1; \ + fi + +ENV ARCH=${TARGETPLATFORM#linux/} + COPY package/qemu-wrapper.sh /usr/bin/qemu-wrapper.sh -COPY bin/vm-import-controller /usr/bin/vm-import-controller +COPY bin/vm-import-controller-${ARCH} /usr/bin/vm-import-controller USER 1000 CMD ["vm-import-controller"] diff --git a/scripts/build b/scripts/build index 1b62bf2..01ca021 100755 --- a/scripts/build +++ b/scripts/build @@ -2,5 +2,6 @@ set -e cd $(dirname $0)/.. -CGO_ENABLED=0 go build -o bin/vm-import-controller . -mkdir -p bin \ No newline at end of file +mkdir -p bin +GOARCH=amd64 CGO_ENABLED=0 go build -o bin/vm-import-controller-amd64 . +GOARCH=arm64 CGO_ENABLED=0 go build -o bin/vm-import-controller-arm64 . \ No newline at end of file diff --git a/scripts/package b/scripts/package index 7ab7ead..0b30c05 100755 --- a/scripts/package +++ b/scripts/package @@ -7,5 +7,7 @@ cd $(dirname $0)/.. IMAGE=${REPO}/harvester-vm-import-controller:${TAG} DOCKERFILE=package/Dockerfile -docker build -f ${DOCKERFILE} -t ${IMAGE} . +buildx build --load \ + -f ${DOCKERFILE} -t ${IMAGE} . + echo Built ${IMAGE}