Skip to content

Commit d589d2c

Browse files
committed
ci: improved workflows
1 parent cd33a6b commit d589d2c

6 files changed

Lines changed: 363 additions & 303 deletions

File tree

.github/workflows/atomicgo.yml

Lines changed: 0 additions & 167 deletions
This file was deleted.

.github/workflows/docs.yml

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
# ┌───────────────────────────────────────────────────────────────────┐
2+
# │ │
3+
# │ IMPORTANT NOTE │
4+
# │ │
5+
# │ This file is synced with https://github.com/atomicgo/template │
6+
# │ │
7+
# │ Please apply all changes to the template repository │
8+
# │ │
9+
# └───────────────────────────────────────────────────────────────────┘
10+
11+
name: Documentation
12+
13+
on:
14+
push:
15+
branches:
16+
- main
17+
18+
concurrency:
19+
group: docs-${{ github.ref }}
20+
cancel-in-progress: false
21+
22+
permissions:
23+
contents: write
24+
25+
jobs:
26+
update-readme:
27+
name: Update generated README
28+
runs-on: ubuntu-latest
29+
if: "!contains(github.event.head_commit.message, 'docs: autoupdate')"
30+
31+
steps:
32+
- name: Check out repository
33+
uses: actions/checkout@v6
34+
with:
35+
fetch-depth: 0
36+
37+
- name: Set up Go
38+
uses: actions/setup-go@v6
39+
with:
40+
go-version-file: go.mod
41+
cache: true
42+
43+
- name: Install documentation tools
44+
shell: bash
45+
run: |
46+
set -euo pipefail
47+
48+
echo "::group::Install documentation tools"
49+
go install github.com/robertkrimen/godocdown/godocdown@latest
50+
go install github.com/princjef/gomarkdoc/cmd/gomarkdoc@latest
51+
echo "::endgroup::"
52+
53+
- name: Download README templates
54+
shell: bash
55+
run: |
56+
set -euo pipefail
57+
58+
echo "::group::Download README templates"
59+
mkdir -p .templates
60+
curl --fail --location --show-error --silent \
61+
https://raw.githubusercontent.com/atomicgo/atomicgo/main/templates/example.gotxt \
62+
--output .templates/example.gotxt
63+
curl --fail --location --show-error --silent \
64+
https://raw.githubusercontent.com/atomicgo/atomicgo/main/templates/readme.md \
65+
--output .templates/readme.md
66+
echo "::endgroup::"
67+
68+
- name: Configure Git
69+
shell: bash
70+
run: |
71+
set -euo pipefail
72+
73+
echo "::group::Configure Git"
74+
git config --global --add safe.directory /github/workspace
75+
git config user.name "github-actions[bot]"
76+
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
77+
git update-index --assume-unchanged .github/workflows/*
78+
echo "::notice::Using github-actions[bot] for generated documentation commits."
79+
echo "::endgroup::"
80+
81+
- name: Generate README
82+
id: readme
83+
shell: bash
84+
run: |
85+
set -euo pipefail
86+
87+
echo "::group::Generate README"
88+
syncignore=./.syncignore
89+
include_unexported=./.github/atomicgo/include_unexported
90+
readme_ignored=false
91+
92+
if [[ -f "$syncignore" ]]; then
93+
while IFS= read -r entry || [[ -n "$entry" ]]; do
94+
entry="${entry%$'\r'}"
95+
entry="${entry#"${entry%%[![:space:]]*}"}"
96+
entry="${entry%"${entry##*[![:space:]]}"}"
97+
98+
while [[ "$entry" == ./* ]]; do
99+
entry="${entry#./}"
100+
done
101+
102+
[[ -z "$entry" || "$entry" == \#* ]] && continue
103+
104+
if [[ "$entry" == "README.md" ]]; then
105+
readme_ignored=true
106+
break
107+
fi
108+
done <"$syncignore"
109+
fi
110+
111+
echo "ignored=$readme_ignored" >> "$GITHUB_OUTPUT"
112+
113+
if [[ "$readme_ignored" == true ]]; then
114+
echo "::notice::README.md is listed in .syncignore. Skipping README generation."
115+
else
116+
echo "::notice::Running godocdown"
117+
"$(go env GOPATH)/bin/godocdown" -template ./.templates/readme.md >README.md
118+
119+
echo "::notice::Running gomarkdoc"
120+
gomarkdoc_flags=(--template-file example=./.templates/example.gotxt)
121+
122+
if [[ -f "$include_unexported" ]]; then
123+
gomarkdoc_flags+=(-u)
124+
fi
125+
126+
"$(go env GOPATH)/bin/gomarkdoc" \
127+
"${gomarkdoc_flags[@]}" \
128+
--repository.url "https://github.com/${{ github.repository }}" \
129+
--repository.default-branch main \
130+
--repository.path / \
131+
-e \
132+
-o README.md \
133+
.
134+
fi
135+
echo "::endgroup::"
136+
137+
- name: Update test badge
138+
if: steps.readme.outputs.ignored != 'true'
139+
shell: bash
140+
run: |
141+
set -euo pipefail
142+
143+
echo "::group::Update unit test badge"
144+
go test -v -p 1 ./... | tee test.log
145+
unittest_count=$(grep -c '^=== RUN' test.log || true)
146+
147+
if [[ -f README.md ]]; then
148+
sed -i -E \
149+
's|<img src="https://img.shields.io/badge/Unit_Tests-[^"]*-magenta\?style=flat-square"|<img src="https://img.shields.io/badge/Unit_Tests-'"$unittest_count"'-magenta?style=flat-square"|g' \
150+
README.md
151+
fi
152+
153+
echo "::notice::Detected $unittest_count unit tests."
154+
echo "::endgroup::"
155+
156+
- name: Tidy module files
157+
shell: bash
158+
run: |
159+
set -euo pipefail
160+
161+
echo "::group::Tidy module files"
162+
git checkout -- go.mod 2>/dev/null || true
163+
git checkout -- go.sum 2>/dev/null || true
164+
go mod tidy
165+
echo "::endgroup::"
166+
167+
- name: Commit generated changes
168+
shell: bash
169+
run: |
170+
set -euo pipefail
171+
172+
echo "::group::Commit generated changes"
173+
changes=$(git status --porcelain -- README.md go.mod go.sum)
174+
175+
if [[ -z "$changes" ]]; then
176+
echo "::notice::No generated documentation changes."
177+
{
178+
echo "## Documentation"
179+
echo ""
180+
echo "No generated documentation changes were detected."
181+
} >> "$GITHUB_STEP_SUMMARY"
182+
echo "::endgroup::"
183+
exit 0
184+
fi
185+
186+
printf '%s\n' "$changes"
187+
git add README.md go.mod
188+
189+
if [[ -f go.sum ]]; then
190+
git add go.sum
191+
fi
192+
193+
git commit -m "docs: autoupdate"
194+
git push origin "HEAD:$GITHUB_REF_NAME"
195+
196+
{
197+
echo "## Documentation"
198+
echo ""
199+
echo "Generated documentation changes were committed and pushed."
200+
} >> "$GITHUB_STEP_SUMMARY"
201+
echo "::endgroup::"

0 commit comments

Comments
 (0)