preface

For those of you who care about the technology front, you’ve probably heard about GitHub Actions

Xiao Nian I happen to meet the demand of use recently, after experiencing some, feel two words: really sweet!

What are GitHub Actions?

GitHub Actions GitHub Actions is a Continuous Integration (CI) service that provides a complete virtual server environment based on which you can build, test, package, deploy projects, and more.

There are three main types of CI \ CD: continuous integration, continuous delivery and continuous deployment.

Our general software development process is:

  1. Developer native code commit
  2. Trigger automated tests via Git hooks
  3. After the tests pass, merge the release branches
  4. Trigger automatic deployment of the service through git hook

This is a simple description of the software development cycle, but it can be more complicated.

As you can see, CI \ CD consists of many Actions, such as performing automated tests, branch merging, service deployment, etc. GitHub calls these Actions Actions.

GitHub allows developers to write actions as separate script files that can be stored in a repository for other developers to reference.

GitHub offers an official marketplace called GitHub Action Market, where you can search for any actions you want and refer directly to other people’s wheels.

The basic concept

GitHub Actions has the following concepts

  • Workflows

    Workflow, automated processes that can be added to the repository. A workflow consists of one or more jobs that can be scheduled or triggered by events.

  • Event

    Event that triggers a specific action of the workflow. For example, submit a PR or pull request to the repository.

  • Jobs

    Job, a group of steps performed on the same pacemaker. By default, workflows with multiple jobs run these jobs in parallel.

  • Steps

    Step to run a single task of a command within a job. Steps can be operations or shell commands. Each step in the job is performed on the same runtime, allowing the operations in the job to share data with each other.

  • Actions

    Actions are individual commands that are combined into steps to create a job. Actions are the smallest portable building block of a workflow. You can create your own actions, or use actions created by the GitHub community.

  • Runners

    Runner, a server on which the GitHub Actions runner application is installed. Github hosts runners based on Ubuntu Linux, Microsoft Windows, and macOS, and each job in the workflow runs in a new virtual environment.

Quick start

Without further discussion, let’s start with a simple example of how GitHub Action works.

Example 1: Sending an email

The example is relatively simple, with the GitHub Action listening for the push event and sending the mail. (The prerequisite is that the MAILBOX needs to be enabled with SMTP service)

The first step is to add a.yml or.yaml file to your project under the./github/workflows/ path. I’ll call it github-action-demo.yml

Github-action-demo.yml: github-action-demo.yml: github-action-demo.yml

name: GitHub Action Demo
on: [push]
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    Copy git repository to virtual machine
    - name: 'Checkout codes'
      uses: actions/checkout@v1
    Get the last committed Git log
    - name: Get Git Log
      id: git_log
      uses: Edisonboy/latest-git-log-action@main
      with:
        tag: origin/master
    # Send email
    - name: Send email
      uses: dawidd6/action-send-mail@v3
      with:
        server_address: smtp.qq.com
        server_port: 465
        username: ${{secrets.MAIL_USERNAME}}
        password: ${{secrets.MAIL_PASSWORD}}
        subject: Github Actions job result
        to: ${{secrets.MAIL_TOUSERNAME}}
        from: ${{secrets.MAIL_USERNAME}}
        body: ${{github.repository}} push log : ${{steps.git_log.outputs.log}}
Copy the code

This.yml file is the definition of GitHub actions. Here’s an overview of the main steps, and the syntax will be explained below

  1. on: [push]Triggered when the project is pushed
  2. jobs.build.runs-onRunning on theubuntuVirtual machine
  3. jobs.build.stepsDifferent steps are defined, as noted above

Step 2: Configure parameters

Where does the ${{XXX}} variable come from and where is it defined?

The above example uses only two types of parameter variables:

  • Secrets.xxx: GitHub allows warehouse owners to create and manage parameters that require confidentiality. For example, the email account and password are sensitive parameters.

    You can set the password through the project Settings -> Secrets -> Actions. Here we add MAIL_USERNAME, MAIL_PASSWORD, and MAIL_TOUSERNAME. The password is the authentication password of the SMTP service.

  • Context: How you can access information about workflow runs, runner environments, jobs, and steps

    • ${{github.repository}}: The owner and name of the current repository. For example,Edisonboy/ActionDemo
    • ${{steps.git_log_outputs.log}}: Gets the output set whose step ID is git_log

