Skip to content

常用 Actions

基础操作

actions/checkout

actions/checkout 是一个用于检出代码库的 GitHub Action,通常是工作流的第一步,用于将代码库的内容拉取到工作流运行环境中。

yaml
- name: Checkout repository
  uses: actions/checkout@v4

常用参数:

  • ref: 指定检出的分支、标签或提交哈希。
    yaml
    with:
      ref: 'main'
  • submodules: 是否检出子模块,默认为 false
    yaml
    with:
      submodules: true
  • fetch-depth: 拉取的提交历史深度,默认为 1
    yaml
    with:
      fetch-depth: 0

actions/cache

actions/cache 用于缓存依赖或构建产物,以加速后续的工作流运行。

yaml
- name: Cache dependencies
  uses: actions/cache@v3
  with:
    path: node_modules
    key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
    restore-keys: |
      ${{ runner.os }}-node-

常用参数:

  • path: 指定需要缓存的文件或目录。
    yaml
    with:
      path: node_modules
  • key: 缓存的唯一键值。
    yaml
    with:
      key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
  • restore-keys: 当 key 不匹配时,尝试使用的备用键值。
    yaml
    with:
      restore-keys: |
        ${{ runner.os }}-node-

环境配置

actions/setup-node

actions/setup-node 用于设置 Node.js 环境,支持指定 Node.js 版本、缓存依赖等功能。

yaml
- name: Setup Node.js
  uses: actions/setup-node@v4
  with:
    node-version: '16'

常用参数:

  • node-version: 指定 Node.js 的版本。
    yaml
    with:
      node-version: '16'
  • registry-url: 设置 npm 的注册表地址。
    yaml
    with:
      registry-url: 'https://registry.npmjs.org'
  • cache: 启用依赖缓存,支持 npmyarnpnpm
    yaml
    with:
      cache: 'npm'

pnpm/action-setup

pnpm/action-setup 用于设置 pnpm 环境,支持指定版本并自动安装依赖。

yaml
- name: Install pnpm
  uses: pnpm/action-setup@v4
  with:
    version: latest
    run_install: false

常用参数:

  • version: 指定 pnpm 的版本。
    yaml
    with:
      version: '8.0.0'
  • run_install: 是否在安装后自动运行 pnpm install,默认为 false
    yaml
    with:
      run_install: true

部署与发布

JamesIves/github-pages-deploy-action

JamesIves/github-pages-deploy-action 用于将构建产物部署到 GitHub Pages。

yaml
- name: Deploy 🚀
  uses: JamesIves/github-pages-deploy-action@v4
  with:
    branch: gh-pages
    folder: dist
    token: ${{ secrets.CICD }}

常用参数:

  • branch: 部署的目标分支。
    yaml
    with:
      branch: gh-pages
  • folder: 部署的文件夹路径。
    yaml
    with:
      folder: dist
  • token: 用于身份验证的 GitHub Token。
    yaml
    with:
      token: ${{ secrets.CICD }}

actions/create-release

actions/create-release 用于在 GitHub 上创建 Release。

yaml
- name: Create github release
  uses: actions/create-release@v1
  env:
    GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  with:
    tag: ${{ github.ref_name }}
    name: Release ${{ github.ref_name }}
    body: |
      Release notes here.
    draft: false
    prerelease: false

常用参数:

  • tag: Release 的标签。
    yaml
    with:
      tag: ${{ github.ref_name }}
  • name: Release 的名称。
    yaml
    with:
      name: Release ${{ github.ref_name }}
  • body: Release 的描述内容。
    yaml
    with:
      body: |
        Release notes here.
  • draft: 是否为草稿,默认为 false
    yaml
    with:
      draft: false
  • prerelease: 是否为预发布版本,默认为 false
    yaml
    with:
      prerelease: false

docker/build-push-action

docker/build-push-action 用于构建并推送 Docker 镜像。

yaml
- name: Build and push
  uses: docker/build-push-action@v5
  with:
    context: .
    push: true
    tags: |
      ghcr.io/<你的用户名>/<你的仓库>:latest
      ghcr.io/<你的用户名>/<你的仓库>:${{ steps.vars.outputs.version }}

常用参数:

  • context: Docker 构建的上下文路径。
    yaml
    with:
      context: .
  • push: 是否推送镜像,默认为 false
    yaml
    with:
      push: true
  • tags: 镜像的标签列表。
    yaml
    with:
      tags: |
        ghcr.io/<你的用户名>/<你的仓库>:latest
        ghcr.io/<你的用户名>/<你的仓库>:${{ steps.vars.outputs.version }}

基于 MIT 许可发布