Assuming you have a basic understanding of GitHub Actions.

For example, you know that the following configuration means that the push to the master branch triggers an action that checks out the code in ubuntu and then executes the NPM I and NPM run test using Node 12.

name: test
on:
  push:
    branches: [ master ]
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - uses: actions/setup-node@v1
      with:
        node-version: 12
    - name: install
      run: npm i
    - name: test
      run: npm run test
Copy the code

So I’m going to give you a little bit of a life lesson a little bit of a practical operation.

Modify the file and push it back to the repository

Code is usually checked out using actions/checkout@v2.

Since git commands are already available in your environment, you can modify them, commit them, and push them back to the repository.

There’s definitely an action in the GitHub Marketplace that provides this functionality, but don’t bother looking, and you’ll have to look at the documentation. I’ll just go through the direct command line.

There are a few things to watch out for:

You may need to change the token

As you can see, even private repositories can use actions/checkout@v2 because the default token used by this action has the permission to checkout the repository. But there are other cases where the token’s permissions are insufficient.

For chestnut, if the action will create a/a lot/workflows/another yaml configuration file, then push it complains:

refusing to allow a GitHub App to create or update workflow `.github/workflows/another.yaml` without `workflows` permission
Copy the code

This token does not have permission to modify workflows. You need to create a Personal Access Token with the appropriate permissions and supply it to actions/ Checkout.

The process of creating PAT is not detailed. In addition, PAT needs to be saved in Secrets for security.

Avoid infinite loops

Many actions are usually triggered by push, and if an action can continue to trigger push, it can cause an infinite loop.

Depending on your needs, change the action or change the code.

Git users need to be configured

Git config –global user.email “[email protected]” git config –global user.name “name”

If the file has not changed

The ideal situation is to modify, commit, and push, but there may be cases where the file doesn’t actually change. If git commit is executed, nothing to commit, working tree clean is displayed.

Note that this is an error indicating that the action failed.

Here we provide a bash script we wrote as workaround that commits only when the file changes:

#! /bin/bash
status_log=$(git status -sb)
The master branch is used here, and can be modified as required
if [ "$status_log"= ="## master... origin/master" ];then
  echo "nothing to commit, working tree clean"
else
  git add .&&git commit -m "update by github actions"&&git push origin master
fi
Copy the code

Run the chmod +x command to add the execute permission.

To summarize, here is an action configuration file available:

name: demo
on:
  # This is a scenario triggered by a timed task that does not lead to an infinite loop
  schedule:
    - cron: '0 */12 * * *'
jobs:
  modify:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [10.x]
    steps:
    - uses: actions/checkout@v2
      with:
        # personal access token
        token: The ${{ secrets.PERSONAL_ACCESS_TOKEN }}
    - name: Use Node.js The ${{ matrix.node-version }}
      uses: actions/setup-node@v1
      with:
        node-version: The ${{ matrix.node-version }}
    # Modify file operation
    - name: modify files
      run: node ./operations.js
    # configuration git
    - name: config git
      run: git config --global user.email "[email protected]"&&git config --global user.name "name"
    # Use the sh script above to submit and push
    - name: run script
      run: chmod +x ./update-repo.sh&&./update-repo.sh
    Or use the git command
    - name: check for changes
      run: git status
    - name: stage changed files
      run: git add .
    - name: commit changed files
      run: git commit -m "update by github actions"
    - name: push code to master
      run: git push origin master
Copy the code

Operate another warehouse

Of course, you can clone other repositories with git command line and then do this and that.

It should be noted that if you want to push or clone a private warehouse, you need to consider the issue of permissions, even if the private warehouse is its own. This is only for SSH scenarios.

There’s definitely action in the Marketplace, but I’m not looking for it. Here’s a simple one. Here’s the original link.

Save the private key to secrets.id_rsa and add the following configuration. Then set the SSH_AUTH_SOCK environment variable where the key is needed.

- name: setup SSH keys and known_hosts
  run: | mkdir -p ~/.ssh ssh-keyscan github.com >> ~/.ssh/known_hosts ssh-agent -a $SSH_AUTH_SOCK > /dev/null ssh-add - <<< "${{ secrets.ID_RSA }}"  env:
    SSH_AUTH_SOCK: /tmp/ssh_agent.sock
- name: clone repository
  Clone a private repository
  run: git clone [email protected]:private/repo.git
  Set env to use the key
  env:
    SSH_AUTH_SOCK: /tmp/ssh_agent.sock
Copy the code

Deploy files to another server

Use ssh-deploy without further details.

Example:

- name: deploy
  uses: Easingthemes/[email protected]
  env:
    SSH_PRIVATE_KEY: The ${{ secrets.ID_RSA }}
    ARGS: "-avzr --delete"
    SOURCE: "dist/"
    REMOTE_HOST: "server ip"
    REMOTE_USER: "root"
    TARGET: "/home/webroot/dist"
Copy the code

Switching working Directory

If you want to perform all operations in a subdirectory, you can directly switch the default working directory to this directory.

name: build
on:
  push:
    branches: [ master ]
defaults:
  run:
    working-directory: client
jobs:
  Now the default directory is./client
Copy the code

Note that if you CD to a directory in one step, subsequent steps are unaffected and remain in the same directory.

Would like to ask is there a way to change the working directory has been effective?