GitHub Actions 快速参考 - 标准结构和最有用的操作列表

GitHub Actions 快速参考 - 标准结构和最有用的操作列表

GitHub Actions

是 GitHub 内部的一个自动化和 CI/CD 平台,用于根据推送、拉取请求或定时任务等事件构建、测试和部署代码。

除了标准的 GitHub 之外,你还可以在 自托管的 Gitea 服务器 上使用 GitHub Actions。

本摘要涵盖了 GitHub Actions 的结构、简要描述以及在开源和商业工作流程中最常用的 Actions。Actions 包括官方、社区和第三方工具,支持从构建和测试到部署、发布管理、QA 和通知的自动化。

基本配置结构

GitHub Actions 工作流在仓库中的 .github/workflows 目录下的 YAML 文件中定义。

name: Workflow Name

on:

push:

branches: [ main ]

jobs:

build:

runs-on: ubuntu-latest

steps:

- uses: actions/checkout@v4

- name: 设置 Node.js

uses: actions/setup-node@v3

with:

node-version: '18'

- name: 运行测试

run: npm test

on 块指定触发事件。

每个 job 在自己的 runner 上运行。

steps 可以运行 shell 命令或调用可重用的 Actions。

核心工作流组件

组件

描述

工作流

由事件触发的自动化过程(YAML)

事件

触发工作流的活动(推送、拉取请求等)

任务

在 runner 上执行的一系列步骤,可以顺序或并行执行

步骤

任务中的每个命令或操作

runner

执行任务的虚拟机或容器

Action

封装任务的可重用扩展/附加组件

触发事件

push

pull_request

schedule(使用 cron 语法)

workflow_dispatch(手动触发,允许输入参数)

release, issue 等

示例:

on:

push:

branches: [ main ]

workflow_dispatch:

inputs:

environment:

description: '部署环境'

required: true

default: 'production'

type: string

有用的内置 Actions

Action

用途

常见参数

actions/checkout

检出仓库代码

ref, token, submodules, persist-credentials

actions/setup-node

设置 Node.js 环境

node-version, cache, architecture, check-latest

actions/setup-python

设置 Python

python-version, architecture

actions/cache

缓存依赖项

path, key, restore-keys

docker/build-push-action

构建并推送 Docker 镜像

context, file, platforms, tags, push, build-args

actions/upload-artifact

上传构建产物

name, path, if-no-files-found

actions/download-artifact

下载产物

name, path

github/email-actions

发送电子邮件通知

to, subject, content, attachments

peter-evans/create-pull-request

通过工作流创建 PR

branch, title, body, labels, base

actions/github-script

在工作流中运行任意 JavaScript

script, github-token

Action 参数

任何 Action 调用的一般参数:

- name: 某个 Action 或步骤

uses: owner/repo@ref

with:

param1: value

param2: value

env:

ENV_VAR: value

if: ${{ condition }}

run: command_to_run

shell: bash|pwsh|python|sh

continue-on-error: true|false

with:传递给 Action 的参数(请参阅 Action 的文档以获取所有选项)

env:步骤的环境变量

if:条件执行

run:shell 命令(如果不使用 Action)

shell:运行步骤所使用的 shell

continue-on-error:即使步骤失败也继续执行

定义自定义 Action 输入

在你的 Action 元数据(action.yml)中:

inputs:

example-input:

description: '一个示例输入'

required: false

default: 'default-value'

在工作流中使用时:

- uses: my/action@v1

with:

example-input: 'custom-value'

当 Action 运行时,可以通过环境变量 INPUT_EXAMPLE_INPUT 访问。

示例:带有手动参数的工作流

name: 部署

on:

workflow_dispatch:

inputs:

environment:

description: '部署环境'

required: true

default: 'production'

type: string

jobs:

deploy:

runs-on: ubuntu-latest

steps:

- name: 检出代码

uses: actions/checkout@v4

- name: 部署到环境

run: echo "正在部署到 ${{ github.event.inputs.environment }}"

Action 市场

GitHub Marketplace 上有数千个可重用的 Actions。

分类包括:云部署、通知工具、测试框架、代码分析等。

探索:actions/checkout、actions/setup-node、docker/build-push-action、actions/cache、actions/upload-artifact 等等。

快速提示

将工作流文件放在 .github/workflows 中。

