From 56ca108d58ad5f4c35ddbe3973226380ae74da9b Mon Sep 17 00:00:00 2001 From: cong Date: Fri, 5 Jan 2024 16:53:03 +0800 Subject: [PATCH] try github ci --- .github/VERSION | 1 + .github/chart/values.yaml | 34 ++++++++++ .github/cr.yaml | 1 + .github/workflows/build.yaml | 121 ++++++++++++++++++++++++++++++++++ .github/workflows/test.yaml | 0 Dockerfile | 7 +- scripts/alb-dev-actions.sh | 1 + scripts/alb-github-actions.sh | 26 ++++++++ template/Dockerfile | 8 ++- 9 files changed, 195 insertions(+), 4 deletions(-) create mode 100644 .github/VERSION create mode 100644 .github/chart/values.yaml create mode 100644 .github/cr.yaml create mode 100644 .github/workflows/build.yaml create mode 100644 .github/workflows/test.yaml create mode 100644 scripts/alb-github-actions.sh diff --git a/.github/VERSION b/.github/VERSION new file mode 100644 index 00000000..60453e69 --- /dev/null +++ b/.github/VERSION @@ -0,0 +1 @@ +v1.0.0 \ No newline at end of file diff --git a/.github/chart/values.yaml b/.github/chart/values.yaml new file mode 100644 index 00000000..da01ce45 --- /dev/null +++ b/.github/chart/values.yaml @@ -0,0 +1,34 @@ +## global +operator: + albImagePullPolicy: IfNotPresent +defaultAlb: false # 部署默认的alb +operatorReplicas: 1 +operatorDeployMode: "deployment" # csv|deployment +global: + labelBaseDomain: cpaas.io + namespace: kube-system + registry: + address: theseedoaa + images: + alb2: + code: https://github.com/alauda/alb + support_arm: true + repository: alb + nginx: + code: https://github.com/alauda/alb + support_arm: true + repository: alb-nginx +resources: + alb: + limits: + cpu: "200m" + memory: 2Gi + requests: + cpu: 50m + memory: 128Mi + limits: + cpu: "2" + memory: 2Gi + requests: + cpu: 50m + memory: 128Mi diff --git a/.github/cr.yaml b/.github/cr.yaml new file mode 100644 index 00000000..4e1180df --- /dev/null +++ b/.github/cr.yaml @@ -0,0 +1 @@ +sign: false \ No newline at end of file diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml new file mode 100644 index 00000000..7ee92c05 --- /dev/null +++ b/.github/workflows/build.yaml @@ -0,0 +1,121 @@ +name: Build + +on: + pull_request: + branches: + - master + - release-* + paths-ignore: + - 'docs/**' + - '**.md' + - '.github/VERSION' + push: + branches: + - master + - release-* + paths-ignore: + - 'docs/**' + - '**.md' + - '.github/VERSION' + +concurrency: + group: "${{ github.workflow }}-${{ github.ref }}" + cancel-in-progress: true + +env: + GO_VERSION: '' + GOSEC_VERSION: '2.18.2' + HELM_VERSION: v3.13.3 + SUBMARINER_VERSION: '0.16.2' + IMAGE_REPO: "theseedoaa" +jobs: + build-alb: + name: Build alb + runs-on: ubuntu-22.04 + steps: + - uses: jlumbroso/free-disk-space@v1.3.1 + with: + android: true + dotnet: true + haskell: true + docker-images: false + large-packages: false + tool-cache: false + swap-storage: false + + - uses: actions/checkout@v4 + - name: version + uses: HardNorth/github-version-generate@v1.3.0 + with: + version-source: file + version-file: .github/VERSION + version-file-extraction-pattern: 'v(.+)' + - uses: docker/setup-buildx-action@v3 + - uses: azure/setup-helm@v3 + with: + version: '${{ env.HELM_VERSION }}' + - name: build + run: | + set -x + source ./scripts/alb-dev-actions.sh + export VERSION=$(alb-github-gen-version) + + OPENRESTY_BASE=theseedoaa/alb-nginx-base:v1.22.0 + GO_BUILD_BASE=golang:1.18.10-alpine3.16 + RUN_BASE=alpine:3.17.6 + + # build images + docker buildx build --platform linux/amd64 -t $IMAGE_REPO/alb:$VERSION --build-arg VERSION=$VERSION --build-arg RUN_BASE=$RUN_BASE --build-arg BUILD_BASE=$GO_BUILD_BASE -o type=docker -f ./Dockerfile . + + docker pull $OPENRESTY_BASE + docker buildx build --platform linux/amd64 -t $IMAGE_REPO/alb-nginx:$VERSION --build-arg VERSION=$VERSION --build-arg OPENRESTY_BASE=$OPENRESTY_BASE --build-arg BUILD_BASE=$GO_BUILD_BASE -o type=docker -f ./template/Dockerfile ./ + + docker images + + docker save $IMAGE_REPO/alb:$VERSION > alb.tar + docker save $IMAGE_REPO/alb-nginx:$VERSION > alb-nginx.tar + ls + env + # build chart + alb-build-github-chart $IMAGE_REPO $VERSION ./deploy/chart/alb ./alauda-alb2.tgz + - name: Upload alb images to artifact + uses: actions/upload-artifact@v3 + with: + name: alb + path: alb.tar + - name: Upload alb-nginx images to artifact + uses: actions/upload-artifact@v3 + with: + name: alb-nginx + path: alb-nginx.tar + - name: Upload chart's to artifact + uses: actions/upload-artifact@v3 + with: + name: alb-chart + path: alauda-alb2.tgz + - name: sync docker + env: + DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }} + DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }} + DOCKER_CLI_EXPERIMENTAL: enabled + if: github.event_name == 'pull_request' && github.event.action == 'closed' && github.event.pull_request.merged == true + run: | + source ./scripts/alb-dev-actions.sh + export VERSION=$(alb-github-gen-version) + echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin + docker push $IMAGE_REPO/alb:$VERSION + docker push $IMAGE_REPO/alb-nginx:$VERSION + - name: sync chart + if: github.event_name == 'pull_request' && github.event.action == 'closed' && github.event.pull_request.merged == true + uses: helm/chart-releaser-action@v1.6.0 + with: + charts_dir: ./deploy/chart/alb + config: .github/cr.yaml + env: + CR_TOKEN: "${{ secrets.GH_TOKEN }}" + - name: bump version + if: github.event_name == 'pull_request' && github.event.action == 'closed' && github.event.pull_request.merged == true + run: | + source ./scripts/alb-dev-actions.sh + git status + git diff \ No newline at end of file diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml new file mode 100644 index 00000000..e69de29b diff --git a/Dockerfile b/Dockerfile index cdd9aecf..455a5c39 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,7 @@ -FROM build-harbor.alauda.cn/ops/golang:1.18-alpine3.16 AS builder +ARG BUILD_BASE=build-harbor.alauda.cn/ops/golang:1.18-alpine3.16 +ARG RUN_BASE=build-harbor.alauda.cn/ops/alpine:3.17 + +FROM ${BUILD_BASE} AS builder ENV GO111MODULE=on ENV GOPROXY=https://goproxy.cn,direct @@ -11,7 +14,7 @@ RUN go build -buildmode=pie -ldflags '-w -s -linkmode=external -extldflags=-Wl,- RUN go build -buildmode=pie -ldflags '-w -s -linkmode=external -extldflags=-Wl,-z,relro,-z,now' -v -o /out/migrate/init-port-info alauda.io/alb2/migrate/init-port-info RUN go build -buildmode=pie -ldflags '-w -s -linkmode=external -extldflags=-Wl,-z,relro,-z,now' -v -o /out/operator alauda.io/alb2/cmd/operator -FROM build-harbor.alauda.cn/ops/alpine:3.17 AS base +FROM $RUN_BASE AS base RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories RUN apk update && apk add --no-cache iproute2 jq libcap && rm -rf /usr/bin/nc diff --git a/scripts/alb-dev-actions.sh b/scripts/alb-dev-actions.sh index 13027484..5e5e3ef9 100755 --- a/scripts/alb-dev-actions.sh +++ b/scripts/alb-dev-actions.sh @@ -4,6 +4,7 @@ source ./scripts/alb-test-actions.sh source ./scripts/alb-lint-actions.sh source ./scripts/alb-build-actions.sh source ./scripts/alb-deploy-actions.sh +source ./scripts/alb-github-actions.sh source ./template/actions/alb-nginx.sh function alb-cleanup() { diff --git a/scripts/alb-github-actions.sh b/scripts/alb-github-actions.sh new file mode 100644 index 00000000..d8452324 --- /dev/null +++ b/scripts/alb-github-actions.sh @@ -0,0 +1,26 @@ +#!/bin/bash +function alb-github-gen-version() { + local branch=$(echo "$GITHUB_HEAD_REF" | sed 's|/|-|g') + echo "v$CURRENT_VERSION-$branch.$(date +%s)" +} + +function alb-build-github-chart() { + # alb-build-github-chart $RELEASE_TAG ./chart/alb ./alb-chart.tar + local repo=$1 + local version=$2 + local chart_dir=$3 + local out_path=$4 + + local branch=$GITHUB_HEAD_REF + local commit=$GITHUB_SHA + cp ./.github/chart/values.yaml $chart_dir + yq -i e ".global.images.alb2.tag |= \"$version\"" $chart_dir/values.yaml + yq -i e ".global.registry.address |= \"$repo\"" $chart_dir/values.yaml + yq -i e ".global.images.nginx.tag |= \"$version\"" $chart_dir/values.yaml + yq -i e ".annotations.branch |= \"$branch\"" $chart_dir/Chart.yaml + yq -i e ".annotations.commit |= \"$commit\"" $chart_dir/Chart.yaml + + helm package --debug $chart_dir --version $version + mv ./alauda-alb2-$version.tgz $out_path + return +} diff --git a/template/Dockerfile b/template/Dockerfile index b07fcd6c..69b79d9a 100644 --- a/template/Dockerfile +++ b/template/Dockerfile @@ -1,5 +1,9 @@ + +ARG BUILD_BASE=build-harbor.alauda.cn/ops/golang:1.18-alpine3.16 +ARG OPENRESTY_BASE=build-harbor.alauda.cn/3rdparty/alb-nginx:v1.22.0 + # build nignx/openresty base image -FROM build-harbor.alauda.cn/ops/golang:1.18-alpine3.15 AS tweak_gen +FROM ${BUILD_BASE} AS tweak_gen COPY ./ /alb/ ENV GO111MODULE=on ENV GOPROXY=https://goproxy.cn,direct @@ -10,7 +14,7 @@ RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositorie RUN cd cmd/utils/tweak_gen && go build -buildmode=pie -ldflags '-w -s -linkmode=external -extldflags=-Wl,-z,relro,-z,now' -v -o /alb/out/tweak_gen alauda.io/alb2/cmd/utils/tweak_gen RUN ls -alh /alb/out/ -FROM build-harbor.alauda.cn/3rdparty/alb-nginx:v1.22.0 +FROM ${OPENRESTY_BASE} # keep it as same as pkg/config/albrun.go ENV OLD_CONFIG_PATH /etc/alb2/nginx/nginx.conf