Skip to content

Commit

Permalink
Merge branch 'Oreoxmt:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
ran-huang authored Sep 1, 2023
2 parents 18d2491 + a7480a7 commit 7682d67
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 35 deletions.
47 changes: 47 additions & 0 deletions .github/git_push.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/bin/bash

set -e

BRANCH="$1"

# Try to push changes to the remote branch
# Call handle_push_failure() if push fails
try_push() {
git push || handle_push_failure
}

# Handle merge conflicts during push
# Force push if all conflicts are in markdown-pages
# Otherwise print error and exit
handle_conflict() {
declare -a CONFLICT_FILES

while read -r file; do
if [[ ! "$file" =~ ^markdown-pages/ ]]; then
CONFLICT_FILES+=("$file")
fi
done < <(git diff --name-only "$BRANCH" "origin/$BRANCH")

if [[ ${#CONFLICT_FILES[@]} -eq 0 ]]; then
echo "All conflicts are in the markdown-pages folder. Force pushing."
git push -f
else
echo "Please resolve conflicts manually. Conflict files are:"
printf '%s\n' "${CONFLICT_FILES[@]}"
exit 1
fi
}

# Handle failure during git push
# Try pull first, then push
# If pull fails, call handle_conflict()
handle_push_failure() {
if git pull origin "$BRANCH" --no-rebase; then
git push
else
git fetch
handle_conflict
fi
}

try_push
12 changes: 9 additions & 3 deletions .github/workflows/preview-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,25 @@ on:
- preview-operator/**
workflow_dispatch:

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
sync:
preview-pr:
runs-on: ubuntu-latest
permissions:
contents: write

steps:
- name: Check out destination repository
- name: Checkout current repo
uses: actions/checkout@v3

- name: Sync PR
run: |
git config user.name github-actions
git config user.email [email protected]
./sync.sh
git push
./.github/git_push.sh ${{ github.ref_name }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
50 changes: 18 additions & 32 deletions .github/workflows/update-files.yml
Original file line number Diff line number Diff line change
@@ -1,44 +1,30 @@
name: Update scaffold
name: Sync scaffold

on:
workflow_dispatch:
schedule:
- cron: "0 0 */15 * *"
- cron: "0 0 */15 * *" # every 15 days

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
copy_files:

sync_scaffold:
runs-on: ubuntu-latest
permissions:
contents: write

steps:
- name: Check out destination repository (pingcap-docs-website-scaffold)
uses: actions/checkout@v3
with:
path: pingcap-docs-website-scaffold

- name: Check out source repository (pingcap/docs-staging)
- name: Checkout current repo
uses: actions/checkout@v3
with:
repository: 'pingcap/docs-staging'
path: docs-staging

- name: Run copy script
run: |
#!/bin/bash
src="docs-staging/markdown-pages/"
dest="pingcap-docs-website-scaffold/markdown-pages/"
rsync -av --delete --checksum --include='*/' --include='TOC.md' --include='_index.md' --include='_docHome.md' --exclude='*' "$src" "$dest"

cp docs-staging/docs.json pingcap-docs-website-scaffold/
- name: Commit and push changes
- name: Run sync_scaffold script
run: |
cd docs-staging
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/pingcap/docs-staging/commit/$last_commit" || echo "No changes to commit"
git push
git config user.name "Docsite Preview Bot"
git config user.email ""
./sync_scaffold.sh
./.github/git_push.sh ${{ github.ref_name }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
95 changes: 95 additions & 0 deletions sync_scaffold.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#!/bin/bash

set -e

# The repository to sync from
REPO_URL="https://github.com/pingcap/docs-staging"
CLONE_DIR="temp/docs-staging"

# Files to sync from the repository
SYNC_FILES=("TOC.md" "_index.md" "_docHome.md")
SYNC_JSON_FILE="docs.json"

# Get the current script's directory and change to it
SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
cd "$SCRIPT_DIR"

# Remove CLONE_DIR if it is a directory without .git
ensure_git_dir() {
if [ -d "$CLONE_DIR" ] && [ ! -e "$CLONE_DIR/.git" ]; then
rm -rf "$CLONE_DIR"
fi
}

# Checkout a specific branch or commit and pull the latest changes
checkout_pull_ref() {
TARGET_REF="$1"
git -C "$CLONE_DIR" checkout "$TARGET_REF"
git -C "$CLONE_DIR" pull origin "$TARGET_REF"
}

# Shallow clone, checkout, and pull the default branch
clone_checkout_default() {
TARGET_REF=$1
ensure_git_dir
# If CLONE_DIR is not a git repository, shallow clone it
if [ ! -e "$CLONE_DIR/.git" ]; then
git clone --depth 1 "$REPO_URL" "$CLONE_DIR"
fi
checkout_pull_ref "$TARGET_REF"
}

# Clone, checkout, and pull a specific branch or commit
clone_checkout_spec() {
TARGET_REF=$1
ensure_git_dir
# If CLONE_DIR is not a git repository, clone it
if [ ! -e "$CLONE_DIR/.git" ]; then
git clone "$REPO_URL" "$CLONE_DIR"
fi
# If CLONE_DIR is a shallow clone, convert it to a normal clone
if [ -f "$CLONE_DIR/.git/shallow" ]; then
echo "Converting a shallow clone to a normal clone..."
git -C "$CLONE_DIR" fetch --unshallow
git -C "$CLONE_DIR" config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
git -C "$CLONE_DIR" fetch origin
fi
checkout_pull_ref "$TARGET_REF"
}

# Parse command-line arguments
if [ -n "$1" ]; then
# If the first command-line argument is provided, sync from a specific branch or commit.
TARGET_REF="$1"
echo "Syncing from $REPO_URL/commit/$TARGET_REF"
clone_checkout_spec "$TARGET_REF"
else
# If the first command-line argument is not provided, sync from the default branch (main).
TARGET_REF="main"
echo "Syncing from $REPO_URL/commit/$TARGET_REF"
clone_checkout_default "$TARGET_REF"
fi

# Set source and destination directories for rsync
SRC="$CLONE_DIR/markdown-pages/"
DEST="markdown-pages/"

# Create an array of --include options for rsync
INCLUDES=('--include=*/')
for file in "${SYNC_FILES[@]}"; do
INCLUDES+=("--include=$file")
done

# Synchronize SRC and DEST
rsync -avm --checksum "${INCLUDES[@]}" --exclude='*' "$SRC" "$DEST"

# Copy SYNC_JSON_FILE from CLONE_DIR to the current directory
cp "$CLONE_DIR/$SYNC_JSON_FILE" "./$SYNC_JSON_FILE"

# Exit if TEST is set and not empty
test -n "$TEST" && echo "Test mode, exiting..." && exit 0

## Commit changes with the commit SHA from the cloned repository
CURRENT_SHA=$(git -C "$CLONE_DIR" rev-parse HEAD)
git add . || true
git commit -m "Sync the scaffold from $REPO_URL/commit/$CURRENT_SHA" || echo "No changes detected"
1 change: 1 addition & 0 deletions temp/docs-staging
Submodule docs-staging added at 00a140

1 comment on commit 7682d67

@vercel
Copy link

@vercel vercel bot commented on 7682d67 Sep 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.