Skip to content

Commit ea56750

Browse files
authored
Merge pull request #7 from metabase/gh_docs_build_workflow
2 parents 4ee5b97 + 8fd9349 commit ea56750

File tree

3 files changed

+164
-63
lines changed

3 files changed

+164
-63
lines changed

.github/workflows/process_docs_changes.yml

Lines changed: 54 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -9,49 +9,71 @@ on:
99
description: Which `metabase/metabase` branch triggered this workflow
1010
required: true
1111
type: string
12+
repository_dispatch:
13+
types: [docs_update]
1214

1315
jobs:
1416
build-and-commit:
1517
runs-on: ubuntu-latest
18+
env:
19+
MAIN_REPO_BRANCH: "${{ github.event.inputs.branch_name || github.event.client_payload.branch_name }}"
1620
steps:
1721
- uses: actions/checkout@v4
1822

19-
- name: Display received payload
23+
- name: Set current branch name
24+
run: echo "LOCAL_REPO_BRANCH=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}" >> $GITHUB_ENV
25+
26+
- name: echo branch name
27+
run: |
28+
echo "Got (metabase) branch_name: $MAIN_REPO_BRANCH"
29+
30+
- name: echo local repo branch
31+
run: |
32+
echo "Running on (docs.metabase.github.io) branch: $LOCAL_REPO_BRANCH"
33+
34+
- name: Setup Babashka
35+
uses: turtlequeue/[email protected]
36+
with:
37+
babashka-version: 1.12.199
38+
39+
- name: Filter non-documented branches
40+
description: This will stop the workflow if the branch isn't master or release-x.\d.x.
2041
run: |
21-
echo "Got branch_name: ${{ github.event.inputs.branch_name }}"
42+
bb script/check_incoming_branchname.clj $MAIN_REPO_BRANCH
2243
23-
- name: Install dependencies
44+
- name: Install js dependencies
2445
run: yarn install --frozen-lockfile --prefer-offline
2546

2647
- name: Set up Ruby # uses version from .ruby-version
2748
uses: ruby/setup-ruby@d5fb7a202fc07872cb44f00ba8e6197b70cb0c55 # v1.179.0
2849
with:
2950
bundler-cache: true
3051

31-
- name: Install dependencies
32-
run: bundle install
52+
- name: Install ruby dependencies
53+
run: |
54+
bundle install
3355
3456
- name: Update docs
3557
run: |
36-
./script/docs-update
37-
./script/docs master --set-version master
58+
# ./script/docs-update
59+
# ./script/docs master --set-version master
60+
./script/docs "$MAIN_REPO_BRANCH" --set-version "$MAIN_REPO_BRANCH"
3861
39-
- name: "Lint the markdown"
40-
run: |
41-
yarn lint-markdown
62+
# - name: "Lint the markdown"
63+
# run: |
64+
# yarn lint-markdown
4265

43-
- name: "Lint the styles"
44-
run: |
45-
yarn lint-styles
66+
# - name: "Lint the styles"
67+
# run: |
68+
# yarn lint-styles
4669

47-
- name: "Lint the scripts"
48-
run: |
49-
yarn lint-scripts
50-
51-
- name: "Validate the links"
52-
run: |
53-
yarn lint-links
70+
# - name: "Lint the scripts"
71+
# run: |
72+
# yarn lint-scripts
5473

74+
# - name: "Validate the links"
75+
# run: |
76+
# yarn lint-links
5577

5678
- name: Build the Jekyll Site
5779
env:
@@ -60,55 +82,24 @@ jobs:
6082
run: |
6183
# Clear the existing site:
6284
rm -rf _site
63-
# Run the jekyll server:
85+
# Build the jekyll site:
6486
JEKYLL_ENV=development bundle exec jekyll build --baseurl '' --config _config.docs.yml --trace
6587
66-
- name: Setup Babashka
67-
uses: turtlequeue/[email protected]
68-
with:
69-
babashka-version: 1.12.197
70-
- name: Check bb runs
71-
run: bb --version
88+
# - name: "Check all of the links"
89+
# run: |
90+
# script/links || true
91+
# echo 'checking reported links...'
92+
# echo "htmlproofer spit out a report of length: $(cat htmlproofer.out | wc -l)"
93+
# bb script/analyze_links.clj --htmlproofer-output htmlproofer.out
7294

73-
- name: "Check all of the links"
95+
- name: Authenticate Git with GITHUB_TOKEN
7496
run: |
75-
script/links || true
76-
echo 'checking reported links...'
77-
echo "htmlproofer spit out a report of length: $(cat htmlproofer.out | wc -l)"
78-
bb script/analyze_links.clj --htmlproofer-output htmlproofer.out
97+
git config --global user.name "Metabase Docs bot"
98+
git config --global user.email "[email protected]"
99+
git remote set-url origin "https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/metabase/docs.metabase.github.io.git"
79100
80101
- name: Create the pull request
81102
env:
82103
GH_TOKEN: ${{ secrets.METABASE_AUTOMATION_USER_TOKEN }}
83104
run: |
84-
if git diff --quiet ; then
85-
echo "No changes to commit."
86-
else
87-
git config user.email "[email protected]"
88-
git config user.name "Metabase Docs bot"
89-
update_branch="docs-update"
90-
91-
# Check out or create the update branch
92-
if git ls-remote --exit-code --heads origin "$update_branch" > /dev/null; then
93-
git fetch origin "$update_branch"
94-
git switch "$update_branch"
95-
else
96-
git switch --create "$update_branch"
97-
fi
98-
99-
git add _site
100-
git commit --author="Metabase Docs bot <[email protected]>" --message="Update master and latest docs"
101-
git push --force origin "$update_branch"
102-
103-
# Check if a PR already exists for this branch
104-
existing_pr=$(gh pr list --head "$update_branch" --json number --jq '.[0].number')
105-
106-
if [ -z "$existing_pr" ]; then
107-
gh pr create \
108-
--title "Refresh docs (${{ github.event.inputs.branch_name }})" \
109-
--body "" \
110-
--head "$update_branch"
111-
else
112-
echo "PR already exists: #$existing_pr"
113-
fi
114-
fi
105+
bb script/update_or_create_pr.clj $MAIN_REPO_BRANCH

