gitHub Action - workflow concepts and basic operations
1. Workflow file
References for writing this article:
- Getting Started with GitHub Actions - Ruan Yifeng
- GithubAction---Workflow concepts and basic operations
github action
The configuration files are called workflow
files and are stored in the .github/workflows
directory.
The file .yml
takes .yaml
the suffix format of or .
A library can have multiple workflow
files , as long as there is a suffix file that matches the format, the file will be automatically executed.
2. Basic Concepts and Terminology
- Workflow: The process of continuous integration running once is a workflow.
- Job (task): A workflow consists of one or more jobs, which means a continuous integration operation that can complete multiple tasks.
- Step (step): Each job consists of multiple steps, which are completed step by step.
- action (action): Each step can execute one or more commands in turn.
There are many configuration fields in the workflow file, see the official documentation for details . Below are some basic fields.
1、name
The name of the workflow. If this field is omitted, it defaults to the file name of the current workflow.
name: GitHub Actions Demo
复制代码
2、on
Specifies the conditions that trigger the workflow, usually some event.
# push 事件触发 workflow
on: push
# 可以是事件的数组
on: [push, pull_request]
# 指定触发事件时,可以限定分支或标签
on:
push:
branches:
- master
复制代码
3、jobs
The body of the workflow is the job field, which represents one or more tasks to be performed.
jobs.<job_id>.name
jobs:
my_first_job:
name: My first job
my_second_job:
name: My second job
复制代码
The above code jobs
contains two tasks, job_id
namely my_first_job
, my_second_job
.
job_id
Inside name
is task.
jobs.<job_id>.needs
Specifies the dependencies of the current task, the order in which to run.
jobs:
job1:
job2:
needs: job1
job3:
needs: [job1, job2]
复制代码
job1 must finish before job2, job3 waits for job1 and job2 to finish before it can run. So the execution order is job1, job2, job3.
jobs<job_id>.runs-on
Specifies the virtual machine environment required to run. Required fields
ubuntu-latest,ubuntu-18.04或ubuntu-16.04
windows-latest,windows-2019或windows-2016
macOS-latest或macOS-10.14
复制代码
jobs<job_id>.steps
Specifies the running steps for each job, which can contain one or more steps.
- jobs.<job_id>.steps.name: Step name.
- jobs.<job_id>.steps.run: The command or action to run for this step.
- jobs.<job_id>.steps.env: Environment variables required for this step.
name: Greeting from Mona
on: push
jobs:
my-job:
name: My Job
runs-on: ubuntu-latest
steps:
- name: Print a greeting
env:
MY_VAR: Hi there! My name is
FIRST_NAME: Mona
MIDDLE_NAME: The
LAST_NAME: Octocat
run: |
echo $MY_VAR $FIRST_NAME $MIDDLE_NAME $LAST_NAME.
复制代码
Simulation example
Simulate a scenario, when a push event occurs in a branch, the entire project will be built, and the deployed product will be pushed to a link we configured
Refer to the configuration of the alita main library
env:
NODE_OPTIONS: --max-old-space-size=6144
复制代码
Set the memory limit for Node.
- name: "Checkout"
uses: actions/checkout@v3
with:
fetch-depth: 0
复制代码
Among the steps of the job, the first step action/checkout
is to obtain the source code.
uses
: Use some official or third-party actions to execute.
- name: "Setup Node.js"
uses: actions/setup-node@v3
with:
node-version: 14
复制代码
Install node on the current operating system
- name: install
run: yarn
- name: Build
run: yarn run docs:build
复制代码
Execute the package command after installation
- name: Deploy
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./docs-dist
cname: dform.alitajs.com
复制代码
publish_dir
: package generated after packagingcname
: Configured documentation link