From ccb929fa5109f3943880309b3a63d6a8e8e8153b Mon Sep 17 00:00:00 2001 From: Jian Wang Date: Mon, 3 Jun 2024 12:30:21 +0200 Subject: [PATCH] Move CI to github action Signed-off-by: Jian Wang --- .dockerignore | 2 + .drone.yml | 174 ----------------------------- .github/workflows/dev.yml | 14 +++ .github/workflows/factory.yml | 56 ++++++++++ .github/workflows/pull_request.yml | 13 +++ .github/workflows/release.yml | 14 +++ .gitignore | 1 - Dockerfile.dapper | 7 +- package/Dockerfile | 16 ++- scripts/build | 5 +- scripts/entry | 2 +- scripts/package | 6 +- 12 files changed, 127 insertions(+), 183 deletions(-) create mode 100644 .dockerignore delete mode 100644 .drone.yml create mode 100644 .github/workflows/dev.yml create mode 100644 .github/workflows/factory.yml create mode 100644 .github/workflows/pull_request.yml create mode 100644 .github/workflows/release.yml diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..2c64c3e5 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,2 @@ +./.dapper +./.cache diff --git a/.drone.yml b/.drone.yml deleted file mode 100644 index fa7798ad..00000000 --- a/.drone.yml +++ /dev/null @@ -1,174 +0,0 @@ ---- -kind: pipeline -name: amd64 - -platform: - os: linux - arch: amd64 - -steps: - - 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: - - ARCH=amd64 - - VERSION=${DRONE_BRANCH}-${DRONE_COMMIT_SHA:0:8}-head - dockerfile: package/Dockerfile - password: - from_secret: docker_password - repo: "rancher/harvester-cloud-provider" - tag: ${DRONE_BRANCH}-head-linux-amd64 - username: - from_secret: docker_username - when: - ref: - include: - - "refs/heads/master" - - "refs/heads/release/v*" - event: - - push - - - name: docker-publish - image: plugins/docker - settings: - dockerfile: package/Dockerfile - password: - from_secret: docker_password - repo: "rancher/harvester-cloud-provider" - tag: "${DRONE_TAG}-linux-amd64" - username: - from_secret: docker_username - when: - instance: - - drone-publish.rancher.io - ref: - - refs/head/master - - refs/tags/* - event: - - tag - -volumes: - - name: docker - host: - path: /var/run/docker.sock - ---- -kind: pipeline -name: arm64 - -platform: - os: linux - arch: arm64 - -steps: - - 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: - - ARCH=arm64 - - VERSION=${DRONE_BRANCH}-${DRONE_COMMIT_SHA:0:8}-head - dockerfile: package/Dockerfile - password: - from_secret: docker_password - repo: "rancher/harvester-cloud-provider" - tag: ${DRONE_BRANCH}-head-linux-arm64 - username: - from_secret: docker_username - when: - ref: - include: - - "refs/heads/master" - - "refs/heads/release/v*" - event: - - push - - - name: docker-publish - image: plugins/docker - settings: - dockerfile: package/Dockerfile - password: - from_secret: docker_password - repo: "rancher/harvester-cloud-provider" - tag: "${DRONE_TAG}-linux-arm64" - username: - from_secret: docker_username - when: - instance: - - drone-publish.rancher.io - ref: - - refs/head/master - - refs/tags/* - event: - - tag - -volumes: - - name: docker - host: - path: /var/run/docker.sock - ---- -kind: pipeline -name: manifest - -steps: - - name: push-manifest-head - image: plugins/manifest - settings: - username: - from_secret: docker_username - password: - from_secret: docker_password - target: "rancher/harvester-cloud-provider:${DRONE_BRANCH}-head" - template: "rancher/harvester-cloud-provider:${DRONE_BRANCH}-head-OS-ARCH" - ignore_missing: true - platforms: - - linux/amd64 - - linux/arm64 - when: - ref: - include: - - refs/heads/master - - refs/heads/release/v* - event: - - push - - - name: push-manifest-tag - image: plugins/manifest - settings: - username: - from_secret: docker_username - password: - from_secret: docker_password - target: "rancher/harvester-cloud-provider:${DRONE_TAG}" - template: "rancher/harvester-cloud-provider:${DRONE_TAG}-OS-ARCH" - ignore_missing: true - platforms: - - linux/amd64 - - linux/arm64 - when: - ref: - include: - - refs/head/master - - refs/tags/* - event: - - tag - -depends_on: - - amd64 - - arm64 diff --git a/.github/workflows/dev.yml b/.github/workflows/dev.yml new file mode 100644 index 00000000..437a6614 --- /dev/null +++ b/.github/workflows/dev.yml @@ -0,0 +1,14 @@ +name: Dev Build and Publish + +on: + push: + branches: + - master + +jobs: + build-for-dev: + uses: ./.github/workflows/factory.yml + with: + tag: ${{ github.ref_name }}-head + push: true + secrets: inherit diff --git a/.github/workflows/factory.yml b/.github/workflows/factory.yml new file mode 100644 index 00000000..1e6d5830 --- /dev/null +++ b/.github/workflows/factory.yml @@ -0,0 +1,56 @@ +on: + workflow_call: + inputs: + tag: + required: true + type: string + push: + required: true + type: boolean + +env: + repo: "rancher" + controllerImageName: "harvester-cloud-provider" + +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 (Controller) + uses: docker/build-push-action@v5 + with: + provenance: false + context: . + platforms: linux/amd64,linux/arm64 + file: package/Dockerfile + push: ${{ inputs.push }} + tags: ${{ env.repo }}/${{ env.controllerImageName }}:${{ inputs.tag }} diff --git a/.github/workflows/pull_request.yml b/.github/workflows/pull_request.yml new file mode 100644 index 00000000..39cfe337 --- /dev/null +++ b/.github/workflows/pull_request.yml @@ -0,0 +1,13 @@ +name: Pull Request Build + +on: + pull_request: + types: [opened, reopened, synchronize] + +jobs: + build-for-dev: + uses: ./.github/workflows/factory.yml + with: + tag: pr-${{ github.event.number }} + push: false + secrets: inherit diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 00000000..88207db3 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,14 @@ +name: Release Build and Publish + +on: + push: + tags: + - v** + +jobs: + build-for-release: + uses: ./.github/workflows/factory.yml + with: + tag: ${{ github.ref_name }} + push: true + secrets: inherit diff --git a/.gitignore b/.gitignore index dde93b6d..89cae5c8 100644 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,4 @@ /.dapper /.idea /bin -/dist /.nocalhost diff --git a/Dockerfile.dapper b/Dockerfile.dapper index 0f1002de..8890281b 100644 --- a/Dockerfile.dapper +++ b/Dockerfile.dapper @@ -10,9 +10,14 @@ RUN zypper -n rm container-suseconnect && \ ## install golangci-lint 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 ENV DAPPER_SOURCE /go/src/github.com/harvester/harvester-cloud-provider/ -ENV DAPPER_OUTPUT ./bin ./dist +ENV DAPPER_OUTPUT ./bin ENV DAPPER_DOCKER_SOCKET true ENV HOME ${DAPPER_SOURCE} WORKDIR ${DAPPER_SOURCE} diff --git a/package/Dockerfile b/package/Dockerfile index f1807701..6e60a6f3 100644 --- a/package/Dockerfile +++ b/package/Dockerfile @@ -1,3 +1,17 @@ +# syntax=docker/dockerfile:1.7.0 + FROM registry.suse.com/bci/bci-minimal:15.5 -COPY bin/harvester-cloud-provider /usr/bin/ + +ARG TARGETPLATFORM + +RUN if [ "$TARGETPLATFORM" != "linux/amd64" ] && [ "$TARGETPLATFORM" != "linux/arm64" ]; then \ + echo "Error: Unsupported TARGETPLATFORM: $TARGETPLATFORM" && \ + exit 1; \ + fi + +ENV ARCH=${TARGETPLATFORM#linux/} + +COPY bin/harvester-cloud-provider-${ARCH} /usr/bin/harvester-cloud-provider + + CMD ["harvester-cloud-provider"] diff --git a/scripts/build b/scripts/build index 946d1c46..76bbf6e0 100755 --- a/scripts/build +++ b/scripts/build @@ -7,4 +7,7 @@ cd $(dirname $0)/.. mkdir -p bin [ "$(uname)" != "Darwin" ] && LINKFLAGS="-extldflags -static -s" -CGO_ENABLED=0 go build -ldflags "-X main.VERSION=$VERSION $LINKFLAGS" -o bin/harvester-cloud-provider + +for arch in "amd64" "arm64"; do + GOARCH="$arch" CGO_ENABLED=0 go build -ldflags "-X main.VERSION=$VERSION $LINKFLAGS" -o bin/harvester-cloud-provider-"$arch" +done diff --git a/scripts/entry b/scripts/entry index 78fb5679..c58311c9 100755 --- a/scripts/entry +++ b/scripts/entry @@ -1,7 +1,7 @@ #!/bin/bash set -e -mkdir -p bin dist +mkdir -p bin if [ -e ./scripts/$1 ]; then ./scripts/"$@" else diff --git a/scripts/package b/scripts/package index 1d798898..5b817691 100755 --- a/scripts/package +++ b/scripts/package @@ -15,14 +15,12 @@ if echo $TAG | grep -q dirty; then TAG=dev fi -mkdir -p dist/artifacts -cp bin/harvester-cloud-provider dist/artifacts/harvester-cloud-provider${SUFFIX} - IMAGE=${REPO}/harvester-cloud-provider:${TAG} DOCKERFILE=package/Dockerfile if [ -e ${DOCKERFILE}.${ARCH} ]; then DOCKERFILE=${DOCKERFILE}.${ARCH} fi -docker build -f ${DOCKERFILE} -t ${IMAGE} . +#docker build -f ${DOCKERFILE} -t ${IMAGE} . +buildx build --load -f ${DOCKERFILE} -t ${IMAGE} . echo Built ${IMAGE}