CodeRoadMap
首页路线题库资源库文章社区
CodeRoadMap

程序员的学习成长路线图。内容免费开放,登录后可同步路线图与课时进度。

由 程序员柏风 独立维护 · 关于作者

学习

  • 路线图
  • 课程
  • 面试题
  • 文章
  • 我

更多

  • 资源
  • 社区
  • 关于作者
  • 内容说明

© 2026 CodeRoadMap

津ICP备2026012044号-1·津公网安备12011002026155号·coderoadmap@126.com
GitHub 协作进阶/课时/GitHub Actions
RawBlame
课时目录12 节

GitHub 协作进阶

  1. 01GitHub 路线图概览
  2. 02账号与 Profile
  3. 03仓库管理
  4. 04提交与历史
  5. 05分支与变基
  6. 06远程与 Fork
  7. 07Pull Request
  8. 08Issues 协作
  9. 09GitHub Actions
  10. 10组织与团队
  11. 11Pages 与静态站
  12. 12综合练习

GitHub Actions

workflow-triggers、secrets、runners、marketplace-actions。

35 分钟

GitHub Actions

GitHub Actions 在仓库内用 YAML 定义 CI/CD:push/PR 时自动测试、构建、部署。本课对应 git-github 路线图 的 workflow-triggers、secrets、runners、marketplace-actions。

系统学习见 github-actions-essentials;本地 Git 触发 push 见 git-essentials 与 ghg-pr。

学习目标

  • 编写最小 workflow:触发器 + job + steps
  • 配置 secrets 与 PR 安全边界
  • 了解 hosted runner 与 Marketplace actions

第一个 Workflow

文件路径:.github/workflows/ci.yml

name: CI

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: "20"
          cache: "npm"
      - run: npm ci
      - run: npm test

push 到 main 或针对 main 的 PR 都会跑 test job。

触发器要点

on:
  push:
    paths:
      - "src/**"
      - ".github/workflows/**"
  pull_request:
    types: [opened, synchronize, reopened]
  workflow_dispatch:   # 手动触发

paths 过滤无关改动,节省分钟数(与 Actions track PR 课一致)。

Secrets 与权限

Settings → Secrets and variables → Actions 添加 NPM_TOKEN 等。

      - run: npm publish
        env:
          NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

Fork PR 默认拿不到仓库 secrets,防止恶意 PR 外泄密钥。需要 CI 对 fork 友好时,查阅官方 pull_request_target 文档并评估安全风险。

Runners

类型说明
GitHub-hostedubuntu-latest / windows-latest / macos-latest
Self-hosted自建机器,适合内网或特殊硬件
    runs-on: self-hosted

Marketplace Actions

uses: actions/checkout@v4 来自官方;第三方如 。 pin 到 (),定期关注安全公告。

docker/build-push-action
major 版本 tag
@v4

常用组合(Java/Node 详见 Actions track):

      - uses: actions/setup-java@v4
        with:
          distribution: temurin
          java-version: "21"
      - run: mvn -B test

在 PR 上看结果

PR 页 Checks 标签显示 workflow 成败;分支保护可设 Require status checks(ghg-pr)。

练习

  • 在练习仓添加 ci.yml,push 后看 Actions 页绿勾/红叉
  • 故意让测试失败,观察 PR check 状态
  • 阅读 github-actions-essentials 第一课 workflow 结构

小结

Actions = 事件驱动自动化;secrets 慎用在 fork PR。 下一课组织、团队与看板协作。

加载本节练习…
← 上一课Issues 协作下一课 →组织与团队