使用 workflow_dispatch 与 inputs 进行手动和参数化运行。

使用 cache Action 加快依赖项安装。

使用 job matrix 测试多个操作系统/运行时版本。

使用 secrets 存储敏感数据,引用方式为 ${{ secrets.SECRET_NAME }}。

此速查表涵盖了使用、配置和扩展 GitHub Actions 以实现自动化、CI/CD 等功能的基本要点。如需更多详细信息和更新,请始终查阅官方 GitHub 文档和 Action 仓库。

常用 GitHub Actions

以下是一些用于自动化 CI/CD、测试、构建、部署和工作流任务的常用 GitHub Actions:

官方核心 Actions

Action

用途

关键示例参数

actions/checkout

检出仓库代码

ref, submodules

actions/setup-node

设置 Node.js 环境

node-version, cache

actions/setup-python

设置 Python 环境

python-version

actions/setup-java

设置 Java JDK

distribution, java-version

actions/cache

缓存依赖项和构建输出

path, key, restore-keys

actions/upload-artifact

上传构建产物

name, path

actions/download-artifact

从工作流下载产物

name, path

actions/github-script

使用 GitHub 上下文和 API 运行 JavaScript

script

actions/create-release

创建 GitHub 发布

tag_name, release_name

actions/upload-release-asset

上传发布资产

upload_url, asset_path

actions/labeler

自动为问题/PR 添加标签

repo-token, configuration-path

流行的社区和第三方 Actions

Action

用途

docker/build-push-action

构建并推送 Docker 镜像

actions/setup-go

设置 Go 环境

super-linter/super-linter

通用自动代码检查

trufflesecurity/trufflehog

扫描秘密和凭证

peaceiris/actions-gh-pages

将静态网站部署到 GitHub Pages

JamesIves/github-pages-deploy-action

将项目部署到 GitHub Pages

peter-evans/create-pull-request

自动创建拉取请求

softprops/action-gh-release

创建并上传 GitHub 发布

ad-m/github-push-action

将更改推回 GitHub 仓库

actions/setup-dotnet

设置 .NET SDK

azure/login

认证到 Azure

google-github-actions/auth

认证到 Google Cloud

测试、QA 和报告 Actions

Action

用途

actions/setup-ruby

设置 Ruby 环境

codecov/codecov-action

将代码覆盖率报告上传到 Codecov

coverallsapp/github-action

将覆盖率结果上传到 Coveralls

dorny/test-reporter

将测试结果附加到 GitHub Checks

stefanzweifel/git-auto-commit-action

自动提交和推送文件更改

部署和通知 Actions

Action

用途

appleboy/scp-action

使用 SCP 上传文件

SamKirkland/FTP-Deploy-Action

通过 FTP/FTPS 部署文件

cypress-io/github-action

运行 Cypress 端到端测试

slackapi/slack-github-action

向 Slack 发送消息

dawidd6/action-send-mail

在工作流运行期间发送电子邮件

工具和工作流管理

Action

用途

peter-evans/repository-dispatch

触发外部仓库的工作流

fregante/setup-git-user

设置提交的 Git 用户凭证

andymckay/labeler

根据路径为问题和 PR 添加标签

actions/configure-pages

配置发布到 GitHub Pages

EndBug/add-and-commit

在工作流中添加并提交文件

dangoslen/changelog-enforcer

检查 PR 是否更新了变更日志

如何查找更多

有许多 GitHub Actions 可以实现工作流自动化,例如自动化测试、部署到生产环境、通知和与其他服务的集成。

GitHub Marketplace

列出了测试、安全、部署、通知、代码质量和集成等类别中的数千个 Actions。

“Awesome Actions” 等精选列表也是灵感和发现的好资源。

有用的链接

https://docs.github.com/actions

https://github.com/marketplace

https://docs.github.com/en/actions/get-started/quickstart

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

GIT 速查表

Gitea - 安装和测试

备份和恢复 Gitea 服务器

使用 Gitea Actions 将 Hugo 网站部署到 AWS S3

Gitflow:步骤、替代方案、优缺点

使用 GitOps 的 DevOps - Argo CD、Flux、Jenkins X、Weave GitOps 等

相关文章

bat365官网登录下载
迪凯特pe

迪凯特pe

📅 08-09 👀 930