GitHub Action

Published on with 0 views and 0 comments

  

WorkFlow

  GithubWorkFlow 就是 Git 自带的 CI 服务

WorkFlow 文件

 1name: Greet Everyone
 2# 事件
 3on: [push]
 4# 工作集合
 5jobs:
 6  build:
 7    # 作业名称为 Greeting
 8    name: Greeting
 9    # 此作业在 Linux 上运行,runs-on选项一共就有几种类型
10    runs-on: ubuntu-latest
11    steps:
12      # 此步骤使用 GitHub 的 hello-world-javascript-action:https://github.com/actions/hello-world-javascript-action
13      - name: Hello world
14        uses: actions/hello-world-javascript-action@v1
15        with:
16          who-to-greet: 'Mona the Octocat'
17        id: hello
18      # 此步骤打印上一步操作的输出(时间)。
19      - name: Echo the greeting's time
20        run: echo 'The time was ${{ steps.hello.outputs.time }}.'

工作流程语法

  工作流程语法原文

  • name
    工作流程的名称
  • on
    触发时机,push,pull_request,create 更多
    具有活动类型或配置的多个事件示例
 1on:
 2  push:
 3    branches:
 4     # master分支发push请求时
 5      - master
 6  pull_request:
 7    branches:
 8     # master分支pull_request时
 9      - master
10  page_build:
11  release:
12    types: 
13      - created
  • on.<event_name>.types
1# 当release的published,created,edited事件被触发时
2on:
3  release:
4    types: [published,created,edited]
  • on.<push|pull_request>.<brancheds|tags>
 1# 在这些分支和tag被push代码时触发
 2# ignore在这些分支和tag被push时不会触发
 3on:
 4  push:
 5    branches:
 6      - master
 7      - 'mona/octocat'
 8      - 'releases/**'
 9    tags:
10      - v1
11      - v1.*
12    branches-ignore:
13      - devp
14      - '**-alpha'
15    tags-ignore:
16      - v2.*
  • on.<push|pull_request>.paths
1# 当有js被推送时触发
2# 当docs下推送时不触发
3on:
4  push:
5    paths:
6    - '**.js'
7    paths-ignore:
8    - 'docs/**'
1# 每隔15分钟构建一次
2on:
3   schedule:
4       - cron: '*/15 * * * *'
  • env
1env:
2   SERVER: production
  • jobs
    工作流程运行包括一项或多项作业。 作业默认是并行运行。 要按顺序运行作业,您可以使用
  • jobs.<job_id>(.name)
1jobs:
2    my_first_job:
3        name: MyfirstJob
4    my_second_job:
5        name: MysecondJob
  • jobs.<job_id>.needs
    job 的依赖关系
1jobs:
2	job1:
3	job2:
4		needs: job1
5	job3:
6		needs: [job1,job2]
  • jobs.<job_id>.runs-on
虚拟环境YAML 工作流程标签
Windows Server 2019windows-latestwindows-2019
Ubuntu 18.04ubuntu-latestubuntu-18.04
Ubuntu 16.04ubuntu-16.04
macOS Catalina 10.15macos-latest or macos-10.15
1runs-on : windows-latest
  • jobs.<job_id>.env
1jobs:
2  job1:
3    env:
4      FIRST_NAME: mona
  • jobs.<job_id>.if
  • jobs.<job_id>.steps
    为作业定义的变量在作业执行时将覆盖名称相同的工作流程变量。步骤可以运行命令、运行设置任务,或者运行您的仓库、公共仓库中的操作或 Docker 注册表中发布的操作。 并非所有步骤都会运行操作,但所有操作都会作为步骤运行。
 1name: Greeting from Mona
 2
 3on: push
 4
 5jobs:
 6  my-job:
 7    name: My Job
 8    runs-on: ubuntu-latest
 9    steps:
10    - name: Print a greeting
11      env:
12        MY_VAR: Hi there! My name is
13        FIRST_NAME: Mona
14        MIDDLE_NAME: The
15        LAST_NAME: Octocat
16      run: |
17        echo $MY_VAR $FIRST_NAME $MIDDLE_NAME $LAST_NAME.
  • jobs.<job_id>.stemp2.uses
    可以使用工作流程所在仓库中、公共仓库中或发布 Docker 容器镜像中定义的操作
 1jobs:
 2  my_first_job:
 3    steps:
 4      - name: My first step
 5        uses: actions/heroku@master
 6      - name: My second step
 7        uses: actions/aws@v2.0.1
 8
 9
10jobs:
11  my_first_job:
12    steps:
13      - name: My first step
14        uses: docker://alpine:3.8
  • jobs.<job_id>.steps.run
    使用 shell,working-directory:指定命令的工作目录,shell 指定 cmd 环境
 1- name: install dependencies and build
 2  run: |
 3     npm ci
 4  shell: bash
 5
 6
 7
 8
 9- name: Clean temp directory