script/check_incoming_branchname.clj

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
(ns check-incoming-branchname)
2+
3+
(defn usage []
4+
(println "Usage: script/check_incoming_branchname.clj branchname")
5+
(System/exit 1))
6+
7+
(def release-regex #"release-x\.(\d+)\.x")
8+
9+
(defn extract-release-num [release-branchname]
10+
(let [[_ num] (re-matches release-regex release-branchname)]
11+
num))
12+
13+
(defn categorize-branchname [branchname]
14+
(cond
15+
(= branchname "master") [:master]
16+
(re-matches release-regex branchname) [:release (extract-release-num branchname)]))
17+
18+
(defn -main
19+
"This is called from the `process_docs_changes.yml` workflow. It checks that
20+
the branchname is either `master` or a release branch. If not, it exits with
21+
an error code, stopping the workflow before it goes ahead and opens a PR with
22+
the docs changes."
23+
[& args]
24+
(let [branchname (or (first args) (usage))
25+
_ (println "Checking branchname:" branchname)
26+
[category release-num] (categorize-branchname branchname)]
27+
(case category
28+
:master (println "Master branch detected.")
29+
:release (println "Release branch detected:" release-num)
30+
(do (println "Unpublishable branchname: " branchname)
31+
(System/exit 1)))))
32+
33+
(when (= *file* (System/getProperty "babashka.file"))
34+
(apply -main *command-line-args*))

script/update_or_create_pr.clj

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#!/usr/bin/env bb
2+
(ns update-or-create-pr
3+
(:require [babashka.process :as p]
4+
[cheshire.core :as json]
5+
[clojure.string :as str]))
6+
7+
(defn usage []
8+
(println "Usage: script/update_or_create_pr.clj branchname")
9+
(System/exit 1))
10+
11+
(def artifact-dirs ["_docs" "_site/docs"]) ;; Directories to copy
12+
13+
14+
15+
(defn existing-pr? [target-branch]
16+
(let [pr-data (->
17+
(p/shell {:out :string :continue true}
18+
"curl"
19+
(str "https://api.github.com/repos/metabase/docs.metabase.github.io/pulls?head=metabase:" "update-" "doc-update-detection"))
20+
:out
21+
json/parse-string)
22+
_ (println "→ PR data: " (pr-str pr-data))
23+
pr-num (some #(when (= target-branch (get % "title"))
24+
(get % "number"))
25+
pr-data)]
26+
(println "→ PR number:" pr-num)
27+
(boolean pr-num)))
28+
29+
(defn -main [& args]
30+
(let [source-branch (or (first args) (usage))
31+
target-branch (str "update-" source-branch)
32+
_ (println "Swithcing to target branch.")
33+
_ (p/shell "git" "checkout" "-B" target-branch)
34+
_ (doseq [ad artifact-dirs]
35+
(println "Adding" ad "...")
36+
(p/shell "git" "add" ad))
37+
{:keys [exit]} (p/shell {:continue true} "git" "diff" "--cached" "--quiet")]
38+
39+
(if (zero? exit)
40+
(println "→ No changes to commit.")
41+
(do
42+
(println "→ Changes detected, committing...")
43+
(p/shell "git" "commit" "-m" (str "[auto] adding content to " target-branch))
44+
(p/shell "git" "push" "--force" "origin" target-branch)
45+
(println "→ Branch updated successfully.")))
46+
47+
(println "→ Checking for existing PR...")
48+
49+
(if (existing-pr? target-branch)
50+
(println "✓ PR already exists: #" existing-pr?)
51+
(do
52+
(println "→ Creating new PR...")
53+
(p/shell "gh" "pr" "create"
54+
"--repo" "metabase/docs.metabase.github.io"
55+
"--title" target-branch
56+
"--body" (str "updated: " (pr-str artifact-dirs))
57+
"--head" target-branch)))))
58+
59+
(when (= *file* (System/getProperty "babashka.file"))
60+
(apply -main *command-line-args*))
61+
62+
(defmacro the-env []
63+
(into {} (for [k (keys &env)]
64+
[(name k) k])))
65+
66+
(defmacro nocommit-repl []
67+
`(clojure.main/repl
68+
:init (fn []
69+
(remove-ns '~'temp)
70+
(create-ns '~'temp)
71+
(doseq [[binding# value#] (the-env)]
72+
(intern '~'temp (symbol binding#) value#)))
73+
:prompt (fn [] (printf "paused.%s=> " (peek (clojure.string/split (str *ns*) #"\."))))
74+
:eval (fn [f#] (binding [clojure.test/*test-out* *out*] (eval f#)))
75+
:read clojure.core.server/repl-read
76+
:print clojure.pprint/pprint))

0 commit comments

Comments
 (0)