|
1 | | -name: Synchronize Help |
2 | | - |
| 1 | +name: Synchronize CLI Help to User Docs |
3 | 2 | on: |
4 | 3 | workflow_dispatch: |
5 | 4 | schedule: |
6 | | - - cron: '0 9 * * 1' # Mon at 9 |
7 | | - push: |
8 | | - branches: [chore/docs-action] |
| 5 | + - cron: '0 9 * * 1' # Monday at 9 |
| 6 | + |
| 7 | +env: |
| 8 | + DESTINATION_BRANCH: docs/automatic-gitbook-update-cli-help |
| 9 | + COMMIT_MESSAGE: "docs: synchronizing help from snyk/user-docs" |
9 | 10 |
|
10 | 11 | jobs: |
11 | 12 | build: |
12 | 13 | name: synchronize-help |
13 | 14 | runs-on: ubuntu-latest |
14 | 15 | steps: |
15 | | - - run: | |
16 | | - gh auth setup-git |
| 16 | + - name: Checkout user-docs repository |
| 17 | + uses: actions/checkout@v4 |
| 18 | + with: |
| 19 | + repository: snyk/user-docs |
| 20 | + path: user-docs |
| 21 | + ref: main |
| 22 | + |
| 23 | + - name: Checkout cli repository |
| 24 | + uses: actions/checkout@v4 |
| 25 | + with: |
| 26 | + repository: snyk/cli |
| 27 | + path: cli |
| 28 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 29 | + fetch-depth: 0 |
| 30 | + |
| 31 | + - name: Configure git user |
| 32 | + run: | |
17 | 33 | git config --global user.email "noreply@snyk.io" |
| 34 | + |
| 35 | + # GITHUB_ACTOR: The name of the person or app that initiated the workflow. |
18 | 36 | 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 | 37 |
|
| 38 | + - name: Create or checkout destination branch |
| 39 | + run: | |
| 40 | + cd cli |
| 41 | + if git show-ref --verify --quiet refs/heads/${{ env.DESTINATION_BRANCH }}; then |
| 42 | + echo "Branch ${{ env.DESTINATION_BRANCH }} already exists, checking out." |
| 43 | + git checkout ${{ env.DESTINATION_BRANCH }} |
| 44 | + exit 0 |
| 45 | + fi |
| 46 | + |
| 47 | + echo "Branch ${{ env.DESTINATION_BRANCH }} does not exist, creating and checking out." |
| 48 | + git checkout -b ${{ env.DESTINATION_BRANCH }} |
| 49 | +
|
| 50 | + - name: Copy help documentation from user-docs to CLI |
| 51 | + run: | |
23 | 52 | cp ./docs/docs/snyk-cli/commands/*.md ./cli/help/cli-commands/ |
24 | 53 |
|
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 |
| 54 | + - name: Format help files with Prettier |
| 55 | + run: | |
| 56 | + cd ./cli |
| 57 | + npm clean-install |
| 58 | + npx prettier --write ./help/cli-commands/*.md |
| 59 | +
|
| 60 | + - name: Check for changes |
| 61 | + id: check_changes |
| 62 | + run: | |
| 63 | + cd ./cli |
| 64 | + if [[ -z "$(git status --porcelain)" ]]; then |
40 | 65 | echo "No documentation changes detected, exiting." |
| 66 | + echo "continue=false" >> "$GITHUB_OUTPUT" |
| 67 | + exit 0 |
| 68 | + fi |
| 69 | +
|
| 70 | + echo "--- Documentation changes detected from GitBooks ---" |
| 71 | + git --no-pager diff --name-only --color=always |
| 72 | + echo "------------------------------------------------" |
| 73 | + |
| 74 | + echo "continue=true" >> "$GITHUB_OUTPUT" |
| 75 | +
|
| 76 | + - name: Commit and push changes (if any) |
| 77 | + if: steps.check_changes.outputs.continue == 'true' |
| 78 | + run: | |
| 79 | + cd ./cli |
| 80 | + git add . |
| 81 | + git commit -m "${{ env.COMMIT_MESSAGE }}" |
| 82 | + git push --force --set-upstream origin ${{ env.DESTINATION_BRANCH }} |
| 83 | +
|
| 84 | + - name: Create or update a pull request |
| 85 | + if: steps.check_changes.outputs.continue == 'true' |
| 86 | + run: | |
| 87 | + cd ./cli |
| 88 | + PR_NUMBER=$(gh pr list \ |
| 89 | + --head "${{ env.DESTINATION_BRANCH }}" \ |
| 90 | + --json number \ |
| 91 | + --jq '.[0].number' \ |
| 92 | + --limit 1) |
| 93 | +
|
| 94 | + if [ -n "$PR_NUMBER" ]; then |
| 95 | + echo "PR #$PR_NUMBER already exists. Updating it." |
| 96 | + echo "Pushed changes to existing PR #$PR_NUMBER." |
| 97 | + exit 0 |
41 | 98 | fi |
| 99 | + |
| 100 | + echo "No existing PR found. Creating a new one." |
| 101 | + gh pr create \ |
| 102 | + --title="${{ env.COMMIT_MESSAGE }}" \ |
| 103 | + --body="Automatic PR controlled by GitHub Action" \ |
| 104 | + --head "${{ env.DESTINATION_BRANCH }}" \ |
| 105 | + --base main |
42 | 106 | env: |
43 | 107 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
0 commit comments