How to push a commit to Github from a CircleCI build using a personal access token

I've used

git push -q https://${GITHUB_PERSONAL_TOKEN}@github.com/<user>/<repo>.git master

and it worked. Update it to be:

# Push changes
git config credential.helper 'cache --timeout=120'
git config user.email "<email>"
git config user.name "<user-name>"
git add .
git commit -m "Update via CircleCI"
# Push quietly to prevent showing the token in log
git push -q https://${GITHUB_PERSONAL_TOKEN}@github.com/giantswarm/docs.git master

Thanks to the hint by Ali Amin I now have this working solution:

version: 2
jobs:
  build:
    machine: true
    steps:
      - run:
          name: Clone docs
          working_directory: ~/workdir
          command: |
            git clone --depth 1 https://${DOCS_GITHUB_TOKEN}@github.com/giantswarm/docs.git
      - deploy:
          name: Trigger docs deployment
          working_directory: ~/workdir/docs
          command: |
            git config credential.helper 'cache --timeout=120'
            git config user.email "<email>"
            git config user.name "Deployment Bot"
            git commit --allow-empty -m "Trigger deployment"
            # Push quietly to prevent showing the token in log
            git push -q https://${DOCS_GITHUB_TOKEN}@github.com/giantswarm/docs.git master

Some notes:

  • The git clone is first.
  • All subsequent git commands have to be executed in the clone directory. working_directory simplifies this a great deal.
  • The token DOCS_GITHUB_TOKEN is a personal access token with repo scope for the target repository.