10  run: rm -rf *
11  working-directory: ./temp

  环境列表 `

支持的平台shell 参数描述内部运行命令
所有bash非 Windows 平台上回退到 sh 的默认 shell。 指定 Windows 上的 bash shell 时,将使用 Git for Windows 随附的 bash shel。bash --noprofile --norc -eo pipefail {0}
所有pwshPowerShell Core。 GitHub 将扩展名 .ps1 附加到您的脚本名称。pwsh -command "& '{0}'"
所有python执行 python 命令。python {0}
Linux / macOSsh未提供 shell 且 在路径中找不到 bash 时的非 Windows 平台的后退行为。sh -e {0}
WindowscmdGitHub 将扩展名 .cmd 附加到您的脚本名称并替换 {0}%ComSpec% /D /E:ON /V:OFF /S /C "CALL "{0}"".
Windowspowershell这是 Windows 上使用的默认 shell。 Desktop PowerShell。 GitHub 将扩展名 .ps1 附加到您的脚本名称。powershell -command "& '{0}'".

  • jobs.<job_id>.steps.with
    传入参数
 1# with传入了actions/hello_word@master操作所需要的三个参数,参数名称为fitst_name,middle_name,last_name
 2jobs:
 3  my_first_job:
 4    steps:
 5      - name: My first step
 6        uses: actions/hello_world@master
 7        with:
 8          first_name: Mona
 9          middle_name: The
10          last_name: Octocat    
  • jobs.<job_id>.steps.with.args
    docker 容器参数
1steps:
2  - name: Explain why this job ran
3    uses: monacorp/action-name@master
4    with:
5      entrypoint: /bin/echo
6      args: The ${{ github.event_name }} event triggered this step.
7
  • jobs.<job_id>.steps.with.entrypoint
    覆盖 dockerfile 中的 docker entrypoint
1steps:
2  - name: Run a custom command
3    uses: monacorp/action-name@master
4    with:
5      entrypoint: /a/different/executable
  • jobs.<job_id>.steps.env
    设置整个工作流程或某个作业的环境变量。
    公共操作可在自述文件中指定预期的环境变量。 如果要在环境变量中设置密码,必须使用 secrets 上下文设置
1steps:
2  - name: My first action
3    env:
4      GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
5      FIRST_NAME: Mona
6      LAST_NAME: Octocat
  • jobs.<job_id>.steps.continue-on-error
    错误跳过
1steps:
2  - continue-on-error: true
3  - name: My first action
4    env:
5      GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
6      FIRST_NAME: Mona
7      LAST_NAME: Octocat
8
  • jobs.<job_id>.steps.timeout-minutes
    最大运行时间
  • jobs.<job_id>.strategy
    矩阵,比如 3*3 实际是 9 个任务
 1# 3*1 3个任务,每个都会试
 2strategy:
 3  matrix:
 4    node: [6, 8, 10]
 5steps:
 6  # Configures the node version used on GitHub-hosted runners
 7  - uses: actions/setup-node@v1
 8    with:
 9      # The Node.js version to configure
10      node-version: ${{ matrix.node }}
11
 1# os和node全排列
 2runs-on: ${{ matrix.os }}
 3strategy:
 4  matrix:
 5    os: [ubuntu-16.04, ubuntu-18.04]
 6    node: [6, 8, 10]
 7steps:
 8  - uses: actions/setup-node@v1
 9    with:
10      node-version: ${{ matrix.node }}
11

  添加额外的配置到矩阵中

 1# include 在os为windows-latest时且node为4时,npm为2
 2runs-on: ${{ matrix.os }}
 3strategy:
 4  matrix:
 5    os: [macos-latest, windows-latest, ubuntu-18.04]
 6    node: [4, 6, 8, 10]
 7    include:
 8      - os: windows-latest
 9        node: 4
10        npm: 2
  • jobs.<job_id>.strategy.fail-fast
    任何 matrix 作业失败,取消所有进行中的作业,默认为 true
  • jobs.<job_id>.strategy.max-parallel
    使用 matrix 作业策略时同时运行的最大作业数,默认情况下,github 将最大并发运行的作业数量
1strategy:
2  max-parallel: 2
  • jobs.<job_id>.container
    用于运行作业中尚未指定容器的任何步骤的容器。
 1jobs:
 2  my_job:
 3    container:
 4      image: node:10.16-jessie
 5      env:
 6        NODE_ENV: development
 7      ports:
 8        - 80
 9      volumes:
10        - my_docker_volume:/volume_mount
11      options: --cpus 1
12
  • jobs.<job_id>.container.options
    附加 docker 容器资源选项
  • jobs.<job_id>.services
    为 ci 提供服务
 1services:
 2  nginx:
 3    image: nginx
 4    volumes:
 5      - my_docker_volume:/volume_mount
 6      - /data/my_data
 7      - /source/directory:/destination/directory
 8    ports:
 9      - 8080:80
10  redis:
11    image: redis
12    ports:
13      - 6379/tcp
14

非卖品

  项目地址:https://github.com/ferried/animal-crossing-cli

  action 地址 : https://github.com/ferried/animal-crossing-cli/runs/648812568?check_suite_focus=true

 1name: Go
 2on:
 3  push:
 4    branches: [ devp ]
 5  pull_request:
 6    branches: [ devp ]
 7jobs:
 8  release:
 9    name: release
10    runs-on: ubuntu-latest
11    steps:
12
13      - name: setup go
14        uses: actions/setup-go@v2
15        with:
16          go-version: ^1.13
17          id: go
18
19      - name: checkout code
20        uses: actions/checkout@v2
21
22      - name: install dependencies
23        run: |
24          go get -v -t -d ./...
25          if [ -f Gopkg.toml ]; then
26              curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
27              dep ensure
28          fi
29
30      - name: test code
31        run: go test
32

   


标题:GitHub Action
作者:fe
地址:https://blog.eiyouhe.com/articles/2020/04/29/1588151564522.html