Third, because we defined push as the trigger condition, the GitHub Action we defined will only be executed if we only have push code. Actions on GitHub can then see the current execution status in real time.

Git push logs will be sent to git push.

Instance address: Edisonboy/ActionDemo

Example 2: Github synchronizes Gitee

Reference: GitHub Actions to synchronize GitHub code with the Gitee repository

Workflow grammar

With this example in mind, let’s take a quick look at workflow’s syntax.

The workflow file must be stored in the.github/workflows directory of the repository with the.yml or.yaml extension

name

The name of the workflow that GitHub displays on the actions page of the repository.

on

The name of the GitHub event that triggers the Workflow.

# Single event
on: push

# Multiple event list
on: [push.pull_request]

Specify the push of the main branch
on:
  push:
    branches:
      - main
Copy the code

You can also use scheduled scheduling:

on:
  schedule:
    - cron: '*/30 5,17 * * *'
Copy the code

Jobs

Jobs, the core tasks that Workflow performs.

jobs.<job_id>

Each job must be associated with an ID. For example, the above example ID is build

jobs.<job_id>.name

The name of the job displayed on GitHub.

jobs.<job_id>.needs

Identify any jobs that must be completed successfully before this job can run.

jobs:
  job1:
  job2:
    needs: job1
  job3:
    needs: [job1.job2]
Copy the code

In this example, JoB1 must complete successfully before JoB2 starts, while Job3 waits for job1 and JoB2 to complete.

jobs.<job_id>.runs-on

The type of machine on which the job is to run. The machine can be a GitHub hosted or self-hosted runner.

The types of GitHub hosted runners available include:

jobs.<job_id>.steps

Steps. Each Job contains one or more steps. The steps can be running commands, running setup tasks, or running operations in the repository and Dcoker image publishing, etc.

For example, the above example defines three steps:

  1. Copy git repositories to virtual machines
  2. Get the last committed Git log
  3. Send E-mail

Each step can define the following fields:

jobs.. steps[*].id : The unique identifier of the step
jobs.. steps[*].name : The steps are displayed in GitHub In the name of the
jobs.. steps[*].if : User-defined expression to determine whether conditions are met
jobs.. steps[*].uses : Select public repository, or publish Docker The operations that the container image runs as part of. For example, the above examples all use operations provided by the common repository
jobs.. steps[*].run : run shell Command program.
Copy the code

For more details on how to use the Workflow syntax, see the Official GitHub Actions document

summary

GitHub automatically provides you with a virtual server while you’re running your workflow. If you’re thinking about it, you should be thinking about whether it’s possible to connect to the same server and do whatever you want.

Taking a look at the GitHub Actions official documentation for virtual server resources, this configuration is better than a low profile personal server.

Hardware specifications of Windows and Linux VMS:

  • 2 nuclear CPU
  • 7 GB RAM memory
  • 14 GB SSD disk space

Hardware specifications of MacOS VIRTUAL machine:

  • 3 core cpus
  • 14 GB RAM memory
  • 14 GB SSD disk space

SSH connect to GitHub Actions virtual server

There are even more hardcore operations! Hackers can be described as the white piao play incisively and vividly, it is thought that the use of GitHub server mining (details: hackers with GitHub server mining, three days run 30,000 tasks, code revealed in Chinese)

Of course, GitHub Actions is not so silly, must think of our white piao various SAO operation, so for the use of a certain amount of restrictions

All in all, GitHub Actions is a very useful and fun feature. There’s nothing you can’t do, only what you can’t think of.

The resources

GitHub Actions official document

GitHub Actions Tutorial – Ruan Yifeng

GitHub Actions Advanced skills

Ordinary change, will change ordinary

I am a house xiaonian, a low-key young man in the Internet

Please visit my blog at 📖 edisonz.cn for more shared articles