If you’re already using GitHub Actions, read this article for more interesting and useful ways to open them. After reading it, I added a few workflows to the repository.

1. Input parameters when workflow is executed

When executing workflow, allow parameters to be entered on the GitHub Actions page to control the execution logic. We can parameterize the manual logic in GitHub Actions for continuous deployment scenarios.

on: 
  workflow_dispatch:
    inputs:
      logLevel:
        description: 'Log level'     
        required: true
        default: 'warning'
      tags:
        description: 'Test scenario tags'  
jobs:
  printInputs:
    runs-on: ubuntu-latest
    steps:
    - run: |
        echo "Log level: ${{ github.event.inputs.logLevel }}"
        echo "Tags: ${{ github.event.inputs.tags }}" 
Copy the code

When the workflow above is executed, the following dialog box pops up.

2. Job choreography controls the execution sequence

A Workflow consists of many jobs. With the help of the NEEDS parameter, we can manage the dependencies between these jobs and control their execution process.

on: push
jobs:
  job1:
    runs-on: ubuntu-latest
    steps:
      - run: echo "job1"
  job2:
    runs-on: ubuntu-latest
    steps:
      - run: sleep 5
    needs: job1
  job3:
    runs-on: ubuntu-latest
    steps:
      - run: sleep 10
    needs: job1
  job4:
    runs-on: ubuntu-latest
    steps:
      - run: echo "job4"
    needs: [job2, job3]
Copy the code

When workflows is executed, job2 and job3 are executed after Job1 and job4 are executed after Job2 and Job3 are executed.

3. Used for project management

Kubernetes uses Prow to coordinate orderly community collaboration based on ChatOps. But not every team is willing to build and maintain a Prow robot system. The core of the ChatOps implementation is event-driven, which is also possible on GitHub using Actions.

Here are some project management-related actions

  • Add labels based on the modified directory
- uses: actions/labeler@main
  with:
    repo-token: "${{ secrets.GITHUB_TOKEN }}"
Copy the code

In the configuration file. Making/workflows/labeler. Yml add rule, to modify the docs directory Pull Requests (hereinafter referred to as the PR) automatically add docs_label tags:

docs_label:
  - ./docs/*
Copy the code
  • Add Issues to Projects according to the tag

By using SRGGRS/assignment-one-project-github-action, we can add the newly added Issues or PR to the specified Projects.

- name: Assign NEW issues and NEW pull requests to project 2
  uses: SRGGRS/[email protected]
  if: github.event.action = = 'opened'
  with:
    project: 'https://github.com/srggrs/assign-one-project-github-action/projects/2'
Copy the code

You can also add Issues or PR containing the specified label to the specified Column of the specified Project.

- name: Assign issues and pull requests with `bug` label to project 3
  uses: SRGGRS/[email protected]
  if: | contains(github.event.issue.labels.*.name, 'bug') || contains(github.event.pull_request.labels.*.name, 'bug')  with:
    project: 'https://github.com/srggrs/assign-one-project-github-action/projects/3'
    column_name: 'Labeled'
Copy the code
  • Clean up Issues that have not been followed up for a long time

If an Issue has not been updated for 30 days, the workflow below will wait another 5 days and then close it.

name: 'Close stale issues and PRs'
on:
  schedule:
    - cron: '1 * * *'

jobs:
  stale:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/stale@v3
        with:
          stale-issue-message: 'This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.'
          days-before-stale: 30
          days-before-close: 5
Copy the code

GitHub’s project management focuses on Issues, Projects, Labels, and Pull Requests. You can search the GitHub Actions Marketplace for relevant Actions.

4. Online debugging

If you need to log in to Runner to debug commands while using GitHub Actions, here’s a tip you’ll be interested in.

- uses: shaowenchen/debugger-action@v2
  name: debugger
  timeout-minutes: 30
  continue-on-error: true
  with:
    ngrok_token: The ${{ secrets.NGROK_TOKEN }}
Copy the code

You only need to apply for a token on the official website of Ngrok to remotely log in to Runner through SSH. Of course, you can also expose Runner’s services and provide links to external networks for up to 6 hours.

In the execution log, we can find the SSH login link and use root/root to log in to Runner. If the port mapping is configured on the Web, you can view related service links.

5. Set cache

Caching can effectively speed up construction, reduce network requests and reuse intermediate codes. This is useful for Java, Nodejs, Python, etc.

- name: Get yarn cache directory path
  id: yarn-cache-dir-path
  run: echo "::set-output name=dir::$(yarn cache dir)"

- uses: actions/cache@v2
  id: yarn-cache # use this to check for `cache-hit` (`steps.yarn-cache.outputs.cache-hit ! = 'true'`)
  with:
    path: The ${{ steps.yarn-cache-dir-path.outputs.dir }}
    key: The ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
    restore-keys: | ${{ runner.os }}-yarn-Copy the code

6. Detect problem links in projects

After a long period of project maintenance, the biggest headache is documentation. Development and testing follow up with code and features, and documentation is often not updated. The lack of documentation to maintain drives away potential participants. The following Action detects Broken links in the document.

name: Check Markdown links

on: push

jobs:
  markdown-link-check:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@master
    - uses: gaurav-nelson/github-action-markdown-link-check@v1
      with:
        use-quiet-mode: 'yes'
        config-file: '.github/workflows/checklink_config.json'
        max-depth: 3
Copy the code

Gaurav-nelson /github- action-markdown-link-Check supports custom configuration and is very flexible and easy to use. It can be regarded as a necessary action.

. Here is a lot/workflows/checklink_config json example:

{
  "replacementPatterns": [{"pattern": "^ /"."replacement": "/github/workspace/"}]."aliveStatusCodes": [
    429.200]}Copy the code

Finally, the GitHub Actions log page will output the following result:

=========================> MARKDOWN LINK CHECK <========================= FILE: ./docs/governance.md 4 links checked. FILE: . / docs/configuration/cri. Md [✖] https://build.opensuse.org/project/show/devel:kubic:libcontainers:stable links checked. ERROR: 1 dead links found! [✖] https://build.opensuse.org/project/show/devel:kubic:libcontainers:stable - > Status: 404 FILE: ./docs/configuration/kubeedge.md 21 links checked. = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =Copy the code

7. Jobs are executed in batches and the parameters are arranged and combined

In a data-driven test scenario, you can control the test flow by entering parameters. GitHub Actions also allows you to batch execute or orchestrate processes in a parameterized manner.

GitHub Actions arranges and combines each parameter in the matrix to create a new running instance.

On: push jobs: node: runs-on: ${{matrix. OS}} Strategy: matrix: OS: [Ubuntu-16.04, Ubuntu-18.04] node: [6, 8, 10] steps: - uses: actions/setup-node@v1 with: node-version: ${{ matrix.node }} - run: node --versionCopy the code

When the workflow above is executed, six jobs are executed.

This is great both for testing compatibility and for executing jobs in batches.

8. Copy the Badge status of Action displayed in the document

In general, we use GitHub Actions for code analysis, testing, compilation, packaging, building, pushing images, etc. These behaviors are critical to the stability of the project.

But not everyone pays attention to the details of Actions execution. We can give the final real-time status of these processes in a prominent place to alert users and developers. If the main branch fails, it reminds users to use it with caution and reminds developers to fix the problem as soon as possible.

From the GitHub Actions page, click Create Status Badge.

Add the URL link in the pop-up box to the Readme document so that you can view the execution result of the workflow in real time.

9. Hook into GitHub behavior with precision

Workflow Defines trigger conditions using the ON keyword. There are three main triggering events:

  • Artificial triggering
on: workflow_dispatch
Copy the code
  • Timing trigger

Workflows are triggered every 15 minutes.

on:
  schedule:
    - cron:  '*/15 * * * *'
