GH Actions: fix PR comment conundrum #177
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: Publish wiki | |
on: | |
push: | |
branches: | |
- 'main' | |
paths: | |
- .github/workflows/publish-wiki.yml | |
- .github/workflows/pr-comment.yml | |
- build/wiki-code-samples/** | |
- build/wiki-command-replacer.sh | |
- wiki/** | |
# Do a dry-run (check, no deploy) for PRs. | |
pull_request: | |
paths: | |
- .github/workflows/publish-wiki.yml | |
- .github/workflows/pr-comment.yml | |
- build/wiki-code-samples/** | |
- build/wiki-command-replacer.sh | |
- wiki/** | |
# Allow running this workflow manually from the Actions tab. | |
workflow_dispatch: | |
# Allow this workflow to be triggered from outside. | |
repository_dispatch: | |
types: | |
- 'phpcs-release' | |
# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued. | |
# However, do NOT cancel in-progress runs as we want to allow these production deployments to complete. | |
concurrency: | |
group: "publish-wiki" | |
cancel-in-progress: false | |
jobs: | |
publish-wiki: | |
name: "Publish Wiki" | |
if: github.repository == 'PHPCSStandards/PHP_CodeSniffer-documentation' | |
runs-on: ubuntu-latest | |
permissions: | |
# Needed for the commit to the wiki. | |
contents: write | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
- name: Install PHP | |
uses: shivammathur/setup-php@v2 | |
with: | |
php-version: 'latest' | |
ini-values: error_reporting=-1, display_errors=On, log_errors_max_len=0 | |
tools: phpcs, phpcbf | |
coverage: none | |
# Make sure we've gotten the latest PHPCS version from setup-php. | |
- name: Retrieve latest release info | |
uses: octokit/[email protected] | |
id: get_latest_release | |
with: | |
route: GET /repos/PHPCSStandards/PHP_CodeSniffer/releases/latest | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
- name: Grab latest tag name from API response | |
id: latest_version | |
run: | | |
echo "TAG=${{ fromJson(steps.get_latest_release.outputs.data).tag_name }}" >> "$GITHUB_OUTPUT" | |
- name: Grab the version | |
id: phar_version | |
# yamllint disable-line rule:line-length | |
run: echo "VERSION=$(phpcs --version | grep --only-matching --max-count=1 --extended-regexp '\b[0-9]+(\.[0-9]+)+')" >> "$GITHUB_OUTPUT" | |
- name: Fail the build if the PHAR is not the correct version | |
if: ${{ steps.phar_version.outputs.VERSION != steps.latest_version.outputs.TAG }} | |
run: exit 1 | |
# ################################################################################ | |
# Update Wiki files. | |
# ################################################################################ | |
- name: Install DocToc table of contents generator | |
run: npm install -g doctoc | |
- name: Copy wiki files to temporary location | |
shell: bash | |
run: cp -v -a wiki _wiki | |
- name: Find / replace output example placeholders | |
shell: bash | |
run: build/wiki-command-replacer.sh | |
- name: Update tables of contents | |
run: doctoc ./_wiki/ --github --maxlevel 4 --update-only | |
- name: Re-run tables of contents with different settings for specific file | |
run: doctoc ./_wiki/Version-4.0-User-Upgrade-Guide.md --github --maxlevel 3 --update-only | |
- name: Preface markdown files with warning not to edit in place | |
shell: bash | |
# yamllint disable rule:line-length | |
# shellcheck disable=SC2016 | |
run: > | |
find ./_wiki/ -name "*.md*" | |
-exec sed -i | |
'1i\<!--\nWARNING: DO NOT EDIT THIS FILE IN THE WIKI.\nThis wiki is updated from the https://github.com/PHPCSStandards/PHP_CodeSniffer-documentation repository.\nSubmit a PR to that repository updating the relevant file in the /wiki/ subdirectory instead.\n-->\n' {} \; | |
# yamllint enable rule:line-length | |
# ################################################################################ | |
# Dry-run/PRs: upload artifact with pre-processed files and post comment in PR. | |
# ################################################################################ | |
# Retention is normally 90 days, but this artifact is only to help with reviewing PRs, | |
# especially when new output blocks are added or the (workflow) code for existing ones | |
# is updated. All in all, no need to keep the artifact for more than a few days. | |
- name: "[PR only] Upload the preprocessed wiki files as an artifact" | |
if: ${{ github.event_name == 'pull_request' }} | |
id: artifact | |
uses: actions/upload-artifact@v4 | |
with: | |
name: wiki-files | |
path: ./_wiki | |
if-no-files-found: error | |
retention-days: 10 | |
# ################################################################################ | |
# Deploy to the wiki in the PHPCS repo. | |
# ################################################################################ | |
- name: Check GitHub Git Operations status | |
uses: crazy-max/ghaction-github-status@v4 | |
with: | |
git_threshold: partial_outage | |
- name: Deploy to wiki | |
if: ${{ github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository }} | |
uses: Andrew-Chen-Wang/[email protected] | |
env: | |
COMMIT_MSG: ${{ github.event.head_commit.message }} | |
DEFAULT_COMMIT_MSG: "Update wiki ${{ github.sha }}" | |
with: | |
strategy: 'clone' | |
path: '_wiki/' | |
commit-message: ${{ env.COMMIT_MSG != '' && env.COMMIT_MSG || env.DEFAULT_COMMIT_MSG }} | |
repository: PHPCSStandards/PHP_CodeSniffer | |
token: ${{ secrets.PHPCS_PUSH_TO_WIKI_TOKEN }} | |
dry-run: ${{ github.event_name == 'pull_request' }} | |
disable-empty-commits: true | |
preprocess: false | |
# ################################################################################ | |
# Dry-run/PRs: save PR info for use in Pull Request Comment workflow. | |
# ################################################################################ | |
- name: Create temporary directory | |
if: ${{ github.event_name == 'pull_request' }} | |
run: mkdir -p ./pr | |
- name: Save PR number | |
if: ${{ github.event_name == 'pull_request' }} | |
env: | |
PR_NUMBER: ${{ github.event.number }} | |
run: echo "${PR_NUMBER}" > ./pr/pr_number | |
- name: Save artifact URL | |
if: ${{ github.event_name == 'pull_request' }} | |
env: | |
ARTIFACT_URL: ${{ steps.artifact.outputs.artifact-url }} | |
run: echo "${ARTIFACT_URL}" > ./pr/artifact_url | |
- name: Upload PR info as artifact | |
if: ${{ github.event_name == 'pull_request' }} | |
uses: actions/upload-artifact@v4 | |
with: | |
name: pr_info | |
path: pr/ |