how does one commit from an action?

I’m trying to commit to gh-pages from an action. Something like:

jobs:

  my_job:

    steps:

      - name: first_step

         run: |

           git config --global user.name ${GITHUB_ACTOR}

           git config --global user.email ${GITHUB_ACTOR}@my-domain.io
           git add ${GITHUB_WORKSPACE}/my-docs
           git commit -m "committing my-docs"
           git subtree push --prefix my-docs origin gh-pages

seems to fail at the push command with 

fatal: could not read Username for 'https://github.com': No such device or address
##[error]Process completed with exit code 128.

I am faking that user.email config. It’s not exposed in the context and the git commit fails without it.

*** Please tell me who you are.

Run

  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: empty ident name (for <runner@fv-az12.gghxzsbq3hye5ppqqusf5o0opc.cx.internal.cloudapp.net>) not allowed
##[error]Process completed with exit code 128.

At this point, the gh-pages orphaned branch is not created. Does that make a difference?

2 Likes

oh - and this is a private repo, and for the sake of argument, I tried to use an email that was hardcoded, to no avail. 

Any help is appreciated. 

try this:

git remote add github "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$GITHUB_REPOSITORY.git"
git pull github ${GITHUB_REF} --ff-only

git add ...

git commit -m "Update native model"
git push github HEAD:${GITHUB_REF}
6 Likes

To avoid having to set up git’s user.name and user.email settings, you could also use GitHub’s API to commit and push changes from an action.

Here is an example where I update a YAML file and push changes using the github3 Python library:

import os

import github3
import yaml

MAIN_BRANCH = 'master'

# Read updated contents of galaxy.yml.
# NOTE: Make changes to the galaxy.yml file before running this script.
with open('galaxy.yml') as f:
    galaxy_file = f.read()

# Connect to GitHub API and push the changes.
github = github3.login(token=os.environ['GITHUB_TOKEN'])
repository = github.repository(*os.environ['GITHUB_REPOSITORY'].split('/'))
github_galaxy_yml = repository.file_contents('/galaxy.yml', ref=MAIN_BRANCH)
pushed_change = github_galaxy_yml.update(
    'Bump version',
    galaxy_file.encode('utf-8'),
    branch=MAIN_BRANCH
)
print("Pushed commit {} to {} branch:\n {}".format(
    pushed_change['commit'].sha,
    MAIN_BRANCH,
    pushed_change['commit'].message,
))
2 Likes

This is a solution written directly as a run (bash) step: https://github.com/1138-4EB/actions/blob/3f0701c2f20780984590bd955839a38b75c96668/.github/workflows/push.yml#L33-L48

Note that GHA_DEPLOY_KEY is a private key that was uploaded as a secret, and the corresponding public key was uploaded as a deploy key to the target repo. The private key pair was created with:

ssh-keygen -t ed25519

The advantage of this approach is that the scope of deploy keys is more limited than that of tokens.

2 Likes

This action does what you want

6 Likes

If you use actions/checkout@v2 instead of actions/checkout@v1 the login is done automatically. You can git commit and git push without setting up anything :slight_smile:

7 Likes

@dlunch This is Helpful

The documentation for v2 specifically mentions that you need to configure name and email: https://github.com/actions/checkout#push-a-commit-using-the-built-in-token

I tried it today without name and email config and it didn’t work. Could you elaborate on your workflow that doesn’t require that configuration?

5 Likes

I’m happy with github’s advice in @eps1lon’s link, but here’s something else I’ve used:

git config --local user.email "$(git log --format='%ae' HEAD^!)"
git config --local user.name "$(git log --format='%an' HEAD^!)"

It just copies the name and email from the last commit.

2 Likes

If somebody from Github could provide a current solution that would be good. I have an action here that (I think) follows the above advice (as far as I can make it out) and runs fine. However it appears to have no impact on the repository.

I created a simple script based on the helpful posts by @dlunch and @moble , if anyone wants to see it in action on a full repo:

Here’s an example of an action run:

Here’s another quick one, from “I should start looking at GitHub actions” to a working action which commits back to the repo in about 10 minutes, basically by reading the discussion above.

Nothing new here, but hopefully condensed enough to help someone see exactly what I did, and a couple of misunderstandings from this thread cleared up by trial and error.

What was slightly unnerving was that pushing back to the repo from the PR didn’t work, but once I merged to the main branch it worked fine.