GitHub Actions工作流语法

一个工作流示例.
欢迎来访我的个人博客Torch-Fan

name: Build and Deploy to ACK

on:
  release:
    types: [created]

# Environment variables available to all jobs and steps in this workflow.
env:
  REGION_ID: cn-hangzhou
  REGISTRY: registry.cn-hangzhou.aliyuncs.com
  NAMESPACE: namespace
  IMAGE: repo
  TAG: ${{ github.sha }}
  ACK_CLUSTER_ID: clusterID
  ACK_DEPLOYMENT_NAME: nginx-deployment

  ACR_EE_REGISTRY: myregistry.cn-hangzhou.cr.aliyuncs.com
  ACR_EE_INSTANCE_ID: instanceID
  ACR_EE_NAMESPACE: namespace
  ACR_EE_IMAGE: repo
  ACR_EE_TAG: ${{ github.sha }}

jobs:
  build:
    runs-on: ubuntu-latest
    environment: production
    
    steps:
    - name: Checkout
      uses: actions/checkout@v2
    
    # 1.1 Login to ACR   
    - name: Login to ACR with the AccessKey pair
      uses: aliyun/acr-login@v1
      with:
        region-id: "${{ env.REGION_ID }}"
        access-key-id: "${{ secrets.ACCESS_KEY_ID }}"
        access-key-secret: "${{ secrets.ACCESS_KEY_SECRET }}"

1. 关于工作流的YAML语法:

  • 工作流文件的语法是YAML语法,以.yml或者.yaml作为文件后缀。
  • 必须将工作流文件存储在你仓库的.github/workflows目录下

2. name

该字段标识了该工作流的名字.

如果该字段空缺,则GitHub在仓库的Actions页面展示的名字是从仓库根目录到工作流文件的相对路径。

https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idsteps

3. on

Required

https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#on

该字段用于指示触发工作流的GitHub事件。 所有可触发工作流的GitHub事件可见该链接:GitHub 可触发工作流的事件

  • on
  • on.<event_name>.types
  • on.<push|pull_request>.<branches|tags>
  • on.<push|pull_request>.paths
  • on.schedule

如需了解GitHub Actions编写中的模式匹配规则,可参考:{% post_link GitHub-Actions模式匹配规则 %}

这里是一些触发事件的例子:

on: push
on: [push, pull_request]
on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main
  # Also trigger on page_build, as well as release created events
  page_build:
  release:
    types: # This configuration does not affect the page_build event above
      - created
on:
  push:
    # Sequence of patterns matched against refs/heads
    branches:    
      # Push events on main branch
      - main
      # Push events to branches matching refs/heads/mona/octocat
      - 'mona/octocat'
      # Push events to branches matching refs/heads/releases/10
      - 'releases/**'
      - '!release/**-alpha'  # 将不会匹配release/**-alpha分支!, 
    # Sequence of patterns matched against refs/tags
    tags:        
      - v1             # Push events to v1 tag
      - v1.*   	       # Push events to v1.0, v1.1, and v1.9 tags
# 当分支或tag和 branches-ignore 和 tags-ignore中的内容匹配时,工作流将不会运行
on:
  push:
    # Sequence of patterns matched against refs/heads
    branches-ignore:
      # Push events to branches matching refs/heads/mona/octocat
      - 'mona/octocat'
      # Push events to branches matching refs/heads/releases/beta/3-alpha
      - 'releases/**-alpha'
    # Sequence of patterns matched against refs/tags
    tags-ignore:
      - v1.*           # Push events to tags v1.0, v1.1, and v1.9
on:
  schedule:
    - cron: '30 5,17 * * *'

4. premissions

https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#permissions

使用该字段,可以限制GITHUB_TOKEN赋予的权限。这是一个全局的权限设置,会运用在工作流的所有任务中。也可以为某个单独的任务添加premissions字段单独设置权限.

下面是可用的字段即权限访问值:

permissions:
  actions: read|write|none
  checks: read|write|none
  contents: read|write|none
  deployments: read|write|none
  issues: read|write|none
  packages: read|write|none
  pull-requests: read|write|none
  repository-projects: read|write|none
  security-events: read|write|none
  statuses: read|write|none

当你为某些字段设定了权限后,所有未设定权限的字段值为None

可以使用如下语法为所有字段设置为可写或可读:

permissions: read-all|write-all

5. env

https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#env

使用该字段,可以设置一系列的环境变量,这些环境变量可以被工作流中的jobs使用。当然也可以在job中单独设置环境变量,在工作流文件根节点设置的环境变量为全局的。

例如:

env:
  SERVER: production

6. defaults

为job提供默认的设置.

defaults:
	run:
		shell: bash
		working-directory: scripts

上面的配置为所有的job配置了默认的shell和工作目录

7. jobs

https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobs

  • 一个工作流中会包含若干个job
  • 这些job的执行默认是并行的,如果需要串行执行job,需要设定job.<job_id>.needs关键字
  • 每一个job运行的环境由runs-on指定

1. job.<job_id>

每一个job都必须有一个id与之相关联,job_id是一个字符串, 他的值是一个map, 里面是job相关的配置数据. 每个job_id都应该是唯一的. job_id 只能以 _ 、字母开头,且只能包含_、字母和-

例如:

jobs:
  my_first_job:
    name: My first job
  my_second_job:
    name: My second job

2. job.<job_id>.name

job展示在GitHub上的名字

3. job.<job_id>.needs

表示执行该job之前,哪些job必须先完成

jobs:
  job1:
  job2:
    needs: job1
  job3:
    needs: [job1, job2]

4. job.<job_id>.runs-on

https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idruns-on

每一个job都必须指定一个运行环境, GitHub-Hosted runner types:

