GitHub Actions CICD
一、基本概念 Basic Concepts
-
本次课程是 GitHub Actions 的继续课程,添加了对 CI/CD 线程的深入讲解。
This session is a continuation of GitHub Actions, going deeper into CI/CD workflows. -
主要基于 Next.js 项目进行 Hands-on 实操。
Mainly involves hands-on practice with a Next.js project. -
通过 GitHub Action 实现以下流程: build -> test -> publish.
Workflow: build -> test -> publish using GitHub Actions.
二、GitHub Actions 框架说明 Structure of GitHub Actions
-
Workflow: 一个 workflow 和 repo 关联,可包含多个 jobs
A workflow is tied to a repo and may include multiple jobs. -
Job: 可应用于运行一些相关操作,可应用 runner 运行
Jobs perform specific tasks and run on runners. -
Runner: 提供培训环境,如 ubuntu-latest
Provides the execution environment, e.g., ubuntu-latest. -
Step: 一个 job 内的实际操作步骤
Steps are the actual actions within a job. -
uses
: 使用预定义好的 action
Calls pre-defined actions. -
run
: 运行 shell 指令
Executes shell commands.
三、Workflow 触发条件 Trigger Events
-
push: 代码 push 时触发
Triggered on code push. -
pull_request: 创建 PR 时触发
Triggered on PR creation. -
workflow_dispatch: 手动触发
Manually triggered. -
schedule: 按 cron 表进行进度执行
Runs on schedule (cron format). -
可对分支名进行 filter 或 ignore
Can filter or ignore specific branches.
四、常用功能 Common Features
-
outputs: 在 jobs/steps 之间传递值
Pass values between jobs/steps. -
cache: 缓存 dependency 加快下一次 job
Cache dependencies to speed up jobs. -
artifacts: 上传输出文件
Upload output files (e.g., for deployment).
五、Variables & Secrets
-
env: 普通变量,可定义在 workflow / job / step 级别
Normal variables can be defined at workflow/job/step level. -
secrets: 用于密码/网络证书等
Used for passwords, API keys, etc. -
GitHub Environment: 定义 dev / uat / prod 环境分别存储 secrets 值
Define different environments (dev/uat/prod) to store secrets.
六、Condition 条件运行
-
可以通过
if
条件条件控制 job/step 是否运行
Useif
to conditionally run jobs or steps. -
例如 only run deploy if tests succeed.
Example: deploy only runs if tests pass.
七、Custom Action 定制主动 Action
-
创建
.github/actions/<name>/action.yml
格式
Define a custom action via.github/actions/<name>/action.yml
. -
可采用 run/bash/scripts 等写法
Can userun
, bash scripts, etc. -
通过
uses: ./github/actions/<name>
引用
Reference viauses: ./github/actions/<name>
.
八、Hands-on 实操例子
-
利用
checkout
,setup-node
,run
,artifact
,deploy to ECS
完成实战
Practice using actions likecheckout
,setup-node
,run
,artifact
,deploy to ECS
. -
通过 Notion 项目列表和 fork 脚本项目进行操作
Refer to the Notion page and fork starter repo to practice.
九、进阶指南 Advanced Topics
-
reusable workflows: 其他 workflow 可以 call 当前设计
Reusable workflows can be called from other workflows. -
expressions: 最好选择看 GitHub 官方表达式文档
Use GitHub’s expression syntax for advanced conditionals. -
event activity type: 举例 open/edit/merge/delete 等 PR 操作分类
PR event activities like open/edit/merge/delete. -
filters: 对 branch / path 进行 include/过滤
Filters on branches and paths.