From c569f0d59cca6e98a960d93e54f6df3bf224d0d5 Mon Sep 17 00:00:00 2001 From: cuisongliu Date: Mon, 13 Feb 2023 22:46:35 +0800 Subject: [PATCH] ci(main): fix pr controllers build (#2564) * ci(main): fix pr controllers build Signed-off-by: cuisongliu * ci(main): fix pr controllers build Signed-off-by: cuisongliu * ci(main): fix pr controllers build Signed-off-by: cuisongliu * ci(main): fix pr controllers build Signed-off-by: cuisongliu * ci(main): fix pr controllers build Signed-off-by: cuisongliu --------- Signed-off-by: cuisongliu --- .github/workflows/check-license.yml | 35 ++++++++++++ .github/workflows/ci.yml | 83 ++++++----------------------- .github/workflows/controllers.yml | 72 +++++++++++++++++++++++-- .github/workflows/coverage.yml | 39 ++++++++++++++ .github/workflows/e2e_metering.yml | 4 +- .github/workflows/format-code.yml | 42 +++++++++++++++ .github/workflows/services.yml | 73 ++++++++++++++++++++++--- .github/workflows/webhooks.yml | 74 ++++++++++++++++++++++--- scripts/resolve-modules.sh | 9 +++- 9 files changed, 343 insertions(+), 88 deletions(-) create mode 100755 .github/workflows/check-license.yml create mode 100644 .github/workflows/coverage.yml create mode 100755 .github/workflows/format-code.yml diff --git a/.github/workflows/check-license.yml b/.github/workflows/check-license.yml new file mode 100755 index 000000000..ad754bc79 --- /dev/null +++ b/.github/workflows/check-license.yml @@ -0,0 +1,35 @@ +name: Check-License + + +on: + workflow_dispatch: + push: + branches: [ "main" ] + paths: + - "!**/*.md" + - "!**/*.yaml" + pull_request: + branches: [ "*" ] + paths: + - "!**/*.md" + - "!**/*.yaml" +env: + # Common versions + GO_VERSION: "1.20" + +jobs: + check-license: + runs-on: ubuntu-20.04 + steps: + - name: Checkout + uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - name: Setup Go + uses: actions/setup-go@v3 + with: + go-version: ${{ env.GO_VERSION }} + + - name: Check License + run: make verify-license \ No newline at end of file diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d4beadfa9..856c860e0 100755 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -14,6 +14,9 @@ on: - "**/*.yaml" - "CONTRIBUTORS" - "CHANGELOG/**" + - "controllers/**" + - "service/**" + - "webhooks/**" pull_request: branches: ["*"] paths-ignore: @@ -23,47 +26,11 @@ on: - "**/*.yaml" - "CONTRIBUTORS" - "CHANGELOG/**" + - "controllers/**" + - "service/**" + - "webhooks/**" jobs: - format-code: - runs-on: ubuntu-20.04 - steps: - - name: Checkout - uses: actions/checkout@v3 - with: - repository: ${{ github.event.pull_request.head.repo.full_name }} - ref: ${{ github.head_ref }} - - - name: Setup Golang with cache - uses: magnetikonline/action-golang-cache@v3 - with: - go-version: ${{ env.GO_VERSION }} - - - name: Format Code - run: make format - - - name: Commit Changes - uses: stefanzweifel/git-auto-commit-action@v4 - with: - commit_message: "chore(fmt): format code" - commit_options: "-s" - - check-license: - runs-on: ubuntu-20.04 - steps: - - name: Checkout - uses: actions/checkout@v3 - with: - fetch-depth: 0 - - - name: Setup Go - uses: actions/setup-go@v3 - with: - go-version: ${{ env.GO_VERSION }} - - - name: Check License - run: make verify-license - resolve-modules: runs-on: ubuntu-20.04 outputs: @@ -77,7 +44,7 @@ jobs: run: sh ./scripts/resolve-modules.sh golangci-lint: - needs: [format-code, check-license, resolve-modules] + needs: [resolve-modules] runs-on: ubuntu-20.04 strategy: matrix: ${{ fromJson(needs.resolve-modules.outputs.matrix) }} @@ -100,29 +67,8 @@ jobs: working-directory: ${{ matrix.workdir }} args: "--out-${NO_FUTURE}format colored-line-number" - coverage: - needs: [format-code, check-license] - runs-on: ubuntu-20.04 - steps: - - name: Checkout - uses: actions/checkout@v3 - - - name: Setup Golang with cache - uses: magnetikonline/action-golang-cache@v3 - with: - go-version: ${{ env.GO_VERSION }} - - - name: Install Dependencies - run: sudo apt update && sudo apt install -y libgpgme-dev libbtrfs-dev libdevmapper-dev - - - name: Run Coverage - run: make coverage - - - name: Upload Coverage to Codecov - uses: codecov/codecov-action@v3 - build: - needs: [golangci-lint, coverage] + needs: [golangci-lint] runs-on: ubuntu-20.04 strategy: matrix: @@ -254,12 +200,15 @@ jobs: run: | TAG=dev echo tag_name=${TAG} >> $GITHUB_OUTPUT - - - name: set up buildx - id: buildx - uses: crazy-max/ghaction-docker-buildx@v2 + - + # Add support for more platforms with QEMU (optional) + # https://github.com/docker/setup-qemu-action + name: Set up QEMU + uses: docker/setup-qemu-action@v2 + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 with: - buildx-version: latest + driver-opts: network=host - name: Login to Docker Hub uses: docker/login-action@v2 diff --git a/.github/workflows/controllers.yml b/.github/workflows/controllers.yml index 5ef6fc23e..d3155ee18 100644 --- a/.github/workflows/controllers.yml +++ b/.github/workflows/controllers.yml @@ -6,14 +6,55 @@ on: branches: [ "main" ] paths: - "controllers/**" - - "!.github/workflows/controllers.yml" + - ".github/workflows/controllers.yml" - "!**/*.md" - "!**/*.yaml" + pull_request: + branches: [ "*" ] + paths: + - "controllers/**" + - ".github/workflows/controllers.yml" + - "!**/*.md" + - "!**/*.yaml" + env: # Common versions GO_VERSION: "1.20" jobs: + resolve-modules: + runs-on: ubuntu-20.04 + outputs: + matrix: ${{ steps.set-matrix.outputs.matrix }} + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Resolve Modules + id: set-matrix + run: sh ./scripts/resolve-modules.sh ./controllers + + golangci-lint: + needs: [ resolve-modules ] + runs-on: ubuntu-20.04 + strategy: + matrix: ${{ fromJson(needs.resolve-modules.outputs.matrix) }} + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Setup Golang with cache + uses: magnetikonline/action-golang-cache@v3 + with: + go-version: ${{ env.GO_VERSION }} + + - name: Run Linter + uses: golangci/golangci-lint-action@v3 + with: + version: latest + working-directory: ${{ matrix.workdir }} + args: "--out-${NO_FUTURE}format colored-line-number" + image-build: runs-on: ubuntu-latest strategy: @@ -53,20 +94,25 @@ jobs: TAG=dev echo tag_name=${TAG} >> $GITHUB_OUTPUT - - name: set up buildx - id: buildx - uses: crazy-max/ghaction-docker-buildx@v2 + - # Add support for more platforms with QEMU (optional) + # https://github.com/docker/setup-qemu-action + name: Set up QEMU + uses: docker/setup-qemu-action@v2 + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 with: - buildx-version: latest + driver-opts: network=host - name: Login to Docker Hub uses: docker/login-action@v2 + if: ${{ github.event_name == 'push' }} with: registry: ghcr.io username: ${{ github.repository_owner }} password: ${{ secrets.GH_TOKEN }} - name: build (and publish) ${{ matrix.module }} main image + if: ${{ github.event_name == 'push' }} env: # fork friendly ^^ DOCKER_REPO: ghcr.io/${{ github.repository_owner }}/sealos-${{ matrix.module }}-controller @@ -81,8 +127,24 @@ jobs: -t ${DOCKER_REPO}:${{ steps.prepare.outputs.tag_name }} \ -f Dockerfile \ . + - name: build ${{ matrix.module }} image + if: ${{ github.event_name == 'pull_request' }} + env: + # fork friendly ^^ + DOCKER_REPO: ghcr.io/${{ github.repository_owner }}/sealos-${{ matrix.module }}-controller + working-directory: controllers/${{ matrix.module }} + run: | + docker buildx build \ + --platform linux/amd64,linux/arm64 \ + --label "org.opencontainers.image.source=https://github.com/${{ github.repository_owner }}/sealos" \ + --label "org.opencontainers.image.description=sealos-${{ matrix.module }}-controller container image" \ + --label "org.opencontainers.image.licenses=MIT" \ + -t ${DOCKER_REPO}:${{ steps.prepare.outputs.tag_name }} \ + -f Dockerfile \ + . build-on-cluster-image: + if: ${{ github.event_name == 'push' }} needs: - image-build runs-on: ubuntu-latest diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml new file mode 100644 index 000000000..8f22b7507 --- /dev/null +++ b/.github/workflows/coverage.yml @@ -0,0 +1,39 @@ +name: Coverage + + +on: + workflow_dispatch: + push: + branches: [ "main" ] + paths: + - "!**/*.md" + - "!**/*.yaml" + pull_request: + branches: [ "*" ] + paths: + - "!**/*.md" + - "!**/*.yaml" +env: + # Common versions + GO_VERSION: "1.20" + +jobs: + coverage: + runs-on: ubuntu-20.04 + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Setup Golang with cache + uses: magnetikonline/action-golang-cache@v3 + with: + go-version: ${{ env.GO_VERSION }} + + - name: Install Dependencies + run: sudo apt update && sudo apt install -y libgpgme-dev libbtrfs-dev libdevmapper-dev + + - name: Run Coverage + run: make coverage + + - name: Upload Coverage to Codecov + uses: codecov/codecov-action@v3 \ No newline at end of file diff --git a/.github/workflows/e2e_metering.yml b/.github/workflows/e2e_metering.yml index b5ddaec8c..a81a9a170 100644 --- a/.github/workflows/e2e_metering.yml +++ b/.github/workflows/e2e_metering.yml @@ -46,7 +46,7 @@ jobs: run: | ls -l sealos build --debug -t ${DOCKER_REPO}:${{ steps.prepare.outputs.tag_name }}-cluster . - sealos login -u ${{ github.repository_owner }} -p ${{ secrets.GITHUB_TOKEN }} --debug ghcr.io + sealos login -u ${{ github.repository_owner }} -p ${{ secrets.GH_TOKEN }} --debug ghcr.io sealos push ${DOCKER_REPO}:${{ steps.prepare.outputs.tag_name }}-cluster - name: docker images run: | @@ -122,4 +122,4 @@ jobs: working-directory: ./controllers/metering run: | sudo -u root kubectl get pod -A - go test ./testdata/e2e/metering_test.go -v \ No newline at end of file + go test ./testdata/e2e/metering_test.go -v diff --git a/.github/workflows/format-code.yml b/.github/workflows/format-code.yml new file mode 100755 index 000000000..6265797c2 --- /dev/null +++ b/.github/workflows/format-code.yml @@ -0,0 +1,42 @@ +name: Format-Code + + +on: + workflow_dispatch: + push: + branches: [ "main" ] + paths: + - "!**/*.md" + - "!**/*.yaml" + pull_request: + branches: [ "*" ] + paths: + - "!**/*.md" + - "!**/*.yaml" +env: + # Common versions + GO_VERSION: "1.20" + +jobs: + format-code: + runs-on: ubuntu-20.04 + steps: + - name: Checkout + uses: actions/checkout@v3 + with: + repository: ${{ github.event.pull_request.head.repo.full_name }} + ref: ${{ github.head_ref }} + + - name: Setup Golang with cache + uses: magnetikonline/action-golang-cache@v3 + with: + go-version: ${{ env.GO_VERSION }} + + - name: Format Code + run: make format + + - name: Commit Changes + uses: stefanzweifel/git-auto-commit-action@v4 + with: + commit_message: "chore(fmt): format code" + commit_options: "-s" \ No newline at end of file diff --git a/.github/workflows/services.yml b/.github/workflows/services.yml index 6cb77cb24..b8c01c5bf 100644 --- a/.github/workflows/services.yml +++ b/.github/workflows/services.yml @@ -7,7 +7,14 @@ on: branches: [ "main" ] paths: - "service/**" - - "!.github/workflows/services.yml" + - ".github/workflows/services.yml" + - "!**/*.md" + - "!**/*.yaml" + pull_request: + branches: [ "*" ] + paths: + - "service/**" + - ".github/workflows/services.yml" - "!**/*.md" - "!**/*.yaml" env: @@ -15,6 +22,39 @@ env: GO_VERSION: "1.20" jobs: + resolve-modules: + runs-on: ubuntu-20.04 + outputs: + matrix: ${{ steps.set-matrix.outputs.matrix }} + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Resolve Modules + id: set-matrix + run: sh ./scripts/resolve-modules.sh ./service + + golangci-lint: + needs: [ resolve-modules ] + runs-on: ubuntu-20.04 + strategy: + matrix: ${{ fromJson(needs.resolve-modules.outputs.matrix) }} + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Setup Golang with cache + uses: magnetikonline/action-golang-cache@v3 + with: + go-version: ${{ env.GO_VERSION }} + + - name: Run Linter + uses: golangci/golangci-lint-action@v3 + with: + version: latest + working-directory: ${{ matrix.workdir }} + args: "--out-${NO_FUTURE}format colored-line-number" + image-build: runs-on: ubuntu-latest strategy: @@ -54,20 +94,25 @@ jobs: TAG=dev echo tag_name=${TAG} >> $GITHUB_OUTPUT - - name: set up buildx - id: buildx - uses: crazy-max/ghaction-docker-buildx@v2 + - # Add support for more platforms with QEMU (optional) + # https://github.com/docker/setup-qemu-action + name: Set up QEMU + uses: docker/setup-qemu-action@v2 + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 with: - buildx-version: latest + driver-opts: network=host - name: Login to Docker Hub uses: docker/login-action@v2 + if: ${{ github.event_name == 'push' }} with: registry: ghcr.io username: ${{ github.repository_owner }} password: ${{ secrets.GH_TOKEN }} - name: build (and publish) ${{ matrix.module }} main image + if: ${{ github.event_name == 'push' }} env: # fork friendly ^^ DOCKER_REPO: ghcr.io/${{ github.repository_owner }}/sealos-${{ matrix.module }}-service @@ -82,8 +127,24 @@ jobs: -t ${DOCKER_REPO}:${{ steps.prepare.outputs.tag_name }} \ -f Dockerfile \ . + - name: build ${{ matrix.module }} image + if: ${{ github.event_name == 'pull_request' }} + env: + # fork friendly ^^ + DOCKER_REPO: ghcr.io/${{ github.repository_owner }}/sealos-${{ matrix.module }}-service + working-directory: service/${{ matrix.module }} + run: | + docker buildx build \ + --platform linux/amd64,linux/arm64 \ + --label "org.opencontainers.image.source=https://github.com/${{ github.repository_owner }}/sealos" \ + --label "org.opencontainers.image.description=sealos-${{ matrix.module }}-service container image" \ + --label "org.opencontainers.image.licenses=MIT" \ + -t ${DOCKER_REPO}:${{ steps.prepare.outputs.tag_name }} \ + -f Dockerfile \ + . build-on-cluster-image: + if: ${{ github.event_name == 'push' }} needs: - image-build runs-on: ubuntu-latest @@ -98,7 +159,7 @@ jobs: if: github.repository == 'labring/sealos' with: issue-number: 140 - repository: labring/cluster-image + repository: labring-actions/cluster-image body: | /imagebuild_apps sealos-${{ matrix.module }}-service dev token: "${{ secrets.GH_TOKEN }}" diff --git a/.github/workflows/webhooks.yml b/.github/workflows/webhooks.yml index d2c73bc58..d0d0b1b40 100644 --- a/.github/workflows/webhooks.yml +++ b/.github/workflows/webhooks.yml @@ -6,7 +6,14 @@ on: branches: [ "main" ] paths: - "webhooks/**" - - "!.github/workflows/webhooks.yml" + - ".github/workflows/webhooks.yml" + - "!**/*.md" + - "!**/*.yaml" + pull_request: + branches: [ "*" ] + paths: + - "webhooks/**" + - ".github/workflows/webhooks.yml" - "!**/*.md" - "!**/*.yaml" env: @@ -14,6 +21,42 @@ env: GO_VERSION: "1.20" jobs: + resolve-modules: + runs-on: ubuntu-20.04 + outputs: + matrix: ${{ steps.set-matrix.outputs.matrix }} + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Resolve Modules + id: set-matrix + run: sh ./scripts/resolve-modules.sh ./webhooks + + golangci-lint: + needs: [ resolve-modules ] + runs-on: ubuntu-20.04 + strategy: + matrix: ${{ fromJson(needs.resolve-modules.outputs.matrix) }} + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Setup Golang with cache + uses: magnetikonline/action-golang-cache@v3 + with: + go-version: ${{ env.GO_VERSION }} + + - name: Install Dependencies + run: sudo apt update && sudo apt install -y libgpgme-dev libbtrfs-dev libdevmapper-dev + + - name: Run Linter + uses: golangci/golangci-lint-action@v3 + with: + version: latest + working-directory: ${{ matrix.workdir }} + args: "--out-${NO_FUTURE}format colored-line-number" + image-build: runs-on: ubuntu-latest strategy: @@ -53,20 +96,25 @@ jobs: TAG=dev echo tag_name=${TAG} >> $GITHUB_OUTPUT - - name: set up buildx - id: buildx - uses: crazy-max/ghaction-docker-buildx@v2 + - # Add support for more platforms with QEMU (optional) + # https://github.com/docker/setup-qemu-action + name: Set up QEMU + uses: docker/setup-qemu-action@v2 + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 with: - buildx-version: latest + driver-opts: network=host - name: Login to Docker Hub uses: docker/login-action@v2 + if: ${{ github.event_name == 'push' }} with: registry: ghcr.io username: ${{ github.repository_owner }} password: ${{ secrets.GH_TOKEN }} - name: build (and publish) ${{ matrix.module }} main image + if: ${{ github.event_name == 'push' }} env: # fork friendly ^^ DOCKER_REPO: ghcr.io/${{ github.repository_owner }}/sealos-${{ matrix.module }}-webhook @@ -81,7 +129,21 @@ jobs: -t ${DOCKER_REPO}:${{ steps.prepare.outputs.tag_name }} \ -f Dockerfile \ . - + - name: build ${{ matrix.module }} image + if: ${{ github.event_name == 'pull_request' }} + env: + # fork friendly ^^ + DOCKER_REPO: ghcr.io/${{ github.repository_owner }}/sealos-${{ matrix.module }}-webhook + working-directory: webhooks/${{ matrix.module }} + run: | + docker buildx build \ + --platform linux/amd64,linux/arm64 \ + --label "org.opencontainers.image.source=https://github.com/${{ github.repository_owner }}/sealos" \ + --label "org.opencontainers.image.description=sealos-${{ matrix.module }}-webhook container image" \ + --label "org.opencontainers.image.licenses=MIT" \ + -t ${DOCKER_REPO}:${{ steps.prepare.outputs.tag_name }} \ + -f Dockerfile \ + . # build-on-cluster-image: # needs: # - image-build diff --git a/scripts/resolve-modules.sh b/scripts/resolve-modules.sh index 20959990e..037ab8db7 100755 --- a/scripts/resolve-modules.sh +++ b/scripts/resolve-modules.sh @@ -1,8 +1,13 @@ #!/bin/bash # Recursively finds all directories with a go.mod file and creates # a GitHub Actions JSON output option. This is used by the linter action. +set -eu +FIND_VAR=${1:-.} echo "Resolving modules in $(pwd)" -PATHS=$(find . -type f -name go.mod -printf '{"workdir":"%h"},') -echo "::set-output name=matrix::{\"include\":[${PATHS%?}]}" +PATHS=$(find $FIND_VAR -type f -name go.mod -printf '{"workdir":"%h"},') +if [[ $FIND_VAR == "." ]]; then + PATHS=$(find $FIND_VAR ! -path './controllers/*' ! -path './webhooks' ! -path './service' -type f -name go.mod -printf '{"workdir":"%h"},') +fi +echo matrix="{\"include\":[${PATHS%?}]}" >> $GITHUB_OUTPUT