Co-authored-by: mc <42146119+mchammer01@users.noreply.github.com> Co-authored-by: Melanie Yarbrough <11952755+myarb@users.noreply.github.com>
17 KiB
title, shortTitle, intro, versions, type, topics
| title | shortTitle | intro | versions | type | topics | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Using the GitHub CLI on a runner | Use the GitHub CLI on a runner | How to use advanced {% data variables.product.prodname_actions %} features for continuous integration (CI). |
|
how_to |
|
{% data reusables.actions.enterprise-github-hosted-runners %}
Example overview
{% data reusables.actions.example-workflow-intro-ci %} When this workflow is triggered, it automatically runs a script that checks whether the {% data variables.product.prodname_dotcom %} Docs site has any broken links. If any broken links are found, the workflow uses the {% data variables.product.prodname_dotcom %} CLI to create a {% data variables.product.prodname_dotcom %} issue with the details.
{% data reusables.actions.example-diagram-intro %}
Features used in this example
{% data reusables.actions.example-table-intro %}
| Feature | Implementation |
|---|---|
| {% data reusables.actions.cron-table-entry %} | |
| {% data reusables.actions.permissions-table-entry %} | |
| {% data reusables.actions.if-conditions-table-entry %} | |
| {% data reusables.actions.secrets-table-entry %} | |
| {% data reusables.actions.checkout-action-table-entry %} | |
| {% data reusables.actions.setup-node-table-entry %} | |
| Using a third-party action | peter-evans/create-issue-from-file |
| Running shell commands on the runner | run |
| Running a script on the runner | Using script/check-english-links.js |
| Generating an output file | Piping the output using the > operator |
| Checking for existing issues using {% data variables.product.prodname_cli %} | gh issue list |
| Commenting on an issue using {% data variables.product.prodname_cli %} | gh issue comment |
Example workflow
{% data reusables.actions.example-docs-engineering-intro %} check-all-english-links.yml.
{% data reusables.actions.note-understanding-example %}
name: Check all English links
# **What it does**: This script once a day checks all English links and reports in issues.
# **Why we have it**: We want to know if any links break.
# **Who does it impact**: Docs content.
on:
workflow_dispatch:
schedule:
- cron: '40 19 * * *' # once a day at 19:40 UTC / 11:40 PST
permissions:
contents: read
issues: write
jobs:
check_all_english_links:
name: Check all links
if: github.repository == 'github/docs-internal'
runs-on: ubuntu-latest
env:
GITHUB_TOKEN: {% raw %}${{ secrets.DOCUBOT_READORG_REPO_WORKFLOW_SCOPES }}{% endraw %}
FIRST_RESPONDER_PROJECT: Docs content first responder
REPORT_AUTHOR: docubot
REPORT_LABEL: broken link report
REPORT_REPOSITORY: github/docs-content
steps:
- name: Check out repo's default branch
uses: {% data reusables.actions.action-checkout %}
- name: Setup Node
uses: {% data reusables.actions.action-setup-node %}
with:
node-version: 16.13.x
cache: npm
- name: npm ci
run: npm ci
- name: npm run build
run: npm run build
- name: Run script
run: |
script/check-english-links.js > broken_links.md
# check-english-links.js returns 0 if no links are broken, and 1 if any links
# are broken. When an Actions step's exit code is 1, the action run's job status
# is failure and the run ends. The following steps create an issue for the
# broken link report only if any links are broken, so {% raw %}`if: ${{ failure() }}`{% endraw %}
# ensures the steps run despite the previous step's failure of the job.
- if: {% raw %}${{ failure() }}{% endraw %}
name: Get title for issue
id: check
{%- ifversion actions-save-state-set-output-envs %}
run: echo "title=$(head -1 broken_links.md)" >> $GITHUB_OUTPUT
{%- else %}
run: echo "::set-output name=title::$(head -1 broken_links.md)"
{%- endif %}
- if: {% raw %}${{ failure() }}{% endraw %}
name: Create issue from file
id: broken-link-report
uses: peter-evans/create-issue-from-file@ceef9be92406ace67ab5421f66570acf213ec395
with:
token: {% raw %}${{ env.GITHUB_TOKEN }}{% endraw %}
title: {% raw %}${{ steps.check.outputs.title }}{% endraw %}
content-filepath: ./broken_links.md
repository: {% raw %}${{ env.REPORT_REPOSITORY }}{% endraw %}
labels: {% raw %}${{ env.REPORT_LABEL }}{% endraw %}
- if: {% raw %}${{ failure() }}{% endraw %}
name: Close and/or comment on old issues
env:
{% raw %}NEW_REPORT_URL: 'https://github.com/${{ env.REPORT_REPOSITORY }}/issues/${{ steps.broken-link-report.outputs.issue-number }}'{% endraw %}
run: |
gh alias set list-reports "issue list \
--repo {% raw %}${{ env.REPORT_REPOSITORY }} \{% endraw %}
--author {% raw %}${{ env.REPORT_AUTHOR }} \{% endraw %}
--label {% raw %}'${{ env.REPORT_LABEL }}'"{% endraw %}
# Link to the previous report from the new report that triggered this
# workflow run.
previous_report_url=$(gh list-reports \
--state all \
--limit 2 \
--json url \
--jq '.[].url' \
| grep -v {% raw %}${{ env.NEW_REPORT_URL }}{% endraw %} | head -1)
gh issue comment {% raw %}${{ env.NEW_REPORT_URL }}{% endraw %} --body "⬅️ [Previous report]($previous_report_url)"
# If an old report is open and assigned to someone, link to the newer
# report without closing the old report.
for issue_url in $(gh list-reports \
--json assignees,url \
--jq '.[] | select (.assignees != []) | .url'); do
if [ "$issue_url" != {% raw %}"${{ env.NEW_REPORT_URL }}"{% endraw %} ]; then
gh issue comment $issue_url --body "➡️ [Newer report]({% raw %}${{ env.NEW_REPORT_URL }}{% endraw %})"
fi
done
# Link to the newer report from any older report that is still open,
# then close the older report and remove it from the first responder's
# project board.
for issue_url in $(gh list-reports \
--search 'no:assignee' \
--json url \
--jq '.[].url'); do
if [ "$issue_url" != {% raw %}"${{ env.NEW_REPORT_URL }}"{% endraw %} ]; then
gh issue comment $issue_url --body "➡️ [Newer report]({% raw %}${{ env.NEW_REPORT_URL }})"{% endraw %}
gh issue close $issue_url
gh issue edit $issue_url --remove-project "{% raw %}${{ env.FIRST_RESPONDER_PROJECT }}"{% endraw %}
fi
done
Understanding the example
{% data reusables.actions.example-explanation-table-intro %}
| Code | Explanation |
|---|---|
|
{% data reusables.actions.explanation-name-key %} |
|
Defines the
|
|
Modifies the default permissions granted to |
|
Groups together all the jobs that run in the workflow file. |
|
Defines a job with the ID |
|
Only run the |
|
Configures the job to run on an Ubuntu Linux runner. This means that the job will execute on a fresh virtual machine hosted by {% data variables.product.prodname_dotcom %}. For syntax examples using other runners, see "AUTOTITLE." |
|
Creates custom environment variables, and redefines the built-in |
|
Groups together all the steps that will run as part of the |
|
The |
|
This step uses the |
|
The |
|
This |
|
If the |
|
Uses the |
|
Uses
|
|
If an issue from a previous run is open and assigned to someone, then use |
|
If an issue from a previous run is open and is not assigned to anyone, then:
|
Next steps
{% data reusables.actions.learning-actions %}
