From 425342977fcb373c6a7bd31cd0488cb9b15e9e01 Mon Sep 17 00:00:00 2001 From: PoAn Yang Date: Wed, 24 Apr 2024 16:29:55 +0800 Subject: [PATCH] ci: change to use GitHub actions Signed-off-by: PoAn Yang --- .github/workflows/build.yml | 94 +++++++++++++++++++++++++++++++++++++ Dockerfile.dapper | 7 ++- package/Dockerfile | 16 ++++++- scripts/build | 3 +- scripts/package | 10 ++-- 5 files changed, 120 insertions(+), 10 deletions(-) create mode 100644 .github/workflows/build.yml diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000000..342c530172 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,94 @@ +name: build +on: + push: + branches: + - master + - v* + tags: + - v* + pull_request: +jobs: + build: + name: Build binaries + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + ref: ${{ github.event.pull_request.head.sha }} + + # Build binaries + - name: Run ci + run: make ci + + - uses: codecov/codecov-action@v4 + with: + files: ./coverage.out + flags: unittests + token: ${{ secrets.CODECOV_TOKEN }} + + - name: Upload binaries + uses: actions/upload-artifact@v4 + with: + name: binaries_artifact + path: ./bin/* + + build_push_image: + name: Build and push image + runs-on: ubuntu-latest + needs: build + if: ${{ startsWith(github.ref, 'refs/heads/') || startsWith(github.ref, 'refs/tags/') }} + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Download binaries + uses: actions/download-artifact@v4 + with: + name: binaries_artifact + path: ./bin/ + + - name: Add executable permission + run: | + chmod +x ./bin/* + + - name: Copy bin folder to package + run: | + cp -r ./bin ./package/ + + # For multi-platform support + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Declare branch + run: | + echo "branch=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}" >> "$GITHUB_ENV" + + - name: Login to Docker Hub + uses: docker/login-action@v3 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + + # longhorn/longhorn-manager image + - name: docker-publish + if: ${{ startsWith(github.ref, 'refs/heads/') }} + uses: docker/build-push-action@v5 + with: + context: ./ + push: true + platforms: linux/amd64,linux/arm64 + tags: longhorn/longhorn-manager:${{ env.branch }}-head + file: package/Dockerfile + + - name: docker-publish-with-tag + if: ${{ startsWith(github.ref, 'refs/tags/') }} + uses: docker/build-push-action@v5 + with: + context: ./ + push: true + platforms: linux/amd64,linux/arm64 + tags: longhorn/longhorn-manager:${{ github.ref_name }} + file: package/Dockerfile diff --git a/Dockerfile.dapper b/Dockerfile.dapper index d98dd17769..229dc6019d 100644 --- a/Dockerfile.dapper +++ b/Dockerfile.dapper @@ -1,6 +1,6 @@ FROM registry.suse.com/bci/bci-base:15.6 -ARG DAPPER_HOST_ARCH=amd64 +ARG DAPPER_HOST_ARCH ARG http_proxy ARG https_proxy ENV HOST_ARCH=${DAPPER_HOST_ARCH} ARCH=${DAPPER_HOST_ARCH} @@ -20,6 +20,11 @@ RUN wget -O - https://storage.googleapis.com/golang/go1.21.3.linux-${!GOLANG_ARC RUN curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.55.2 +# The docker version in dapper is too old to have buildx. Install it manually. +RUN wget https://github.com/docker/buildx/releases/download/v0.13.1/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_SOURCE /go/src/github.com/longhorn/longhorn-manager ENV DAPPER_OUTPUT ./bin coverage.out ENV DAPPER_DOCKER_SOCKET true diff --git a/package/Dockerfile b/package/Dockerfile index f82c2cbdd0..fcf59036a2 100644 --- a/package/Dockerfile +++ b/package/Dockerfile @@ -1,10 +1,22 @@ +# syntax=docker/dockerfile:1.7.0 + FROM registry.suse.com/bci/bci-base:15.6 +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/bin/longhorn-manager-${ARCH} /usr/local/sbin/longhorn-manager + +COPY package/launch-manager package/nsmounter /usr/local/sbin/ + RUN zypper -n install iputils iproute2 nfs-client cifs-utils bind-utils e2fsprogs xfsprogs zip unzip && \ rm -rf /var/cache/zypp/* -COPY bin package/launch-manager package/nsmounter /usr/local/sbin/ - VOLUME /usr/local/sbin EXPOSE 9500 CMD ["launch-manager"] diff --git a/scripts/build b/scripts/build index 74a086ce20..f7b7091ab9 100755 --- a/scripts/build +++ b/scripts/build @@ -20,4 +20,5 @@ cd $(dirname $0)/.. mkdir -p bin -CGO_ENABLED=0 go build -o bin/longhorn-manager -ldflags "$LINKFLAGS $OTHER_LINKFLAGS" $COVER $COVERPKG +CGO_ENABLED=0 GOARCH=amd64 go build -o bin/longhorn-manager-amd64 -ldflags "$LINKFLAGS $OTHER_LINKFLAGS" $COVER $COVERPKG +CGO_ENABLED=0 GOARCH=arm64 go build -o bin/longhorn-manager-arm64 -ldflags "$LINKFLAGS $OTHER_LINKFLAGS" $COVER $COVERPKG diff --git a/scripts/package b/scripts/package index f276177104..8c118452fd 100755 --- a/scripts/package +++ b/scripts/package @@ -5,11 +5,7 @@ source $(dirname $0)/version cd $(dirname $0)/.. -ARCH=${ARCH:-amd64} -SUFFIX="" -[ "${ARCH}" != "amd64" ] && SUFFIX="_${ARCH}" - -TAG=${TAG:-${IMAGE_TAG_PREFIX}${SUFFIX}} +TAG=${TAG:-${IMAGE_TAG_PREFIX}} REPO=${REPO:-longhornio} IMAGE=${IMAGE:-${REPO}/longhorn-manager:${TAG}} @@ -17,13 +13,15 @@ if [ ! -e ./bin/longhorn-manager ]; then ./scripts/build fi +cp -r bin package/ + trap 'rm -rf ./package/bin' exit # update base image to get latest changes BASE_IMAGE=`grep FROM package/Dockerfile | awk '{print $2}'` docker pull ${BASE_IMAGE} -docker build -t ${IMAGE} -f package/Dockerfile . +buildx build --load -t ${IMAGE} -f package/Dockerfile . echo Built ${IMAGE}