From d6efffdd98f26b6fa3643593ac24537e0e11d2e3 Mon Sep 17 00:00:00 2001 From: xhe Date: Sun, 18 Sep 2022 15:06:27 +0800 Subject: [PATCH] *: refine github actions Signed-off-by: xhe --- .github/workflows/{build.yml => common.yml} | 56 +++++++++------------ .github/workflows/main.yml | 15 +++++- .github/workflows/test.yml | 32 ------------ 3 files changed, 36 insertions(+), 67 deletions(-) rename .github/workflows/{build.yml => common.yml} (54%) delete mode 100644 .github/workflows/test.yml diff --git a/.github/workflows/build.yml b/.github/workflows/common.yml similarity index 54% rename from .github/workflows/build.yml rename to .github/workflows/common.yml index 5270b5e7..138c00d5 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/common.yml @@ -1,4 +1,4 @@ -name: build +name: make on: workflow_call: inputs: @@ -6,17 +6,28 @@ on: type: boolean description: "set tmate on failure" required: true + target: + type: string + description: "makefile target" + required: true ref: type: string description: "checkout specific ref" required: true + all_platform: + type: boolean + description: "test on all platforms or not" + default: false + +defaults: + run: + shell: bash jobs: - cmd: + make: strategy: - matrix: - platform: [ubuntu-latest, macos-latest, windows-latest] - runs-on: ${{ matrix.platform }} + matrix: ${{ fromJSON(inputs.all_platform && '{"platform": ["ubuntu-latest", "macos-latest", "windows-latest"]}' || '{}') }} + runs-on: ${{ matrix.platform || 'ubuntu-latest' }} steps: - if: ${{ runner.os == 'Windows' }} name: Use GNU tar for faster cache restore @@ -33,41 +44,20 @@ jobs: with: go-version-file: go.mod check-latest: true - cache: true - - name: "build cmd" - run: make - - name: "set up tmate session if necessary" - if: ${{ failure() && inputs.debug }} - uses: mxschmitt/action-tmate@v3 - - all: - needs: cmd - runs-on: ubuntu-latest - steps: - - name: "checkout repo" - uses: actions/checkout@v3 - with: - ref: ${{ inputs.ref }} - - name: "setup golang" - uses: actions/setup-go@v3 - with: - go-version: 1.18 + - name: "set vars" + run: | + echo "GOCACHE=$(go env GOCACHE)" >> $GITHUB_ENV + echo "GOMODCACHE=$(go env GOMODCACHE)" >> $GITHUB_ENV - name: "try to use build cache" uses: actions/cache@v3 with: path: | - ~/.cache/go-build - ~/go/pkg/mod - ~/Library/Caches/go-build - ~\AppData\Local\go-build + ${{ env.GOCACHE }} + ${{ env.GOMODCACHE }} key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} restore-keys: | ${{ runner.os }}-go- - - name: "build all" - run: make build - - name: "set up tmate session if necessary" - if: ${{ failure() && inputs.debug }} - uses: mxschmitt/action-tmate@v3 + - run: make ${{ inputs.target }} - name: "set up tmate session if necessary" if: ${{ failure() && inputs.debug }} uses: mxschmitt/action-tmate@v3 diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 88deb3b9..9ed681b2 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -27,15 +27,26 @@ concurrency: cancel-in-progress: true jobs: + cmd: + uses: ./.github/workflows/common.yml + with: + debug: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.debug }} + ref: ${{ inputs.ref || github.ref }} + target: "cmd" + all_platform: true + build: - uses: ./.github/workflows/build.yml + needs: cmd + uses: ./.github/workflows/common.yml with: debug: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.debug }} ref: ${{ inputs.ref || github.ref }} + target: "build" test: needs: build - uses: ./.github/workflows/test.yml + uses: ./.github/workflows/common.yml with: debug: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.debug }} ref: ${{ inputs.ref || github.ref }} + target: "test" diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml deleted file mode 100644 index a72cc764..00000000 --- a/.github/workflows/test.yml +++ /dev/null @@ -1,32 +0,0 @@ -name: test -on: - workflow_call: - inputs: - debug: - type: boolean - description: "set tmate on failure" - required: true - ref: - type: string - description: "checkout specific ref" - required: true - -jobs: - ut: - runs-on: ubuntu-latest - steps: - - name: "checkout repo" - uses: actions/checkout@v3 - with: - ref: ${{ inputs.ref }} - - name: "setup golang" - uses: actions/setup-go@v3 - with: - go-version-file: go.mod - check-latest: true - cache: true - - name: "test all" - run: make test - - name: "set up tmate session if necessary" - if: ${{ failure() && inputs.debug }} - uses: mxschmitt/action-tmate@v3