Using a unique branch name to avoid conflicts #4
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
name: Generate Python Documentation | |
on: | |
push: | |
branches: [main] | |
pull_request: | |
branches: [main] | |
release: | |
types: [published] | |
workflow_dispatch: | |
jobs: | |
build_python_docs: | |
name: Build Python Documentation | |
runs-on: ubuntu-latest | |
permissions: | |
contents: write | |
pull-requests: write # Grant permissions to create and manage pull requests | |
steps: | |
- name: Checkout Stochtree Monorepo | |
uses: actions/checkout@v4 | |
with: | |
repository: 'StochasticTree/stochtree' | |
submodules: 'recursive' | |
- name: Setup Python 3.10 | |
uses: actions/setup-python@v5 | |
with: | |
python-version: '3.10' | |
cache: 'pip' | |
- name: Install Package with Relevant Dependencies | |
run: | | |
pip install --upgrade pip | |
pip install -r python_docs/requirements.txt | |
pip install . | |
- name: Build HTML Documentation | |
run: | | |
sphinx-build -M html python_docs/source/ python_docs/_build/ | |
- name: Prepare Documentation Directory | |
run: | | |
# Ensure the target directory exists | |
mkdir -p docs/python-documentation | |
# Clear existing content, if present | |
if [ -d docs/python-documentation ]; then rm -rf docs/python-documentation/*; fi | |
- name: Move Documentation to Docs Directory | |
run: | | |
cp -r python_docs/_build/html/* docs/python-documentation/ | |
- name: Commit and Push Documentation to New Branch | |
run: | | |
# Configure Git to use the GitHub actor for commits | |
git config --global user.name "${{ github.actor }}" | |
git config --global user.email "${{ github.actor }}@users.noreply.github.com" | |
# Create a unique branch name using a fixed prefix and GitHub run ID | |
BRANCH_NAME="python-docs-temp-${{ github.run_id }}" | |
# Create and switch to the new branch | |
git checkout -b $BRANCH_NAME | |
# Add the generated documentation and commit | |
git add docs/python-documentation | |
git commit -m "Updated the Python documentation" | |
# Push the changes to the new unique branch | |
git push -u origin $BRANCH_NAME | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
- name: Create Pull Request Using GitHub CLI | |
run: | | |
# Use the unique branch name for PR creation | |
BRANCH_NAME="python-docs-temp-${{ github.run_id }}" | |
gh pr create --title "Updated the Python documentation" --body "This PR updates the Python documentation." --base main --head $BRANCH_NAME | |
env: | |
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
- name: Enable Auto-Merge on the PR | |
run: | | |
BRANCH_NAME="python-docs-temp-${{ github.run_id }}" | |
PR_NUMBER=$(gh pr list --head $BRANCH_NAME --json number --jq '.[0].number') | |
gh pr merge $PR_NUMBER --auto --merge | |
env: | |
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
- name: Delete Branch After Merge | |
if: success() # Only delete if previous steps were successful | |
run: | | |
BRANCH_NAME="python-docs-temp-${{ github.run_id }}" | |
gh api repos/:owner/:repo/git/refs/heads/$BRANCH_NAME -X DELETE | |
env: | |
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
owner: ${{ github.repository_owner }} | |
repo: ${{ github.event.repository.name }} |