1
0
mirror of synced 2025-12-30 12:02:01 -05:00
Files
docs/.github/workflows/site-policy-sync.yml
James M. Greene 3d88cae50b Add permission matrices to all Actions workflows (#23563)
* Add permission matrices to all Actions workflows

Also cleanup a few token references

* Add actions:read permissions for CodeQL

* Add prs:read permissions for unit test workflow
2021-12-14 04:37:36 +00:00

81 lines
3.4 KiB
YAML

name: Site policy sync
# **What it does**: Updates our site-policy repo when changes happen to site policy docs.
# **Why we have it**: We want keep site-policy repo up to date.
# **Who does it impact**: site-policy-admins and Developer Policy teams.
# Controls when the action will run.
on:
# Triggers the workflow pull requests merged to the main branch
pull_request:
branches:
- main
types:
- closed
paths:
- 'content/github/site-policy/**'
workflow_dispatch:
permissions:
contents: read
jobs:
sync:
name: Get the latest docs
if: github.event_name == 'workflow_dispatch' || (github.event.pull_request.merged == true && github.repository == 'github/docs-internal')
runs-on: ubuntu-latest
steps:
- name: checkout docs-internal
uses: actions/checkout@ec3a7ce113134d7a93b817d10a8272cb61118579
- name: checkout public site-policy
uses: actions/checkout@ec3a7ce113134d7a93b817d10a8272cb61118579
with:
repository: github/site-policy
token: ${{ secrets.API_TOKEN_SITEPOLICY }}
fetch-depth: ''
path: public-repo
- name: Commits internal policies to copy of public repo with descriptive message from triggering PR title
env:
PR_TITLE: ${{ github.event.pull_request.title }}
run: |
cd public-repo
git config --local user.name 'site-policy-bot'
git config --local user.email 'site-policy-bot@github.com'
rm -rf Policies
cp -r ../content/github/site-policy Policies
git status
git checkout -b automated-sync-$GITHUB_RUN_ID
git add .
echo PR_TITLE: $PR_TITLE
[[ ! -z $PR_TITLE ]] && DESCRIPTION="${PR_TITLE}" || DESCRIPTION="Update manually triggered by workflow"
echo "DESCRIPTION=$DESCRIPTION" >> $GITHUB_ENV
git commit -m "$(echo $DESCRIPTION)"
- name: If there are changes to push, create a pull request in the public repo using the gh command line tool, then immediately merge the PR and delete the branch
id: createAndMergePullRequest
env:
GITHUB_TOKEN: ${{ secrets.API_TOKEN_SITEPOLICY }}
run: |
cd public-repo
git config --local user.name 'site-policy-bot'
git config --local user.email 'site-policy-bot@github.com'
DIFF=$(git diff --name-status --summary HEAD^..HEAD)
NUM_FILES_CHANGED=$(git diff --name-only HEAD^..HEAD | wc -l)
[[ $NUM_FILES_CHANGED -ge 2 ]] && TITLE="Sync changes from GitHub Docs" || TITLE=$(echo $DIFF | sed -e 's/^A\s/Added /g;s/^D\s/Deleted /g;s/^C\s/Copied /g;s/^M\s/Modified /g;s/^R100\s/Renamed /g;')
if [[ ! -z $TITLE ]]
then
echo -e "This is an automated pull request to sync changes from GitHub Docs.\n\nDiff summary:\n\n${DIFF}" > msg
git push --set-upstream origin automated-sync-$GITHUB_RUN_ID
PR_URL=$(gh pr create --title "${TITLE}" --body-file msg --head automated-sync-$GITHUB_RUN_ID --base main --repo github/site-policy)
gh pr diff ${PR_URL}
gh pr merge ${PR_URL} --merge --delete-branch
else
echo "No updates to push to the public repo"
fi
- name: Delete remote updates branch if previous step failed
if: failure() && steps.createAndMergePullRequest.outcome == 'failure'
run: git push github/site-policy --delete automated-sync-$GITHUB_RUN_ID