-
Notifications
You must be signed in to change notification settings - Fork 20
ci: new doc-preview workflow changes. #82
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
0ae625c
ci: Clean unused documentation preview
ArekBalysNordic 8112b32
ci: Simplify doc preview note
ArekBalysNordic bfc964f
ci: Enable running doc-preview workflow on changes in samples
ArekBalysNordic b1a5b9e
ci: Do no call on_pr workflow on changes in doc-preview.
ArekBalysNordic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| # Cleanup site state: remove pr-preview directories for closed PRs. | ||
| # Keeps only directories for currently open PRs so saved state matches reality. | ||
| # This action keeps the existing pages only for the actually opened PRs to avoid restoring | ||
| # the pages of closed PRs. | ||
|
|
||
| name: 'Cleanup site state' | ||
| description: 'Remove PR preview directories for closed PRs; keep only open PRs in site state' | ||
|
|
||
| inputs: | ||
| site_dir: | ||
| description: 'Path to the site directory (e.g. _site) containing pr-preview subdirs' | ||
| required: true | ||
|
|
||
| runs: | ||
| using: 'composite' | ||
| steps: | ||
| - name: Collect list of open PRs | ||
| id: open-prs | ||
| shell: bash | ||
| env: | ||
| GH_TOKEN: ${{ github.token }} | ||
| run: | | ||
| set -e | ||
|
|
||
| # Build JSON array of open PR numbers for use in next step. | ||
| if ! OPEN_PRS_JSON=$(gh pr list --state open --json number -q '.[].number' --limit 100 | jq -R -s -c '[split("\n")[] | select(length > 0) | tonumber]'); then | ||
| echo "Error: Failed to list open PRs" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| # Validate the output is valid JSON array | ||
| if ! echo "$OPEN_PRS_JSON" | jq -e 'type == "array"' >/dev/null 2>&1; then | ||
| echo "Error: Invalid JSON output from PR list" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| echo "open_prs_json=$OPEN_PRS_JSON" >> "$GITHUB_OUTPUT" | ||
| echo "Found $(echo "$OPEN_PRS_JSON" | jq 'length') open PR(s)" | ||
|
|
||
| - name: Remove closed PR preview directories | ||
| shell: bash | ||
| env: | ||
| SITE_DIR: ${{ inputs.site_dir }} | ||
| OPEN_PRS_JSON: ${{ steps.open-prs.outputs.open_prs_json }} | ||
| run: | | ||
| set -e | ||
|
|
||
| # Validate site_dir input | ||
| if [ -z "$SITE_DIR" ]; then | ||
| echo "Error: site_dir is empty" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| # Reject paths with path traversal, absolute paths, or control characters | ||
| if [[ "$SITE_DIR" == *".."* ]] || \ | ||
| [[ "$SITE_DIR" == /* ]] || \ | ||
| [[ "$SITE_DIR" == *$'\n'* ]] || \ | ||
| [[ "$SITE_DIR" == *$'\r'* ]] || \ | ||
| [[ "$SITE_DIR" == *$'\t'* ]]; then | ||
| echo "Error: Invalid site_dir (path traversal or absolute path)" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| # Get absolute path of workspace root for boundary checking | ||
| WORKSPACE_ROOT=$(cd "$GITHUB_WORKSPACE" && pwd) | ||
|
|
||
| # Resolve and validate site_dir path | ||
| if [ ! -d "$SITE_DIR" ]; then | ||
| echo "Info: site_dir does not exist: $SITE_DIR (first run or no previous state)" >&2 | ||
| exit 0 | ||
| fi | ||
|
|
||
| RESOLVED_SITE_DIR=$(cd "$SITE_DIR" && pwd) | ||
|
|
||
| # Ensure resolved path is within workspace | ||
| if [[ "$RESOLVED_SITE_DIR" != "$WORKSPACE_ROOT"* ]]; then | ||
| echo "Error: site_dir resolves outside workspace" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| PR_PREVIEW="${RESOLVED_SITE_DIR}/pr-preview" | ||
|
|
||
| # Validate PR_PREVIEW path is still within workspace | ||
| if [[ "$PR_PREVIEW" != "$WORKSPACE_ROOT"* ]]; then | ||
| echo "Error: PR_PREVIEW path escapes workspace" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| # If there is no previews, just exit | ||
| if [ ! -d "$PR_PREVIEW" ]; then | ||
| exit 0 | ||
| fi | ||
|
|
||
| # Process each pr-* directory | ||
| for dir in "$PR_PREVIEW"/pr-*; do | ||
| RESOLVED_DIR=$(cd "$dir" && pwd) | ||
|
|
||
| # Skip if glob didn't match anything, files, symlinks, etc. | ||
| if [ ! -e "$dir" ] || [ ! -d "$dir" ] || [[ "$RESOLVED_DIR" != "$WORKSPACE_ROOT"* ]] || [[ "$RESOLVED_DIR" != "$PR_PREVIEW"* ]]; then | ||
| continue | ||
| fi | ||
| name=$(basename "$dir") | ||
|
|
||
| # Validate directory name matches expected pattern (pr- followed by digits only) | ||
| if [[ "$name" =~ ^pr-([0-9]+)$ ]]; then | ||
| pr_num="${BASH_REMATCH[1]}" | ||
|
|
||
| # Validate pr_num is numeric (extra safety) | ||
| if ! [[ "$pr_num" =~ ^[0-9]+$ ]]; then | ||
| echo "Warning: Invalid PR number extracted: $pr_num" >&2 | ||
| continue | ||
| fi | ||
|
|
||
| # Validate OPEN_PRS_JSON is valid before using it | ||
| if ! echo "$OPEN_PRS_JSON" | jq -e 'type == "array"' >/dev/null 2>&1; then | ||
| echo "Error: Invalid OPEN_PRS_JSON format" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| # Check if PR is still open | ||
| if ! echo "$OPEN_PRS_JSON" | jq -e --argjson n "$pr_num" 'index($n) != null' >/dev/null 2>&1; then | ||
| rm -rf "$dir" | ||
| fi | ||
| fi | ||
| done | ||
|
|
||
| # Clean up empty pr-preview directory | ||
| if [ -d "$PR_PREVIEW" ] && [ -z "$(ls -A "$PR_PREVIEW" 2>/dev/null)" ]; then | ||
| rm -rf "$PR_PREVIEW" | ||
| fi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.