From 94b9fecc4e2a97a2b24cfb8a991033762677dc5b Mon Sep 17 00:00:00 2001 From: Gaurav Mehta Date: Wed, 24 Apr 2024 13:59:07 +1000 Subject: [PATCH] migrate from drone to gh actions (#29) * migrte from drone to gh actions * increase golangci-lint timeout * missing changes needed for cross-compilation and image builds --- .drone.yaml | 74 ---------------------------- .github/workflows/master.yml | 15 ++++++ .github/workflows/pull-request.yml | 12 +++++ .github/workflows/tag.yml | 14 ++++++ .github/workflows/template-build.yml | 57 +++++++++++++++++++++ Dockerfile.dapper | 6 ++- package/Dockerfile | 12 ++++- scripts/build | 3 +- scripts/package | 4 +- scripts/validate | 2 +- 10 files changed, 120 insertions(+), 79 deletions(-) delete mode 100644 .drone.yaml 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.yaml b/.drone.yaml deleted file mode 100644 index a5ac477..0000000 --- a/.drone.yaml +++ /dev/null @@ -1,74 +0,0 @@ ---- -kind: pipeline -name: harvester-seeder - -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 - privileged: true - 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-seeder" - 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-seeder" - 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 diff --git a/.github/workflows/master.yml b/.github/workflows/master.yml new file mode 100644 index 0000000..97e7ab3 --- /dev/null +++ b/.github/workflows/master.yml @@ -0,0 +1,15 @@ +name: Master Build and Publish + +on: + push: + branches: + - master + - 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..62bcdc2 --- /dev/null +++ b/.github/workflows/template-build.yml @@ -0,0 +1,57 @@ +on: + workflow_call: + inputs: + release-tag-name: + required: true + type: string + push: + required: true + type: boolean +env: + imageName: "rancher/harvester-seeder" + +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 }} + build-args: | + VERSION=${{ github.ref_name }}-${{ github.sha }}-head \ No newline at end of file diff --git a/Dockerfile.dapper b/Dockerfile.dapper index 2ff82f6..661d143 100644 --- a/Dockerfile.dapper +++ b/Dockerfile.dapper @@ -19,7 +19,11 @@ RUN if [ "${ARCH}" = "amd64" ]; then \ curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s v1.57.1; \ fi - +# 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 SKIPINDRONE ENV DAPPER_SOURCE /go/src/github.com/harvester/seeder ENV DAPPER_OUTPUT ./bin ./pkg/api ./config diff --git a/package/Dockerfile b/package/Dockerfile index db77123..1cfa248 100644 --- a/package/Dockerfile +++ b/package/Dockerfile @@ -1,8 +1,18 @@ +# syntax=docker/dockerfile:1.7.0 + # Build the manager binary FROM registry.suse.com/bci/bci-base:15.5 RUN zypper -n install ipmitool=1.8.18.238.gb7adc1d-150400.3.6.1 && zypper clean +ARG TARGETPLATFORM + +RUN if [ "$TARGETPLATFORM" != "linux/amd64" ] && [ "$TARGETPLATFORM" != "linux/arm64" ]; then \ + echo "Error: Unsupported TARGETPLATFORM: $TARGETPLATFORM" && \ + exit 1; \ + fi + +ENV ARCH=${TARGETPLATFORM#linux/} WORKDIR / -COPY bin/manager /bin/manager +COPY bin/manager-${ARCH} /bin/manager USER 65532:65532 ENTRYPOINT ["/bin/manager"] diff --git a/scripts/build b/scripts/build index 6dc3ce4..321b489 100755 --- a/scripts/build +++ b/scripts/build @@ -5,5 +5,6 @@ cd $(dirname $0)/.. go fmt ./... go vet ./... go generate ./... -CGO_ENABLED=0 go build -o bin/manager . +GOARCH=amd64 CGO_ENABLED=0 go build -o bin/manager-amd64 . +GOARCH=arm64 CGO_ENABLED=0 go build -o bin/manager-arm64 . mkdir -p bin \ No newline at end of file diff --git a/scripts/package b/scripts/package index 1c91350..dfcb26e 100755 --- a/scripts/package +++ b/scripts/package @@ -18,5 +18,7 @@ fi IMAGE=${REPO}/harvester-seeder:${TAG} DOCKERFILE=package/Dockerfile -docker build -f ${DOCKERFILE} -t ${IMAGE} . +buildx build --load \ + -f ${DOCKERFILE} -t ${IMAGE} . + echo Built ${IMAGE} \ No newline at end of file diff --git a/scripts/validate b/scripts/validate index 8c3b3f0..7ea8c9a 100755 --- a/scripts/validate +++ b/scripts/validate @@ -15,4 +15,4 @@ if [[ -z "$(command -v golangci-lint)" ]]; then fi echo "Running: golangci-lint run" -golangci-lint run \ No newline at end of file +golangci-lint run --timeout 10m \ No newline at end of file