Update examples

This commit is contained in:
Peter Evans 2019-11-09 22:46:01 +09:00
parent 1281ebd51a
commit 11baa5dc07

View file

@ -5,11 +5,14 @@
- [Keep Go up to date](#keep-go-up-to-date) - [Keep Go up to date](#keep-go-up-to-date)
- [Spider and download a website](#spider-and-download-a-website) - [Spider and download a website](#spider-and-download-a-website)
- [Use case: Create a pull request to update X by calling the GitHub API](#use-case-create-a-pull-request-to-update-x-by-calling-the-github-api) - [Use case: Create a pull request to update X by calling the GitHub API](#use-case-create-a-pull-request-to-update-x-by-calling-the-github-api)
- [Call the GitHub API from an external service](#call-the-github-api-from-an-external-service)
- [Call the GitHub API from another GitHub Actions workflow](#call-the-github-api-from-another-github-actions-workflow)
- [Use case: Create a pull request to modify/fix pull requests](#use-case-create-a-pull-request-to-modifyfix-pull-requests) - [Use case: Create a pull request to modify/fix pull requests](#use-case-create-a-pull-request-to-modifyfix-pull-requests)
- [autopep8](#autopep8) - [autopep8](#autopep8)
- [Misc workflow tips](#misc-workflow-tips) - [Misc workflow tips](#misc-workflow-tips)
- [Filtering push events](#filtering-push-events) - [Filtering push events](#filtering-push-events)
- [Dynamic configuration using variables](#dynamic-configuration-using-variables) - [Dynamic configuration using variables](#dynamic-configuration-using-variables)
- [Debugging GitHub Actions](#debugging-github-actions)
## Use case: Create a pull request to update X periodically ## Use case: Create a pull request to update X periodically
@ -38,14 +41,14 @@ jobs:
ncu -u ncu -u
npm install npm install
- name: Create Pull Request - name: Create Pull Request
uses: peter-evans/create-pull-request@v1.6.1 uses: peter-evans/create-pull-request@v1.7.0
env: with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} token: ${{ secrets.GITHUB_TOKEN }}
COMMIT_MESSAGE: update dependencies commit-message: update dependencies
PULL_REQUEST_TITLE: Automated Dependency Updates title: Automated Dependency Updates
PULL_REQUEST_BODY: This is an auto-generated PR with dependency updates. body: This is an auto-generated PR with dependency updates.
PULL_REQUEST_BRANCH: dep-updates branch: dep-updates
BRANCH_SUFFIX: none branch-suffix: none
``` ```
### Keep Go up to date ### Keep Go up to date
@ -72,14 +75,14 @@ jobs:
- run: echo "##[set-output name=pr_title;]update to latest Go release ${{ steps.ensure_go.outputs.go_version}}" - run: echo "##[set-output name=pr_title;]update to latest Go release ${{ steps.ensure_go.outputs.go_version}}"
id: pr_title_maker id: pr_title_maker
- name: Create pull request - name: Create pull request
uses: peter-evans/create-pull-request@v1.6.1 uses: peter-evans/create-pull-request@v1.7.0
env: with:
PULL_REQUEST_TITLE: ${{ steps.pr_title_maker.outputs.pr_title }} token: ${{ secrets.GITHUB_TOKEN }}
PULL_REQUEST_BODY: Auto-generated pull request created by the GitHub Actions [create-pull-request](https://github.com/peter-evans/create-pull-request) and [ensure-latest-go](https://github.com/jmhodges/ensure-latest-go). title: ${{ steps.pr_title_maker.outputs.pr_title }}
COMMIT_MESSAGE: ${{ steps.pr_title_maker.outputs.pr_title }} body: Auto-generated pull request created by the GitHub Actions [create-pull-request](https://github.com/peter-evans/create-pull-request) and [ensure-latest-go](https://github.com/jmhodges/ensure-latest-go).
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} commit-message: ${{ steps.pr_title_maker.outputs.pr_title }}
BRANCH_SUFFIX: none branch-suffix: none
PULL_REQUEST_BRANCH: ensure-latest-go/patch-${{ steps.ensure_go.outputs.go_version }} branch: ensure-latest-go/patch-${{ steps.ensure_go.outputs.go_version }}
``` ```
### Spider and download a website ### Spider and download a website
@ -107,14 +110,14 @@ jobs:
--domains quotes.toscrape.com \ --domains quotes.toscrape.com \
http://quotes.toscrape.com/ http://quotes.toscrape.com/
- name: Create Pull Request - name: Create Pull Request
uses: peter-evans/create-pull-request@v1.6.1 uses: peter-evans/create-pull-request@v1.7.0
env: with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} token: ${{ secrets.GITHUB_TOKEN }}
COMMIT_MESSAGE: update local website copy commit-message: update local website copy
PULL_REQUEST_TITLE: Automated Updates to Local Website Copy title: Automated Updates to Local Website Copy
PULL_REQUEST_BODY: This is an auto-generated PR with website updates. body: This is an auto-generated PR with website updates.
PULL_REQUEST_BRANCH: website-updates branch: website-updates
BRANCH_SUFFIX: none branch-suffix: none
``` ```
## Use case: Create a pull request to update X by calling the GitHub API ## Use case: Create a pull request to update X by calling the GitHub API
@ -124,25 +127,42 @@ This pattern will work well for updating any kind of static content from an exte
You can modify any of the examples in the previous section to work in this fashion. You can modify any of the examples in the previous section to work in this fashion.
1. Set the workflow to execute `on: repository_dispatch`. Set the workflow to execute `on: repository_dispatch`.
``` ```
on: on:
repository_dispatch: repository_dispatch:
types: [create-pull-request] types: [create-pull-request]
``` ```
2. To trigger the workflow call the GitHub API as follows. ### Call the GitHub API from an external service
- `[username]` is a GitHub username
- `[token]` is a `repo` scoped [Personal Access Token](https://help.github.com/en/articles/creating-a-personal-access-token-for-the-command-line) An `on: repository_dispatch` workflow can be triggered by a call to the GitHub API as follows.
- `[repository]` is the name of the repository the workflow resides in.
- `[username]` is a GitHub username
``` - `[token]` is a `repo` scoped [Personal Access Token](https://help.github.com/en/articles/creating-a-personal-access-token-for-the-command-line)
curl -XPOST -u "[username]:[token]" \ - `[repository]` is the name of the repository the workflow resides in.
-H "Accept: application/vnd.github.everest-preview+json" \
-H "Content-Type: application/json" \ ```
https://api.github.com/repos/[username]/[repository]/dispatches \ curl -XPOST -u "[username]:[token]" \
--data '{"event_type": "create-pull-request"}' -H "Accept: application/vnd.github.everest-preview+json" \
``` -H "Content-Type: application/json" \
https://api.github.com/repos/[username]/[repository]/dispatches \
--data '{"event_type": "create-pull-request"}'
```
### Call the GitHub API from another GitHub Actions workflow
An `on: repository_dispatch` workflow can be triggered from another workflow with [repository-dispatch](https://github.com/peter-evans/repository-dispatch) action.
```
- name: Repository Dispatch
uses: peter-evans/repository-dispatch@v1.0.0
with:
token: ${{ secrets.REPO_ACCESS_TOKEN }}
repository: username/my-repo
event-type: create-pull-request
client-payload: '{"ref": "${{ github.ref }}", "sha": "${{ github.sha }}"}'
```
## Use case: Create a pull request to modify/fix pull requests ## Use case: Create a pull request to modify/fix pull requests
@ -181,15 +201,15 @@ jobs:
run: echo ::set-output name=branch-name::"autopep8-patches/$GITHUB_HEAD_REF" run: echo ::set-output name=branch-name::"autopep8-patches/$GITHUB_HEAD_REF"
- name: Create Pull Request - name: Create Pull Request
if: steps.autopep8.outputs.exit-code == 2 if: steps.autopep8.outputs.exit-code == 2
uses: peter-evans/create-pull-request@v1.6.1 uses: peter-evans/create-pull-request@v1.7.0
env: with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} token: ${{ secrets.GITHUB_TOKEN }}
COMMIT_MESSAGE: autopep8 action fixes commit-message: autopep8 action fixes
PULL_REQUEST_TITLE: Fixes by autopep8 action title: Fixes by autopep8 action
PULL_REQUEST_BODY: This is an auto-generated PR with fixes by autopep8. body: This is an auto-generated PR with fixes by autopep8.
PULL_REQUEST_LABELS: autopep8, automated pr labels: autopep8, automated pr
PULL_REQUEST_BRANCH: ${{ steps.vars.outputs.branch-name }} branch: ${{ steps.vars.outputs.branch-name }}
BRANCH_SUFFIX: none branch-suffix: none
- name: Fail if autopep8 made changes - name: Fail if autopep8 made changes
if: steps.autopep8.outputs.exit-code == 2 if: steps.autopep8.outputs.exit-code == 2
run: exit 1 run: exit 1
@ -228,11 +248,11 @@ The recommended method is to use [`set-output`](https://help.github.com/en/githu
echo ::set-output name=pr_body::"This PR was auto-generated on $(date +%d-%m-%Y) \ echo ::set-output name=pr_body::"This PR was auto-generated on $(date +%d-%m-%Y) \
by [create-pull-request](https://github.com/peter-evans/create-pull-request)." by [create-pull-request](https://github.com/peter-evans/create-pull-request)."
- name: Create Pull Request - name: Create Pull Request
uses: peter-evans/create-pull-request@v1.6.1 uses: peter-evans/create-pull-request@v1.7.0
env: with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} token: ${{ secrets.GITHUB_TOKEN }}
PULL_REQUEST_TITLE: ${{ steps.vars.outputs.pr_title }} title: ${{ steps.vars.outputs.pr_title }}
PULL_REQUEST_BODY: ${{ steps.vars.outputs.pr_body }} body: ${{ steps.vars.outputs.pr_body }}
``` ```
Alternatively, [`set-env`](https://help.github.com/en/github/automating-your-workflow-with-github-actions/development-tools-for-github-actions#set-an-environment-variable-set-env) can be used to create environment variables. Alternatively, [`set-env`](https://help.github.com/en/github/automating-your-workflow-with-github-actions/development-tools-for-github-actions#set-an-environment-variable-set-env) can be used to create environment variables.
@ -244,9 +264,46 @@ Alternatively, [`set-env`](https://help.github.com/en/github/automating-your-wor
echo ::set-env name=PULL_REQUEST_BODY::"This PR was auto-generated on $(date +%d-%m-%Y) \ echo ::set-env name=PULL_REQUEST_BODY::"This PR was auto-generated on $(date +%d-%m-%Y) \
by [create-pull-request](https://github.com/peter-evans/create-pull-request)." by [create-pull-request](https://github.com/peter-evans/create-pull-request)."
- name: Create Pull Request - name: Create Pull Request
uses: peter-evans/create-pull-request@v1.6.1 uses: peter-evans/create-pull-request@v1.7.0
env: with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} token: ${{ secrets.GITHUB_TOKEN }}
PULL_REQUEST_TITLE: ${{ env.PULL_REQUEST_TITLE }} title: ${{ env.PULL_REQUEST_TITLE }}
PULL_REQUEST_BODY: ${{ env.PULL_REQUEST_BODY }} body: ${{ env.PULL_REQUEST_BODY }}
```
### Debugging GitHub Actions
**Step Debug Logging** : To enable step debug logging set the secret `ACTIONS_STEP_DEBUG` to `true` in the repository that contains the workflow.
**Output Various Contexts**
```yml
- name: Dump event JSON
env:
EVENT_JSON_FILENAME: ${{ github.event_path }}
run: cat "$EVENT_JSON_FILENAME"
- name: Dump GitHub context
env:
GITHUB_CONTEXT: ${{ toJson(github) }}
run: echo "$GITHUB_CONTEXT"
- name: Dump job context
env:
JOB_CONTEXT: ${{ toJson(job) }}
run: echo "$JOB_CONTEXT"
- name: Dump steps context
env:
STEPS_CONTEXT: ${{ toJson(steps) }}
run: echo "$STEPS_CONTEXT"
- name: Dump runner context
env:
RUNNER_CONTEXT: ${{ toJson(runner) }}
run: echo "$RUNNER_CONTEXT"
- name: Dump strategy context
env:
STRATEGY_CONTEXT: ${{ toJson(strategy) }}
run: echo "$STRATEGY_CONTEXT"
- name: Dump matrix context
env:
MATRIX_CONTEXT: ${{ toJson(matrix) }}
run: echo "$MATRIX_CONTEXT"
``` ```