actions/checkout@v2
Version 2 of checkout resolves the detached HEAD state issue and simplifies pushing to origin.
name: Push commit
on: push
jobs:
report:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Create report file
run: date +%s > report.txt
- name: Commit report
run: |
git config --global user.name 'Your Name'
git config --global user.email 'your-username@users.noreply.github.com'
git commit -am "Automated report"
git push
If you need the push event to trigger other workflows, use a repo
scoped Personal Access Token.
- uses: actions/checkout@v2
with:
token: ${{ secrets.PAT }}
actions/checkout@v1 (original answer)
The problem is that the actions/checkout@v1
action leaves the git repository in a detached HEAD state. See this issue about it for more detailed information: https://github.com/actions/checkout/issues/6
The workaround I have used successfully is to setup the remote as follows:
git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/username/repository
You may also need to checkout. You can extract the branch name from the GITHUB_REF
:
git checkout "${GITHUB_REF:11}"
Here is a complete example to demonstrate.
name: Push commit example
on: push
jobs:
report:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: Create report file
run: date +%s > report.txt
- name: Commit report
run: |
git config --global user.name 'Your Name'
git config --global user.email 'your-username@users.noreply.github.com'
git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/$GITHUB_REPOSITORY
git checkout "${GITHUB_REF:11}"
git commit -am "Automated report"
git push
By the way, I have written a GitHub action which may help you achieve what you want to do. It will take any changes made locally during a workflow, commit them to a new branch and raise a pull request.
https://github.com/peter-evans/create-pull-request
Also see this related question and answer. Push to origin from GitHub action