Skip to content

Commit b70c21c

Browse files
author
Kaspar Lyngsie
committed
chore: re-structured github action for a modular approach
1 parent d9de57a commit b70c21c

1 file changed

Lines changed: 113 additions & 29 deletions

File tree

Lines changed: 113 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,127 @@
1-
name: Synchronize Help
1+
name: Synchronize CLI Help from GitBooks
22

33
on:
44
workflow_dispatch:
55
schedule:
6-
- cron: '0 9 * * 1' # Mon at 9
7-
push:
8-
branches: [chore/docs-action]
6+
- cron: '0 9 * * 1' # Monday at 9
97

108
jobs:
11-
build:
12-
name: synchronize-help
9+
synchronize-cli-help:
10+
name: Synchronize CLI Help
1311
runs-on: ubuntu-latest
12+
13+
permissions:
14+
contents: write
15+
pull-requests: write
16+
17+
env:
18+
DESTINATION_BRANCH: docs/automatic-gitbook-update
19+
COMMIT_MESSAGE: 'docs: synchronizing README from GitBook'
20+
1421
steps:
15-
- run: |
16-
gh auth setup-git
22+
- name: Checkout user-docs repository
23+
uses: actions/checkout@v4
24+
with:
25+
repository: snyk/user-docs
26+
path: user-docs
27+
ref: main
28+
29+
- name: Checkout cli repository
30+
uses: actions/checkout@v4
31+
with:
32+
repository: snyk/cli
33+
path: cli
34+
token: ${{ secrets.GITHUB_TOKEN }}
35+
fetch-depth: 0
36+
37+
- name: Configure git user
38+
run: |
1739
git config --global user.email "noreply@snyk.io"
40+
41+
# GITHUB_ACTOR: The name of the person or app that initiated the workflow.
1842
git config --global user.name "$GITHUB_ACTOR"
19-
gh repo clone snyk/snyk cli -- --depth=1 --quiet
20-
gh repo clone snyk/user-docs docs -- --depth=1 --quiet
21-
git -C ./cli checkout -b docs/automatic-gitbook-update
22-
23-
cp ./docs/docs/snyk-cli/commands/*.md ./cli/help/cli-commands/
24-
25-
if [[ $(git -C ./cli status --porcelain) ]]; then
26-
echo "Documentation changes detected"
27-
cd ./cli
28-
npm clean-install
29-
npx prettier --write ./help/cli-commands/*.md
30-
git --no-pager diff --name-only
31-
git add .
32-
git commit -m "docs: synchronizing help from snyk/user-docs"
33-
git push --force --set-upstream origin docs/automatic-gitbook-update
34-
if [[ ! $(gh pr view docs/automatic-gitbook-update 2>&1 | grep -q "no open pull requests";) ]]; then
35-
echo "Creating PR"
36-
gh pr create --title="docs: Synchronizing CLI help from user-docs" --body="Automatic PR controlled by GitHub Action" --head docs/automatic-gitbook-update
37-
fi
38-
echo "PR exists, pushed changes to it."
39-
else
43+
44+
- name: Create or checkout destination branch
45+
run: |
46+
cd cli
47+
if git show-ref --verify --quiet refs/heads/${{ env.DESTINATION_BRANCH }}; then
48+
echo "Branch ${{ env.DESTINATION_BRANCH }} already exists, checking out."
49+
git checkout ${{ env.DESTINATION_BRANCH }}
50+
exit 0
51+
fi
52+
53+
echo "Branch ${{ env.DESTINATION_BRANCH }} does not exist, creating and checking out."
54+
git checkout -b ${{ env.DESTINATION_BRANCH }}
55+
56+
- name: Retrieve GitBook content and update README
57+
run: |
58+
wget \
59+
https://raw.githubusercontent.com/snyk/user-docs/main/docs/snyk-cli/getting-started-with-the-snyk-cli.md \
60+
-O ./cli/README.md
61+
62+
# GitBook Markdown files often use relative paths for assets (e.g., images)
63+
# like `../.gitbook/assets/image.png`. When this README.md is viewed directly
64+
# on GitHub, these relative paths won't resolve correctly. This `sed` command
65+
# replaces those relative paths with the full, absolute URL to the assets
66+
# hosted in the `snyk/user-docs` GitHub repository's raw content.
67+
#
68+
# - `-i`: Edits the file in-place, modifying the original README.md directly.
69+
# - `-e`: Specifies the script to be executed. In this case, it's the 's'
70+
# (substitute) command which replaces the old path with the new URL.
71+
sed -i \
72+
-e "s|../.gitbook/assets/|https://github.com/snyk/user-docs/raw/HEAD/docs/.gitbook/assets/|g" \
73+
./cli/README.md
74+
75+
- name: Format README with Prettier
76+
run: |
77+
cd ./cli
78+
npx prettier --write README.md
79+
80+
- name: Check for changes
81+
id: check_changes
82+
run: |
83+
cd ./cli
84+
if [[ -z "$(git status --porcelain)" ]]; then
4085
echo "No documentation changes detected, exiting."
86+
echo "continue=false" >> "$GITHUB_OUTPUT"
87+
exit 0
88+
fi
89+
90+
echo "--- Documentation changes detected from GitBooks (before Prettier) ---"
91+
git --no-pager diff --color=always
92+
echo "---------------------------------------------------------------------"
93+
94+
echo "continue=true" >> "$GITHUB_OUTPUT"
95+
96+
- name: Commit and push changes (if any)
97+
if: steps.check_changes.outputs.continue == 'true'
98+
run: |
99+
cd ./cli
100+
git add .
101+
git commit -m "${{ env.COMMIT_MESSAGE }}"
102+
git push --force --set-upstream origin ${{ env.DESTINATION_BRANCH }}
103+
104+
- name: Create or update a pull request
105+
if: steps.check_changes.outputs.continue == 'true'
106+
run: |
107+
cd ./cli
108+
PR_NUMBER=$(gh pr list \
109+
--head "${{ env.DESTINATION_BRANCH }}" \
110+
--json number \
111+
--jq '.[0].number' \
112+
--limit 1)
113+
114+
if [ -n "$PR_NUMBER" ]; then
115+
echo "PR #$PR_NUMBER already exists. Updating it."
116+
echo "Pushed changes to existing PR #$PR_NUMBER."
117+
exit 0
41118
fi
119+
120+
echo "No existing PR found. Creating a new one."
121+
gh pr create \
122+
--title="${{ env.COMMIT_MESSAGE }}" \
123+
--body="Automatic PR controlled by GitHub Action" \
124+
--head "${{ env.DESTINATION_BRANCH }}" \
125+
--base main
42126
env:
43127
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

0 commit comments

Comments
 (0)