Copy the code
  • Webhook trigger

Our actions on GitHub, such as creating Issues, adding Deployment, etc., can be accessed through the API. With these events, we can precisely customize Workflow’s behavior. We usually trigger push or pull requests. Here are a few unusual examples:

Triggered when someone forks the warehouse

on:
  fork
Copy the code

Triggered when someone star warehouse

on:
  watch:
    types: [started]
Copy the code

Triggered when a new Issue is created

on:
  issues:
    types: [opened]
Copy the code

10. Developing an Action is simple

If you can’t find Action in the Marketplace, it’s a good idea to develop your own Action.

Actually, developing an Action is not as difficult as you might think. An Action is a processing logic that takes input parameters, performs some logic, and then outputs the parameters. There are three types of actions:

  • Docker Container is used in Linux

The Docker container provides the execution logic of the Action. Take this example:

Dockerfile

FROM appleboy/drone-scp:1.6.2-linux-amd64

ADD entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
Copy the code

entrypoint.sh

#! /bin/sh

set -eu

[ -n "$INPUT_STRIP_COMPONENTS" ] && export INPUT_STRIP_COMPONENTS=$((INPUT_STRIP_COMPONENTS + 0))

sh -c "/bin/drone-scp $*"
Copy the code

Dron-scp image is used to quickly develop an Action that provides a copy of SCP files.

  • JavaScript for Linux, macOS, and Windows

Process the Action logic by executing JavaScript. JavaScript and TypeScript Action templates are officially available. When creating a project, use a template to create it, then write the processing logic and publish your own actions.

GitHub Actions provides a toolkit to support extensions in this way, such as executing commands, manipulating GitHub, etc., by referring to packages and calling related functions directly. Here are a few of them:

@actions/exec, execute commands @actions/core, input, output, log, key related @actions/ IO, operate filesCopy the code
  • Composite Run Steps for Linux, macOS, and Windows

This type allows a series of Shell operations to be used as an Action.

name: 'Hello World'
description: 'Greet someone'
inputs:
  who-to-greet:  # id of input
    description: 'Who to greet'
    required: true
    default: 'World'
outputs:
  random-number:
    description: "Random number"
    value: The ${{ steps.random-number-generator.outputs.random-id }}
runs:
  using: "composite"
  steps:
    - run: echo Hello The ${{ inputs.who-to-greet }} instead!
      shell: bash
    - id: random-number-generator
      run: echo "::set-output name=random-id::$(echo $RANDOM)"
      shell: bash
    - run: The ${{ github.action_path }}/goodbye.sh
      shell: bash
Copy the code

Reference 11.

  • Github.com/actions/typ…
  • Github.com/shaowenchen…

The original link www.chenshaowen.com/blog/10-tip…

More exciting content please pay attention to the public number.