如何构建你的第一个 GitHub 工作流(上)

「这是我参与2022首次更文挑战的第26天,活动详情查看:2022首次更文挑战」。

什么是 GitHub 操作?

首先,我们需要区分“GitHub Actions”和“Action”。前者是产品名称,后者是自定义代码,您可以将其作为完成任务的步骤包含在工作流作业中。

GitHub Actions 的组件是什么?

在开始编写代码之前,了解 GitHub Actions 的构建块对我们很重要。

构建你的第一个 github-action_components

让我们分解这个图,从左到右:

  1. 事件:这是触发动作的事件。它表示存储库中将触发工作流运行的活动。
  2. 工作流:这是事件发生时运行的工作流。
  3. 作业:为完成一项任务而按顺序运行的一组步骤。每个作业都在自己的运行器上运行。
  4. Step:一个步骤可以是一个 shell 脚本,也可以是一个动作,它将在为该步骤所属的作业分配的运行器上运行。
  5. Runner:Runner 是运行作业中的步骤的虚拟机(或任何具有受支持操作系统的计算机)。

这在 GitHub 的大量文档中得到了很好的解释,您可以在此处阅读有关组件的更多信息。

我什么时候需要创建一个动作?

由于每个步骤都可以是 shell 脚本或动作,我们如何决定选择哪个选项?

如果你对以下任何问题回答“是”,那么你最好创建一个操作:

  1. 你是否需要构建无法用 shell 脚本编写的复杂逻辑?
  2. 你打算使用任何第三方库吗?
  3. 你是否需要对第三方服务进行 API 调用?
  4. 您是否需要能够在不同的操作系统上运行此操作?
  5. 你是否精通 JavaScript 但不精通 Bash 或 PowerShell?

让我们创建我们的动作

我们将构建一个 Action,只要在我们的存储库上打开拉取请求,它就会创建一个评论,并根据更改的文件类型添加标签。评论将包含拉取请求中引入的更改的摘要。

构建你的第一个 github 预览

1.创建一个空的公共仓库

让我们从创建一个名为: 的空 GitHub 存储库开始PR-metadata-action。这将是我们将用来存储我们的操作的存储库。

它必须是公开的,否则我们将无法在我们的工作流程中使用它。

build-your-first-github-action_newrepo-1

.在本地克隆仓库,初始化一个Node项目

转到您希望存储操作存储库的文件夹。然后让我们在我们的机器上克隆存储库:

$ git clone [email protected]:Link-/PR-metadata-action.git
Cloning into 'PR-metadata-action'...
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (4/4), done.
Receiving objects: 100% (4/4), done.
remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
复制代码

在我们新创建的存储库文件夹中,让我们初始化一个新的 Node.js 项目:

$ cd PR-metadata-action/
$ npm init -y
Wrote to /Users/link-/PR-metadata-action/package.json:

{
  "name": "pr-metadata-action",
  "version": "1.0.0",
  "description": "Adds pull request file changes as a comment to a newly opened PR",
  "main": "index.js",
  "scripts": {
    "test": "echo "Error: no test specified" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/Link-/PR-metadata-action.git"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/Link-/PR-metadata-action/issues"
  },
  "homepage": "https://github.com/Link-/PR-metadata-action#readme"
}
复制代码

3. 创建动作元数据文件

让我们创建action.yml. 这个文件非常重要,因为它将定义interface我们的 Action:

  • 输入:包含操作预期在运行时使用的数据的参数
  • 输出:动作完成后设置的数据。这次我们不会为我们的操作提供输出。
  • runs:指定动作的执行运行时,在本例中为 node16

阅读有关元数据文件语法的更多信息。

name: 'PR Metadata Action'
description: 'Adds pull request file changes as a comment to a newly opened PR'
inputs:
  owner:
    description: 'The owner of the repository'
    required: true
  repo:
    description: 'The name of the repository'
    required: true
  pr_number:
    description: 'The number of the pull request'
    required: true
  token:
    description: 'The token to use to access the GitHub API'
    required: true
runs:
  using: 'node16'
  main: 'index.js'
复制代码

4. 添加 Actions 工具包

GitHub 创建了一个开源软件开发工具包 (SDK),可以让你在创建操作时更加轻松。

我们今天将使用的两个主要包是:

  • @actions/core:这个包包含了Action的核心功能,比如context包含当前运行信息的inputs对象,包含action参数的outputs对象,以及将包含action设置的数据的对象完成后。
  • @actions/github:这个包包含我们将用来与 GitHub API 交互的 GitHub API REST 客户端。
$ npm install @actions/core
added 3 packages, and audited 4 packages in 1s

found 0 vulnerabilities

$ npm install @actions/github
added 21 packages, and audited 25 packages in 1s

found 0 vulnerabilities
复制代码

我们的文件夹结构现在应该如下所示:

/Users/link-/PR-metadata-action
├── LICENSE
├── README.md
├── action.yml
├── node_modules
├── package-lock.json
└── package.json

1 directory, 6 files
复制代码

未完待续

おすすめ

転載: juejin.im/post/7068631753262383134