Skip to content

Commit

Permalink
improve docs sync script
Browse files Browse the repository at this point in the history
  • Loading branch information
ReneWerner87 committed Jul 9, 2023
1 parent 032bde9 commit fb9b57f
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 24 deletions.
78 changes: 55 additions & 23 deletions .github/scripts/sync_docs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,69 @@

# Some env variables
BRANCH="master"
MAJOR_VERSION="v2"
REPO_URL="github.com/gofiber/docs.git"
AUTHOR_EMAIL="github-actions[bot]@users.noreply.github.com"
AUTHOR_USERNAME="github-actions[bot]"
VERSION_FILE="versions.json"
REPO_DIR="core"
COMMIT_URL="https://github.com/gofiber/fiber"
DOCUSAURUS_COMMAND="npm run docusaurus -- docs:version"

# Set commit author
git config --global user.email "${AUTHOR_EMAIL}"
git config --global user.name "${AUTHOR_USERNAME}"

git clone https://${TOKEN}@${REPO_URL} fiber-docs

# Handle push event
if [ "$EVENT" == "push" ]; then
latest_commit=$(git rev-parse --short HEAD)
log_output=$(git log --oneline ${BRANCH} HEAD~1..HEAD --name-status -- docs/)
if [[ $log_output != "" ]]; then
cp -a docs/* fiber-docs/docs/${REPO_DIR}
fi

# Handle release event
elif [ "$EVENT" == "release" ]; then
major_version="${TAG_NAME%%.*}"

# Form new version name
new_version="${major_version}.x"

cd fiber-docs/ || true
npm ci

# Check if contrib_versions.json exists and modify it if required
if [[ -f $VERSION_FILE ]]; then
jq --arg new_version "$new_version" 'del(.[] | select(. == $new_version))' $VERSION_FILE >temp.json && mv temp.json $VERSION_FILE
jq -S . ${VERSION_FILE} >temp.json && mv temp.json ${VERSION_FILE}
fi

# Run docusaurus versioning command
$DOCUSAURUS_COMMAND "${new_version}"
fi

# Push changes
cd fiber-docs/ || true
git add .
if [[ $EVENT == "push" ]]; then
latest_commit=$(git rev-parse --short HEAD)
log_output=$(git log --oneline ${BRANCH} HEAD~1..HEAD --name-status -- docs/)

if [[ $log_output != "" ]]; then
git clone https://${TOKEN}@${REPO_URL} fiber-docs
cp -a docs/* fiber-docs/docs/core

# Push changes for next docs
cd fiber-docs/ || return
git add .
git commit -m "Add docs from https://github.com/gofiber/fiber/commit/${latest_commit}"
git push https://${TOKEN}@${REPO_URL}
fi
git commit -m "Add docs from ${COMMIT_URL}/commit/${latest_commit}"
elif [[ $EVENT == "release" ]]; then
latest_tag=$(git describe --tags --abbrev=0)

# Push changes for stable docs
git clone https://${TOKEN}@${REPO_URL} fiber-docs
cd fiber-docs/ || return
cp -a docs/core/* versioned_docs/version-${MAJOR_VERSION}.x
git add .
git commit -m "Sync docs for ${latest_tag} release"
git push https://${TOKEN}@${REPO_URL}
git commit -m "Sync docs for release ${COMMIT_URL}/releases/tag/${TAG_NAME}"
fi

MAX_RETRIES=5
DELAY=5
retry=0

while ((retry < MAX_RETRIES)); do
git push https://${TOKEN}@${REPO_URL} && break
retry=$((retry + 1))
git pull --rebase
sleep $DELAY
done

if ((retry == MAX_RETRIES)); then
echo "Failed to push after $MAX_RETRIES attempts. Exiting with 1."
exit 1
fi
11 changes: 10 additions & 1 deletion .github/workflows/sync-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:
paths:
- 'docs/**'
release:
types: [published]
types: [ published ]

jobs:
sync-docs:
Expand All @@ -20,8 +20,17 @@ jobs:
ref: ${{ github.event.pull_request.head.sha }}
fetch-depth: 2

- name: Setup Node.js environment
uses: actions/setup-node@v3
with:
node-version: '18'

- name: Install JQ
run: sudo apt-get install jq

- name: Sync docs
run: ./.github/scripts/sync_docs.sh
env:
EVENT: ${{ github.event_name }}
TAG_NAME: ${{ github.ref_name }}
TOKEN: ${{ secrets.DOC_SYNC_TOKEN }}

0 comments on commit fb9b57f

Please sign in to comment.