From d6e758cdb1567b803deb8724298f26df6546fe08 Mon Sep 17 00:00:00 2001 From: PoAn Yang Date: Tue, 30 Apr 2024 11:41:18 +0800 Subject: [PATCH] ci: change to use GitHub actions Signed-off-by: PoAn Yang --- .github/workflows/build.yml | 92 +++++++++++++++++++++++++++++++++++++ Dockerfile.dapper | 10 +++- package/Dockerfile | 13 +++++- scripts/build | 3 +- scripts/package | 10 ++-- scripts/test | 2 +- 6 files changed, 119 insertions(+), 11 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 00000000..cc52e7da --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,92 @@ +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 + + # 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 binaries 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 }} + + # longhornio/upgrade-responder image + - name: docker-publish + if: ${{ startsWith(github.ref, 'refs/heads/') }} + uses: docker/build-push-action@v5 + with: + context: package/ + push: true + platforms: linux/amd64,linux/arm64 + tags: longhornio/upgrade-responder:${{ 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: package/ + push: true + platforms: linux/amd64,linux/arm64 + tags: longhornio/upgrade-responder:${{ github.ref_name }} + file: package/Dockerfile diff --git a/Dockerfile.dapper b/Dockerfile.dapper index 8bf9170a..1e4051da 100644 --- a/Dockerfile.dapper +++ b/Dockerfile.dapper @@ -1,11 +1,19 @@ FROM golang:1.22-alpine +ARG DAPPER_HOST_ARCH +ENV ARCH=${DAPPER_HOST_ARCH} + RUN apk -U add bash git gcc musl-dev docker vim less file curl wget ca-certificates 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_ENV REPO TAG DRONE_TAG ENV DAPPER_SOURCE /go/src/github.com/longhorn/upgrade-responder/ -ENV DAPPER_OUTPUT ./bin ./dist ./package +ENV DAPPER_OUTPUT ./bin ./dist ./package coverage.out ENV DAPPER_DOCKER_SOCKET true ENV HOME ${DAPPER_SOURCE} WORKDIR ${DAPPER_SOURCE} diff --git a/package/Dockerfile b/package/Dockerfile index a0f18133..714f6539 100644 --- a/package/Dockerfile +++ b/package/Dockerfile @@ -1,5 +1,16 @@ +# syntax=docker/dockerfile:1.7.0 + FROM ubuntu:20.04 + +ARG TARGETPLATFORM +RUN if [ "$TARGETPLATFORM" != "linux/amd64" ] && [ "$TARGETPLATFORM" != "linux/arm64" ]; then \ + echo "Error: Unsupported TARGETPLATFORM: $TARGETPLATFORM" && \ + exit 1; \ + fi + +ENV ARCH ${TARGETPLATFORM#linux/} + COPY GeoLite2-City.mmdb /usr/share/ -COPY upgrade-responder /usr/bin/ +COPY upgrade-responder-${ARCH} /usr/bin/upgrade-responder CMD ["upgrade-responder"] ENV GEODB /usr/share/GeoLite2-City.mmdb diff --git a/scripts/build b/scripts/build index 7d0799ad..48b16df1 100755 --- a/scripts/build +++ b/scripts/build @@ -7,4 +7,5 @@ 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/upgrade-responder +CGO_ENABLED=0 GOARCH=amd64 go build -ldflags "-X main.VERSION=$VERSION $LINKFLAGS" -o bin/upgrade-responder-amd64 +CGO_ENABLED=0 GOARCH=arm64 go build -ldflags "-X main.VERSION=$VERSION $LINKFLAGS" -o bin/upgrade-responder-arm64 diff --git a/scripts/package b/scripts/package index 7a34f12b..83a40372 100755 --- a/scripts/package +++ b/scripts/package @@ -3,23 +3,19 @@ set -e source $(dirname $0)/version -ARCH=${ARCH:-"amd64"} -SUFFIX="" -[ "${ARCH}" != "amd64" ] && SUFFIX="_${ARCH}" - cd $(dirname $0)/../package -TAG=${TAG:-${VERSION}${SUFFIX}} +TAG=${TAG:-${VERSION}} REPO=${REPO:-longhornio} if echo $TAG | grep -q dirty; then TAG=dev fi -cp ../bin/upgrade-responder . +cp ../bin/upgrade-responder-* . cp ../geodb/GeoLite2-City.mmdb . IMAGE=${REPO}/upgrade-responder:${TAG} -docker build -t ${IMAGE} . +buildx build --load -t ${IMAGE} . echo ${IMAGE} > ../dist/images echo Built ${IMAGE} diff --git a/scripts/test b/scripts/test index 5d45c8a5..537c0028 100755 --- a/scripts/test +++ b/scripts/test @@ -8,4 +8,4 @@ echo Running tests PACKAGES=". $(find -name '*.go' | xargs -I{} dirname {} | cut -f2 -d/ | sort -u | grep -Ev '(^\.$|.git|vendor|bin)' | sed -e 's!^!./!' -e 's!$!/...!')" [ "${ARCH}" == "amd64" ] && RACE=-race -go test ${RACE} -cover -tags=test ${PACKAGES} +go test ${RACE} -cover -tags=test ${PACKAGES} -coverprofile=coverage.out