Virtual environmentYAML workflow label
Windows Server 2019windows-latest or windows-2019
Windows Server 2016windows-2016
Ubuntu 20.04ubuntu-latest or ubuntu-20.04
Ubuntu 18.04ubuntu-18.04
Ubuntu 16.04ubuntu-16.04
macOS Big Sur 11.0macos-11.0
macOS Catalina 10.15macos-latest or macos-10.15
runs-on: ubuntu-latest

当然也可以自定义环境,但是大多数情况似乎没有必要。如何自定义环境

5. job.<job_id>.permissions

见全局权限

6. job.<job_id>.environment

为github创建一个environment, github pages就是一个environment.

暂时没有使用过, 后续补充了解

7. job.<job_id>.outputs

https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idoutputs

job的outputs可以被依赖该job的所有jobs访问。输出中如果有密钥,则会被修改且不会发送给GitHub Actions

如果需要使用被依赖job的输出,可以使用needs上下文环境

jobs:
  job1:
    runs-on: ubuntu-latest
    # Map a step output to a job output
    outputs:
      output1: ${{ steps.step1.outputs.test }}
      output2: ${{ steps.step2.outputs.test }}
    steps:
      - id: step1
        run: echo "::set-output name=test::hello"
      - id: step2
        run: echo "::set-output name=test::world"
  job2:
    runs-on: ubuntu-latest
    needs: job1
    steps:
      - run: echo ${{needs.job1.outputs.output1}} ${{needs.job1.outputs.output2}}

8. job.<job_id>.env

见全局环境变量

9. job.<job_id>.defaults

见全局默认配置

10. job.<job_id>.if

使用if, 我们可以让job只在if的条件条件满足时才运行

注意, 如果要告诉GitHub if后面要视为一个表达式而不是一个字符串, 需要用: ${{ <expression> }}$

相关表达式运算和函数可见: GitHub支持的运算和函数

jobs:
  job1:
  job2:
    needs: job1
  job3:
    if: always()  # 无论 job1, job2执行是否成功都执行
    needs: [job1, job2]

常用的函数有:

steps:
  ...
  - name: The job has succeeded 
    if: ${{ success() }}  # 该step之前的步骤都成功执行在执行该step
if: ${{ always() }}  # 无论咋样都执行(工作流被取消也返回True)
if: ${{ cancelled() }}  # 工作流被取消时执行
if: ${{ failure() }}  # 前面的失败了再执行

11. job.<job_id>.steps

https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idsteps

steps 内容很丰富,为了不过多和文档重合,这里只介绍主要部分:

1. run
  • 每一条命令行指令都使用操作系统的shell进行执行
  • 每个run都可以有个字段name, 如果不指定, 则名字为run对应的命令
  • 每一个run关键字都对应一个新的进程,如果run中有多条指令,那么这些指令会执行在同一个shell内
# single-line command
- name: Install Dependencies
  run: npm install
# multi-line command
- name: Clean install dependencies and build
  run: |  # yaml的保留换行
  	npm cli
  	npm run build

working-directory 可以指定执行指令的工作路径

- name: Clean temp directory
  run: rm -rf *
  working-directory: ./temp
steps:
  - name: Display the path
    run: echo ${env:PATH}
    shell: powershell
2. with

该字段用于为Actions传参

jobs:
  my_first_job:
    steps:
      - name: My first step
        uses: actions/hello_world@main
        with:
          first_name: Mona  # 这些都是参数
          middle_name: The
          last_name: Octocat    

当使用的Actions来自Docker容器时, with用法如下:

steps:
  - name: Explain why this job ran
    uses: monacorp/action-name@main
    with:
      entrypoint: /bin/echo
      args: The ${{ github.event_name }} event triggered this step.

来自Docker的Action在使用with时有两个字段, 一个是entrypoint, 一个是args. args是传给entrypoint的参数 经常使用docker的人知道,entrypoint就是在进入容器时立即执行的可执行文件路径。

3. container

https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idcontainer

容器相关使用及验证

4. services

https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idservices

服务使用(如ngnix)、配置及验证

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
GitHub Actions是GitHub的持续集成服务,它可以帮助开发者通过自动化的构建、发布和测试来验证代码,从而尽快发现集成错误。\[2\]GitHub Actions由多个操作组成,比如抓取代码、运行测试、登录远程服务器、发布到第三方服务等等,这些操作被称为actions。一个workflow是一次持续集成的运行过程,由一个或多个jobs构成,每个job又由多个steps构成,每个step可以执行一个或多个命令(action)。\[3\] GitHub Actions允许开发者把每个操作写成独立的脚本文件,存放到代码仓库,使得其他开发者可以引用该脚本,这个脚本就是一个Action。开发者可以从GitHub社区共享的官方市场查找需要的Action,也可以自己编写Action并开源供其他人使用。Action可以通过指定commit、标签或分支来引用不同的版本。\[1\] 在使用GitHub Actions之前,需要了解持续集成/持续交付的概念、Git相关知识、Linux/Windows/macOS脚本相关知识以及Yaml基础语法。Yaml是一种用于配置文件的简洁易读的数据序列化格式。\[2\] GitHub Actions提供了Linux、Windows和macOS虚拟机来运行工作流程,也可以在自己的数据中心或云基础架构中托管自己的自托管运行器。通过在代码仓库中的.github/workflows目录下创建.yml文件,可以配置自动触发的工作流程。\[2\]\[3\] #### 引用[.reference_title] - *1* *2* [GitHubActions详解](https://blog.csdn.net/unreliable_narrator/article/details/124468384)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Github Actions](https://blog.csdn.net/SeriousLose/article/details/121476152)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

fanqiliang630

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值