GitHub Actions 使用笔记

基本概念

Workflow :工作流程是可配置的自动化过程,可以运行一个或多个作业。它由一个或多个jobs构成,每个job由多个step构成,而每个step可以依次执行一个或多个action,

Event: 事件是存储库中发生的特定活动,触发工作流程运行。

Job: 在GitHub Actions中,Job是一个持续集成运行中的任务单元,代表一次持续集成的运行,可以完成多个任务。一个Workflow由一个或多个Jobs构成,而每个Job由多个Steps构成,一步步完成具体的操作。

Step:一步步的运行操作

Runners:运行器是在触发时运行工作流程的服务器。

触发条件

触发条件 有很多,可以是issue 创建,可以是 git 的 pr 和push ,可以是 定时器,全部的事件

yml 复制代码
on:
  issues:
    types:
      - opened
      - labeled
  push:
    branches:
      - main
    tags:        
      - v2
      - v*
  schedule:
    # * is a special character in YAML so you have to quote this string
    - cron:  '30 5,17 * * *'

授权

在构建过程中,会读写一些内容,根据自己的需求设置权限,这里是全部的权限,可以在整个job中设置,也可以在每个 step 中设置

yaml 复制代码
permissions:
  actions: read|write|none
  checks: read|write|none
  contents: read|write|none
  deployments: read|write|none
  id-token: read|write|none
  issues: read|write|none
  discussions: read|write|none
  packages: read|write|none
  pages: read|write|none
  pull-requests: read|write|none
  repository-projects: read|write|none
  security-events: read|write|none
  statuses: read|write|none

permissions: write-all

条件

依赖条件

yaml 复制代码
jobs:
	job1:
	job2:
	    needs: job1
	job3:
	    needs: [job1, job2]

	production-deploy:
	    if: github.repository == 'octo-org/octo-repo-prod'
	    runs-on: ubuntu-latest
		steps:
		  - name: My first step
		    if: ${{ github.event_name == 'pull_request' && github.event.action == 'unassigned' }}
		    run: echo This event is a pull request that had an assignee removed.

运行时判断

yaml 复制代码
name: Run a step if a secret has been set
on: push
jobs:
  my-jobname:
    runs-on: ubuntu-latest
    env:
      super_secret: ${{ secrets.SuperSecret }}
    steps:
      - if: ${{ env.super_secret != '' }}
        run: echo 'This step will only run if the secret has a value set.'
      - if: ${{ env.super_secret == '' }}
        run: echo 'This step will only run if the secret does not have a value set.'

运行环境

yaml 复制代码
jobname:
	runs-on: [self-hosted, linux, x64, gpu]

可用的运行环境

ubuntu-latest, ubuntu-22.04, ubuntu-20.04 The ubuntu-latest label currently uses the Ubuntu 22.04 runner image.
windows-latest, windows-2022, windows-2019 The windows-latest label currently uses the Windows 2022 runner image.
macos-latest, macos-14, macos-13, macos-12, macos-11 The macos-latest workflow label currently uses the macOS 14 runner image.
yaml 复制代码
steps:
  - name: Display the path
    shell: bash
    run: echo $PATH

steps:
  - name: Display the path
    shell: python
    run: |
      import os
      print(os.environ['PATH'])

组合环境

yaml 复制代码
strategy:
      matrix:
        include:
          - site: "production"
            datacenter: "site-a"
          - site: "staging"
            datacenter: "site-b"

环境变量

yaml 复制代码
environment:
  name: production_environment
  url: https://github.com

The value of url can be an expression. Allowed expression contexts: github, inputs, vars, needs, strategy, matrix, job, runner, and env. For more information about expressions,

github doc

使用公共的action

yaml 复制代码
jobs:
  my_first_job:
    steps:
      - name: My first step
        # Uses the default branch of a public repository
        uses: actions/heroku@main
      - name: My second step
        # Uses a specific version tag of a public repository
        uses: actions/aws@v2.0.1

上传 构建产物

bash 复制代码
   - uses: actions/upload-artifact@v3
        if: matrix.os == 'ubuntu-latest'
        with:
          name: artifacts-${{ matrix.arch }}
          path: |
            ./target/${{ matrix.target }}/release/bundle/appimage/**.AppImage.*
            ./target/${{ matrix.target }}/release/bundle/deb/**.deb

发布

yaml 复制代码
 - name: Create Release
        uses: softprops/action-gh-release@master
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: v${{ env.version }}
          name: ChatGPT v${{ env.version }}
          body: See the assets to download this version and install.
          prerelease: false
          generate_release_notes: false
          files: ./artifacts/**/*
相关推荐
telllong2 小时前
Cursor AI vs GitHub Copilot vs Cline:三大AI编程工具深度横评
人工智能·github·copilot
Jurio.12 小时前
本机开发 + 多机执行的极简远端运行工具
linux·git·python·github·远程工作
Hical_W15 小时前
为 C++ Web 框架设计三层 PMR 内存池:从原理到实战
c++·github
CoovallyAIHub18 小时前
无人机拍叶片→AI找缺陷:CEA-DETR改进RT-DETR做风电叶片表面缺陷检测,mAP50达89.4%
算法·架构·github
CoovallyAIHub18 小时前
混合训练反而更差?VLM Agent在训练前协调跨数据集标注,文档布局检测F-score从0.860提升至0.883
算法·架构·github
OpenTiny社区18 小时前
多端开发头疼?TinyVue 3.30 一招搞定,AI还帮你写代码!
前端·vue.js·github
峰向AI19 小时前
Vercel 官方出品,你的 24 小时 AI 编程助手
人工智能·github
研究点啥好呢20 小时前
Github热门项目推荐 | 开放数据的新时代
大数据·人工智能·机器学习·github·数据
逛逛GitHub20 小时前
帮你节省 65% 的 token,这个 GitHub 项目让 Claude 模仿原始人。
github
字符串str1 天前
nikto中文解释和常用的github信息收集语法
github