Push built files to repo using Actions

I want to setup a GitHub Action, which would build two things - a wiki using MkDocs and Javadoc using Gradle’s Javadoc command - and then push those towards the repository itself, so that it can then be displayed using GitHub pages.

My current workflow is setup to build the pages and all I need now is a way to push those towards the repository itself.

What would be a good way to do it?

The Workflow can be found here:

https://github.com/botblock/JavaBotBlockAPI/blob/master/.github/workflows/build_docs.yml

Other things to know:

GitHub pages is set up to take the content from the docs directory to then display. So using the gh-pages branch and maybe MkDocs’ gh-deploy command isn’t a solution for me.

Additionally do I need to build the wiki with MkDocs first before the Javadoc due to MkDocs deleting everything in the target-directory (docs folder).

@andre601 ,

You can use the  git push command to push your local changes to the remote GitHub repository.

Before execute the git push, you need to set the  git config.

A simple demo to push changes to remote in the workflow:

- name: update file and push to remote
        run: |
          echo "Test push commit to the remote in the workflow" >> test.txt

          git config --global user.name "github-actions[bot]"
          git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"

          git add -A
          git commit -m "Update test.txt"
          git push

Here I use the bot account github-actions[bot] to set the  git config. You also can set yourself account if you want.

2 Likes

So this would only push files that are in the docs directory on the workflow?
I don’t really want to test this out, as I fear it could mess up the repository, which I would like to avoid.

Also, does this mean that the workflow already has a git-repo setup? Doesn’t it also require it to first being setup in order to push stuff, or does GitHub (Actions) already set the workflow to have the repository as a remote set?

@andre601 ,

All my above steps are based on having checked out the repository from the remote to the local of the runner machine.
The basic process is following:

steps:
  - name: Checkout code
    uses: actions/checkout@v2

  - name: Modify/add files
    . . .

  - name: push changes to remote
  run: |
    git config --global user.name "user mane"
    git config --global user.email "user email"

    git add **\<pathspec\>** git commit -m "commit message"
    git push

Description:

1) The checkout action can check out the repository from the remote to the local and also generate the local repository on the runner machine, it does not only download the source files to the local.

2) In the git add command, you can specify which modified files ( <pathspec> ) you want to commit,
for example, only commit the modified files, in the directory " docs":

git add docs

If yo want to commit all the modified files, you can use the -A option.

git add -A

More details, you can reference here: https://git-scm.com/docs/git-add

1 Like

Thank you very much!

It works now and like I wanted it to.

But before I go another small question. I currently use a workflow on another repository to automatically push changes to the gh-pages branch of said repository and created a PAT (Personal Access Token) for it.

Is this even required? Because when I check my PATs is this one shown as “never used”.

I have it as secret in the repo and use it when adding a remote (I guess? I assume this isn’t really needed here?)

git remote add gh-token "https://${{ secrets.GH_TOKEN}}@github.com/:user/:repo.git"

The entire workflow:

name: Deploy Site

on:
  push:
    paths:
    - 'docs/**'
    - '**.yml'
    branches:
    - master

jobs:
  build:
    runs-on: [ubuntu-latest]
    steps:
    - uses: actions/checkout@v2
    - name: Set up Python 3.7
      uses: actions/setup-python@v1
      with:
        python-version: 3.7
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip setuptools
        python -m pip install -r requirements.txt
    - name: Deploy Files
      run: |
        git config user.name ${{ secrets.GH_USER }}
        git config user.email "${{ secrets.GH_MAIL }}"
        git remote add gh-token "https://${{ secrets.GH_TOKEN}}@github.com/:user/:repo.git"
        git fetch gh-token && git fetch gh-token gh-pages:gh-pages
        python -m mkdocs gh-deploy -v --clean --remote-name gh-token
        git push gh-token gh-pages

To add there is an automatically generated token secrets.GITHUB_TOKEN now.