Github原生CI/CD,初尝Github Actions

Github 原生 CI/CD,初尝 Github Actions

Intro

Github 目前已经推出了自己的 CICD 服务 —— Github Actions,而且比微软的 Azure DevOps Pipelines 对开发者来说更友好,使用起来更好用。

Github Actions 核心概念

总体看下来感觉是从 Azure Pipelines 迁移过来的东西,有许多概念和 Azure Pipelines 是类似的,如果你之前用过 azure pipelines,应该很容易上手

  • Runner 用来跑 cicd build 的服务器
    • Github Hosted Runner Github 官方提供的 Runner
    • Self-Hosted Runner 用自己的服务器作为 Runner
  • Workflow 定义 CI/CD 的流程,需要执行哪些操作,需要做什么
  • Workflow 定义 workflow 的配置文件,通常放在项目根目录下的 .github/workflows 文件夹下
  • Workflow Run 每一次 CI/CD build
  • Event 触发 ci/cd build 的事件,如 push/issue/pr
  • Job 由一系列 Step 组成,Job 可以并行执行也可以串行执行,每一个 Job 都是一个新的环境
  • Step 对应 Job 执行的每一个步骤
  • Action 对应 Step 里执行的可复用的操作

Github Actions 配置示例

来看一个 Github Actions 的 dotnet 配置:

name: dotnetcore # workflow name

on: [push] # event trigger,什么事件触发 build

jobs:
  build:
    runs-on: ubuntu-latest # 指定 runner,使用 Github 提供的 runner

    steps:
    - uses: actions/checkout@v1 # checkout
    - name: Setup .NET Core # 设置 dotnet core 环境
      uses: actions/setup-dotnet@v1
      with:
        dotnet-version: 3.0.100
    - name: dotnet info # 输出 dotnet -info,查看 dotnet 版本信息
      run: dotnet --info
    - name: build
      run: bash build.sh # 在 bash 中运行 build 脚本

Github 示例: https://github.com/WeihanLi/WeihanLi.Common/blob/dev/.github/workflows/dotnetcore.yml

More

徽章:

Sample:

[![Github Build Status](https://github.com/WeihanLi/WeihanLi.Common/workflows/dotnetcore/badge.svg?branch=dev)](https://github.com/WeihanLi/WeihanLi.Common/actions?query=workflow%3Adotnetcore+branch%3Adev)

Github Build Status

https://github.com/<OWNER>/<REPOSITORY>/workflows/<WORKFLOW_NAME>/badge.svg

https://github.com/<OWNER>/<REPOSITORY>/workflows/<WORKFLOW_NAME>/badge.svg?branch=<branch-name>

Summary

总体来说,用起来还可以,但是感觉还是不如 travis-ci 以及 azure pipelines成熟,比如说常用 ci 都支持的 commit message 里包含 [skip ci] 的不触发 build,目前 Github Action 还是不支持的,不过毕竟是新推出来的产品,相信以后一定会越来越好哒,想尝试的小伙伴们可以实践一下

Reference

猜你喜欢

转载自www.cnblogs.com/weihanli/p/11980499.html