Skip to content

Commit 6ea1526

Browse files
authored
fix(ci/golangci): fix golangcl-lint git push and apply format code on Push (#4077)
1 parent 3a19ee9 commit 6ea1526

9 files changed

+149
-72
lines changed

.github/workflows/before_script.sh

+33-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,36 @@
11
#!/usr/bin/env bash
22

3-
find . -name "*.go" | xargs gofmt -w
4-
git diff --name-only --exit-code || if [ $? != 0 ]; then echo "Notice: gofmt check failed,please gofmt before pr." && exit 1; fi
5-
echo "gofmt check pass."
3+
# Install gci
4+
echo "Installing gci..."
5+
go install github.com/daixiang0/gci@latest
6+
7+
# Check if the GCI is installed successfully
8+
if ! command -v gci &> /dev/null
9+
then
10+
echo "gci could not be installed. Please check your Go setup."
11+
exit 1
12+
fi
13+
14+
# Use GCI to format the code
15+
echo "Running gci to format code..."
16+
gci write \
17+
--custom-order \
18+
--skip-generated \
19+
--skip-vendor \
20+
-s standard \
21+
-s blank \
22+
-s default \
23+
-s dot \
24+
-s "prefix(github.com/gogf/gf/v2)" \
25+
-s "prefix(github.com/gogf/gf/cmd)" \
26+
-s "prefix(github.com/gogf/gf/contrib)" \
27+
-s "prefix(github.com/gogf/gf/example)" \
28+
./
29+
30+
# Check the code for changes
31+
git diff --name-only --exit-code || if [ $? != 0 ]; then echo "Notice: gci check failed, please gci before pr." && exit 1; fi
32+
echo "gci check pass."
33+
34+
# Add the local domain name to `/etc/hosts`
35+
echo "Adding local domain to /etc/hosts..."
636
sudo echo "127.0.0.1 local" | sudo tee -a /etc/hosts
+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
name: Format Code on Push
2+
3+
on:
4+
push
5+
6+
jobs:
7+
format-code:
8+
strategy:
9+
matrix:
10+
go-version: [ 'stable' ]
11+
name: format-code-by-gci
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Checkout code
15+
uses: actions/checkout@v4
16+
- name: Setup Golang ${{ matrix.go-version }}
17+
uses: actions/setup-go@v5
18+
with:
19+
go-version: ${{ matrix.go-version }}
20+
- name: Install gci
21+
run: go install github.com/daixiang0/gci@latest
22+
- name: Run gci
23+
run: |
24+
gci write --custom-order \
25+
--skip-generated \
26+
--skip-vendor \
27+
-s standard \
28+
-s blank \
29+
-s default \
30+
-s dot \
31+
-s "prefix(github.com/gogf/gf/v2)" \
32+
-s "prefix(github.com/gogf/gf/cmd)" \
33+
-s "prefix(github.com/gogf/gf/contrib)" \
34+
-s "prefix(github.com/gogf/gf/example)" \
35+
./
36+
- name: Check for changes
37+
run: |
38+
if [[ -n "$(git status --porcelain)" ]]; then
39+
echo "HAS_CHANGES=true" >> $GITHUB_ENV
40+
else
41+
echo "HAS_CHANGES=false" >> $GITHUB_ENV
42+
fi
43+
- name: Configure Git
44+
run: |
45+
if [[ "$HAS_CHANGES" == 'true' ]]; then
46+
git config --global user.name "github-actions[bot]"
47+
git config --global user.email "github-actions[bot]@users.noreply.github.com"
48+
else
49+
echo "HAS_CHANGES= $HAS_CHANGES "
50+
fi
51+
- name: Commit and push changes
52+
run: |
53+
if [[ "$HAS_CHANGES" == 'true' ]]; then
54+
git add .
55+
git commit -m "Apply gci import order changes"
56+
git push origin ${{ github.event.pull_request.head.ref }}
57+
else
58+
echo "No change to commit push"
59+
fi
60+
env:
61+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/golangci-lint.yml

+6-38
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# If a copy of the MIT was not distributed with this file,
55
# You can obtain one at https://github.com/gogf/gf.
66

7-
name: GolangCI-Lint
7+
name: GolangCI Lint
88
on:
99
push:
1010
branches:
@@ -26,11 +26,11 @@ on:
2626
- feat/**
2727

2828
jobs:
29-
golangci:
29+
golang-ci:
3030
strategy:
3131
matrix:
3232
go-version: [ 'stable' ]
33-
name: golangci-lint
33+
name: golang-ci-lint
3434
runs-on: ubuntu-latest
3535
steps:
3636
- name: Checkout
@@ -39,44 +39,12 @@ jobs:
3939
uses: actions/setup-go@v5
4040
with:
4141
go-version: ${{ matrix.go-version }}
42-
- name: golangci-lint
42+
- name: golang-ci-lint
4343
uses: golangci/golangci-lint-action@v6
4444
with:
4545
# Required: specify the golangci-lint version without the patch version to always use the latest patch.
4646
version: v1.62.2
4747
only-new-issues: true
48+
skip-cache: true
4849
github-token: ${{ secrets.GITHUB_TOKEN }}
49-
args: --timeout 3m0s
50-
- name: Install gci
51-
run: go install github.com/daixiang0/gci@latest
52-
- name: Run gci
53-
run: |
54-
gci write --custom-order \
55-
--skip-generated \
56-
--skip-vendor \
57-
-s standard \
58-
-s blank \
59-
-s default \
60-
-s dot \
61-
-s "prefix(github.com/gogf/gf/v2)" \
62-
-s "prefix(github.com/gogf/gf/cmd)" \
63-
-s "prefix(github.com/gogf/gf/contrib)" \
64-
-s "prefix(github.com/gogf/gf/example)" \
65-
./
66-
- name: Check for changes
67-
# Check if the event is a push or a pull request from a forked repository
68-
if: github.event_name == 'push'|| (github.event_name == 'pull_request' && github.event.pull_request.head.repo.fork == true)
69-
run: |
70-
if [[ -n "$(git status --porcelain)" ]]; then
71-
echo "HAS_CHANGES=true" >> $GITHUB_ENV
72-
else
73-
echo "HAS_CHANGES=false" >> $GITHUB_ENV
74-
fi
75-
- name: Commit and push changes
76-
if: env.HAS_CHANGES == 'true'
77-
run: |
78-
git config --global user.name "github-actions[bot]"
79-
git config --global user.email "github-actions[bot]@users.noreply.github.com"
80-
git add .
81-
git commit -m "Apply gci import order changes"
82-
git push origin HEAD:$(git rev-parse --abbrev-ref HEAD)
50+
args: --timeout 3m0s --config=.golangci.yml -v

.github/workflows/issue-check-inactive.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
# 规则描述:每天凌晨3点(GMT+8)执行一次,将最近7天没有活跃且非BUG的ISSUE设置标签:inactive
1+
# Rule description: Execute the ISSUE once a day at 3 a.m. (GMT+8) and set the non-bug issue that has not been active in the last 7 days to inactive
22
name: Issue Check Inactive
33

44
on:
55
schedule:
66
- cron: "0 19 * * *"
77

8-
env: # 设置环境变量
9-
TZ: Asia/Shanghai #时区(设置时区可使页面中的`最近更新时间`使用时区时间)
8+
env: # Set environment variables
9+
TZ: Asia/Shanghai #Time zone (setting the time zone allows the 'Last Updated' on the page to use the time zone)
1010

1111
permissions:
1212
contents: read

.github/workflows/issue-close-inactive.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
# 规则描述:每天凌晨 4 点 (GMT+8) 执行一次,将最近 30 天没有活跃且非 BUG 的 ISSUE 关闭
1+
# RULE DESCRIPTION: EXECUTED ONCE A DAY AT 4 A.M. (GMT+8) TO CLOSE NON-BUG ISSUES THAT HAVE NOT BEEN ACTIVE IN THE LAST 30 DAYS
22
name: Issue Close Inactive
33

44
on:
55
schedule:
66
- cron: "0 20 * * *"
77

8-
env: # 设置环境变量
9-
TZ: Asia/Shanghai #时区(设置时区可使页面中的`最近更新时间`使用时区时间)
8+
env: # Set environment variables
9+
TZ: Asia/Shanghai #Time zone (setting the time zone allows the 'Last Updated' on the page to use the time zone)
1010

1111
jobs:
1212
close-issues:

.github/workflows/issue-labeled.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
## 规则描述:当 issue 被标记为 help wanted 时,增加评论
1+
## Rule description: Add comments when an issue is marked as help wanted
22

33
name: Issue Labeled
44

55
on:
66
issues:
77
types: [labeled]
88

9-
env: # 设置环境变量
10-
TZ: Asia/Shanghai # 时区(设置时区可使页面中的`最近更新时间`使用时区时间)
9+
env: # Set environment variables
10+
TZ: Asia/Shanghai # Time zone (setting the time zone allows the 'Last Updated' on the page to use the time zone)
1111

1212
jobs:
1313
reply-labeled:

.github/workflows/issue-remove-inactive.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 规则描述:在 issue 没有活跃且尚未被关闭期间,若 issue 作者更新或评论该 ISSUE,则移除其 inactive 标签
1+
# Rule description: If an issue author updates or comments on an issue while it is not active and has not been closed, the inactive tag will be removed
22
name: Issue Remove Inactive
33

44
on:
@@ -7,8 +7,8 @@ on:
77
issue_comment:
88
types: [created, edited]
99

10-
env: # 设置环境变量
11-
TZ: Asia/Shanghai #时区(设置时区可使页面中的`最近更新时间`使用时区时间)
10+
env: # Set environment variables
11+
TZ: Asia/Shanghai #Time zone (setting the time zone allows the 'Last Updated' on the page to use the time zone)
1212

1313
permissions:
1414
contents: read

.github/workflows/issue-remove-need-more-details.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 规则描述:将需要提供更多细节且暂未关闭的 issue,在 issue 作者评论后,移除 need more details 标签
1+
# Rule Description: For issues that need more details and are not yet closed, remove the "need more details" tag after the issue author comments
22
name: Issue Remove Need More Details
33

44
on:
@@ -7,8 +7,8 @@ on:
77
issue_comment:
88
types: [created, edited]
99

10-
env: # 设置环境变量
11-
TZ: Asia/Shanghai #时区(设置时区可使页面中的`最近更新时间`使用时区时间)
10+
env: # Set environment variables
11+
TZ: Asia/Shanghai #Time zone (setting the time zone allows the 'Last Updated' on the page to use the time zone)
1212

1313
permissions:
1414
contents: read

.golangci.yml

+34-16
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,46 @@
66

77
# Options for analysis running.
88
run:
9+
# Timeout for analysis, e.g. 30s, 5m.
10+
# Default: 1m
11+
timeout: 5m
912
# Exit code when at least one issue was found.
1013
# Default: 1
1114
issues-exit-code: 2
12-
1315
# Include test files or not.
1416
# Default: true
1517
tests: false
16-
17-
# Which dirs to skip: issues from them won't be reported.
18-
# Can use regexp here: `generated.*`, regexp is applied on full path.
19-
# Default value is empty list,
20-
# but default dirs are skipped independently of this option's value (see skip-dirs-use-default).
21-
# "/" will be replaced by current OS file path separator to properly work on Windows.
22-
skip-dirs: []
23-
24-
# Which files to skip: they will be analyzed, but issues from them won't be reported.
25-
# Default value is empty list,
26-
# but there is no need to include all autogenerated files,
27-
# we confidently recognize autogenerated files.
28-
# If it's not please let us know.
29-
# "/" will be replaced by current OS file path separator to properly work on Windows.
30-
skip-files: []
18+
# List of build tags, all linters use it.
19+
# Default: []
20+
build-tags: []
21+
# If set, we pass it to "go list -mod={option}". From "go help modules":
22+
# If invoked with -mod=readonly, the go command is disallowed from the implicit
23+
# automatic updating of go.mod described above. Instead, it fails when any changes
24+
# to go.mod are needed. This setting is most useful to check that go.mod does
25+
# not need updates, such as in a continuous integration and testing system.
26+
# If invoked with -mod=vendor, the go command assumes that the vendor
27+
# directory holds the correct copies of dependencies and ignores
28+
# the dependency descriptions in go.mod.
29+
#
30+
# Allowed values: readonly|vendor|mod
31+
# Default: ""
32+
modules-download-mode: readonly
33+
# Allow multiple parallel golangci-lint instances running.
34+
# If false, golangci-lint acquires file lock on start.
35+
# Default: false
36+
allow-parallel-runners: true
37+
# Allow multiple golangci-lint instances running, but serialize them around a lock.
38+
# If false, golangci-lint exits with an error if it fails to acquire file lock on start.
39+
# Default: false
40+
allow-serial-runners: true
41+
# Define the Go version limit.
42+
# Mainly related to generics support since go1.18.
43+
# Default: use Go version from the go.mod file, fallback on the env var `GOVERSION`, fallback on 1.17
44+
go: '1.20'
45+
# Number of operating system threads (`GOMAXPROCS`) that can execute golangci-lint simultaneously.
46+
# If it is explicitly set to 0 (i.e. not the default) then golangci-lint will automatically set the value to match Linux container CPU quota.
47+
# Default: the number of logical CPUs in the machine
48+
concurrency: 4
3149

3250

3351
# Main linters configurations.

0 commit comments

Comments
 (0)