forked from Oreoxmt/pingcap-docs-website-scaffold
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'Oreoxmt:main' into main
- Loading branch information
Showing
5 changed files
with
154 additions
and
115 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,71 +1,26 @@ | ||
name: PR Preview | ||
|
||
on: | ||
push: | ||
branches: | ||
- preview/** | ||
- preview-cloud/** | ||
- preview-operator/** | ||
workflow_dispatch: | ||
inputs: | ||
PR_REPO: | ||
required: true | ||
type: choice | ||
options: ['pingcap/docs', 'pingcap/docs-cn'] | ||
default: 'pingcap/docs' | ||
description: 'The repository to preview' | ||
PR_NUMBER: | ||
required: true | ||
type: number | ||
description: 'The PR number to preview' | ||
PREVIEW_PRODUCT: | ||
required: true | ||
type: choice | ||
options: ["tidb", "tidbcloud"] | ||
default: "tidb" | ||
description: 'The product to preview' | ||
PREVIEW_BRANCH: | ||
required: true | ||
type: string | ||
default: 'master' | ||
description: 'The branch to preview, such as master, release-x.y' | ||
|
||
jobs: | ||
copy_files: | ||
sync: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Check out destination repository | ||
uses: actions/checkout@v3 | ||
with: | ||
path: pingcap-docs-website-scaffold | ||
|
||
- name: Check out source repository | ||
uses: actions/checkout@v3 | ||
with: | ||
repository: ${{ github.event.inputs.PR_REPO }} | ||
path: source-repo | ||
|
||
- name: Copy files | ||
- name: Sync PR | ||
run: | | ||
chmod +x pingcap-docs-website-scaffold/sync_pr.sh | ||
if [ "${{ github.event.inputs.PR_REPO }}" = "pingcap/docs" ] | ||
then | ||
export LANG="en" | ||
elif [ "${{ github.event.inputs.PR_REPO }}" = "pingcap/docs-cn" ] | ||
then | ||
export LANG="zh" | ||
fi | ||
pingcap-docs-website-scaffold/sync_pr.sh ${{ github.event.inputs.PR_NUMBER }} source-repo pingcap-docs-website-scaffold/markdown-pages/$LANG/${{ github.event.inputs.PREVIEW_PRODUCT }}/${{ github.event.inputs.PREVIEW_BRANCH }} | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
|
||
- name: Commit and push changes | ||
run: | | ||
cd source-repo | ||
LAST_COMMIT=$(git rev-parse HEAD) | ||
cd ../pingcap-docs-website-scaffold | ||
git status | ||
git config user.name github-actions | ||
git config user.email [email protected] | ||
git add -A | ||
git commit -m "Update files from https://github.com/${{ github.event.inputs.PR_REPO }}/pull/${{ github.event.inputs.PR_NUMBER }}/commits/$LAST_COMMIT" || echo "No changes to commit" | ||
./sync.sh | ||
git push | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
This file contains 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 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 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,99 @@ | ||
#!/bin/bash | ||
|
||
set -e | ||
|
||
# Get the directory of this script | ||
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) | ||
cd "$SCRIPT_DIR" | ||
|
||
# Verify if jq is installed and GITHUB_TOKEN is set. | ||
which jq &> /dev/null || (echo "Error: jq is not installed, but is required for this script to parse JSON response from GitHub API. You can download and install jq from https://stedolan.github.io/jq/download/" && exit 1) | ||
test -n "$GITHUB_TOKEN" || (echo "Error: The GITHUB_TOKEN environment variable is not set. This token is required for accessing the GitHub API and needs to have the repo scope." && exit 1) | ||
|
||
# If the branch name is not provided as an argument, use the current branch. | ||
BRANCH_NAME=${1:-$(git branch --show-current)} | ||
|
||
# Extract product, repo owner, repo name, and PR number from the branch name. The name pattern is r"preview(-cloud|-operator)?/pingcap/docs(-cn|-tidb-operator)?/[0-9]+)" | ||
PREVIEW_PRODUCT=$(echo "$BRANCH_NAME" | cut -d'/' -f1) | ||
REPO_OWNER=$(echo "$BRANCH_NAME" | cut -d'/' -f2) | ||
REPO_NAME=$(echo "$BRANCH_NAME" | cut -d'/' -f3) | ||
PR_NUMBER=$(echo "$BRANCH_NAME" | cut -d'/' -f4) | ||
|
||
# Get the base branch of this PR <https://docs.github.com/en/rest/pulls/pulls?apiVersion=2022-11-28#get-a-pull-request> | ||
BASE_BRANCH=$(curl -fsSL -H "Authorization: token $GITHUB_TOKEN" \ | ||
"https://api.github.com/repos/$REPO_OWNER/$REPO_NAME/pulls/$PR_NUMBER" | \ | ||
jq -r '.base.ref') | ||
|
||
# Ensure that BASE_BRANCH is not empty | ||
test -n "$BASE_BRANCH" || (echo "Error: Cannot get BASE_BRANCH." && exit 1) | ||
|
||
# Determine product name based on the PREVIEW_PRODUCT | ||
case "$PREVIEW_PRODUCT" in | ||
preview) | ||
DIR_SUFFIX="tidb/${BASE_BRANCH}" | ||
;; | ||
preview-cloud) | ||
DIR_SUFFIX="tidbcloud/master" | ||
;; | ||
preview-operator) | ||
DIR_SUFFIX="tidb-in-kubernetes/${BASE_BRANCH}" | ||
;; | ||
*) | ||
echo "Error: Branch name must start with preview/, preview-cloud/, or preview-operator/." | ||
exit 1 | ||
;; | ||
esac | ||
|
||
# Define sync tasks for different repos | ||
case "$REPO_NAME" in | ||
docs) | ||
# sync all modified or added files from root dir to markdown-pages/en/ | ||
SYNC_TASKS=("./,en/") | ||
;; | ||
docs-cn) | ||
# sync all modified or added files from root dir to markdown-pages/zh/ | ||
SYNC_TASKS=("./,zh/") | ||
;; | ||
docs-tidb-operator) | ||
# Task 1: sync all modified or added files from en/ to markdown-pages/en/ | ||
# Task 2: sync all modified or added files from zh/ to markdown-pages/zh/ | ||
SYNC_TASKS=("en/,en/" "zh/,zh/") | ||
;; | ||
*) | ||
echo "Error: Invalid repo name. Only docs, docs-cn, and docs-tidb-operator are supported." | ||
exit 1 | ||
;; | ||
esac | ||
|
||
REPO_DIR="src/$REPO_NAME" | ||
|
||
# Clone repo if it doesn't exist already | ||
test -e "$REPO_DIR/.git" || git clone "https://github.com/$REPO_OWNER/$REPO_NAME.git" "$REPO_DIR" | ||
|
||
# --update-head-ok: By default git fetch refuses to update the head which corresponds to the current branch. This flag disables the check. This is purely for the internal use for git pull to communicate with git fetch, and unless you are implementing your own Porcelain you are not supposed to use it. | ||
# use --force to overwrite local branch when remote branch is force pushed | ||
git -C "$REPO_DIR" fetch origin "$BASE_BRANCH" #<https://stackoverflow.com/questions/33152725/git-diff-gives-ambigious-argument-error> | ||
git -C "$REPO_DIR" fetch origin pull/"$PR_NUMBER"/head:PR-"$PR_NUMBER" --update-head-ok --force | ||
git -C "$REPO_DIR" checkout PR-"$PR_NUMBER" | ||
|
||
# Perform sync tasks | ||
for TASK in "${SYNC_TASKS[@]}"; do | ||
|
||
SRC_DIR=$(echo "$TASK" | cut -d',' -f1) | ||
DEST_DIR="markdown-pages/$(echo "$TASK" | cut -d',' -f2)/$DIR_SUFFIX" | ||
mkdir -p "$DEST_DIR" | ||
# Only sync modified or added files | ||
git -C "$REPO_DIR" diff --merge-base --name-only --diff-filter=AM origin/"$BASE_BRANCH" "$SRC_DIR" | tee /dev/fd/2 | \ | ||
rsync -av --files-from=- "$REPO_DIR" "$DEST_DIR" | ||
|
||
done | ||
|
||
test -n "$TEST" && echo "Test mode, exiting..." && exit 0 | ||
|
||
# Get the current commit SHA | ||
CURRENT_COMMIT=$(git -C "$REPO_DIR" rev-parse HEAD) | ||
|
||
# Handle untracked files | ||
git add . | ||
# Commit changes, if any | ||
git commit -m "Preview PR https://github.com/$REPO_OWNER/$REPO_NAME/pull/$PR_NUMBER and this preview is triggered from commit https://github.com/$REPO_OWNER/$REPO_NAME/pull/$PR_NUMBER/commits/$CURRENT_COMMIT" || echo "No changes to commit" |
This file was deleted.
Oops, something went wrong.
cd75352
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
pingcap-docs-website-scaffold – ./
pingcap-docs-website-scaffold-ran-huang.vercel.app
pingcap-docs-website-scaffold.vercel.app
pingcap-docs-website-scaffold-git-main-ran-huang.vercel.app