更新工作流以生成构建文件
在这一步中,你将修改工作流文件(workflow file)以模拟一个构建过程。你将添加一个步骤来创建一个 dist 目录和一个虚拟的构建产物(artifact)文件。
- 在你的 GitHub 仓库页面
github-actions-demo 上,点击绿色的 Code 按钮。
- 确保选择了 HTTPS 标签页,并复制 URL。它应该看起来像
https://github.com/your-username/github-actions-demo.git。
- 在 LabEx 环境中打开终端。默认路径是
~/project。
- 使用
git clone 命令下载仓库。将 your-username 替换为你实际的 GitHub 用户名。
cd ~/project
git clone https://github.com/your-username/github-actions-demo.git
示例输出:
Cloning into 'github-actions-demo'...
remote: Enumerating objects: X, done.
remote: Counting objects: 100% (X/X), done.
remote: Total X (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (X/X), done.
- 进入克隆下来的仓库目录:
cd ~/project/github-actions-demo
-
使用 WebIDE 编辑器创建一个新的工作流文件 .github/workflows/upload-artifacts.yml。你可以在左侧的文件浏览器中 project/github-actions-demo/.github/workflows/ 路径下找到该文件。
-
首先创建基本的工作流结构。添加工作流名称和触发器:
name: Upload Artifacts
on: [push]
- 添加
jobs(任务)部分,并定义 build 任务及其运行器(runner):
jobs:
build:
runs-on: ubuntu-latest
- 添加
steps(步骤)部分。首先,添加 checkout 步骤:
steps:
- uses: actions/checkout@v4
- 添加 Node.js 设置步骤:
- name: Use Node.js
uses: actions/setup-node@v4
with:
node-version: "20"
- 添加安装依赖的步骤:
- name: Install dependencies
run: npm install
- 添加构建步骤,该步骤创建
dist 目录和其中的一个文件:
- name: Build project
run: |
mkdir dist
echo "This is the build artifact" > dist/build.txt
- 添加测试步骤:
- name: Run tests
run: npm test
现在你的完整文件应该如下所示:
name: Upload Artifacts
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Use Node.js
uses: actions/setup-node@v4
with:
node-version: "20"
- name: Install dependencies
run: npm install
- name: Build project
run: |
mkdir dist
echo "This is the build artifact" > dist/build.txt
- name: Run tests
run: npm test
解释
mkdir dist: 创建一个名为 dist 的目录。
echo ... > dist/build.txt: 在 dist 内部创建一个简单的文本文件,以模拟编译后的资源(asset)。
修改完成后,保存文件(Ctrl+S 或 Cmd+S)。