主题
常用 Actions
基础操作
actions/checkout
actions/checkout 是一个用于检出代码库的 GitHub Action,通常是工作流的第一步,用于将代码库的内容拉取到工作流运行环境中。
yaml
- name: Checkout repository
uses: actions/checkout@v4常用参数:
ref: 指定检出的分支、标签或提交哈希。yamlwith: ref: 'main'submodules: 是否检出子模块,默认为false。yamlwith: submodules: truefetch-depth: 拉取的提交历史深度,默认为1。yamlwith: 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: 指定需要缓存的文件或目录。yamlwith: path: node_moduleskey: 缓存的唯一键值。yamlwith: key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}restore-keys: 当key不匹配时,尝试使用的备用键值。yamlwith: 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 的版本。yamlwith: node-version: '16'registry-url: 设置 npm 的注册表地址。yamlwith: registry-url: 'https://registry.npmjs.org'cache: 启用依赖缓存,支持npm、yarn和pnpm。yamlwith: 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 的版本。yamlwith: version: '8.0.0'run_install: 是否在安装后自动运行pnpm install,默认为false。yamlwith: 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: 部署的目标分支。yamlwith: branch: gh-pagesfolder: 部署的文件夹路径。yamlwith: folder: disttoken: 用于身份验证的 GitHub Token。yamlwith: 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 的标签。yamlwith: tag: ${{ github.ref_name }}name: Release 的名称。yamlwith: name: Release ${{ github.ref_name }}body: Release 的描述内容。yamlwith: body: | Release notes here.draft: 是否为草稿,默认为false。yamlwith: draft: falseprerelease: 是否为预发布版本,默认为false。yamlwith: 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 构建的上下文路径。yamlwith: context: .push: 是否推送镜像,默认为false。yamlwith: push: truetags: 镜像的标签列表。yamlwith: tags: | ghcr.io/<你的用户名>/<你的仓库>:latest ghcr.io/<你的用户名>/<你的仓库>:${{ steps.vars.outputs.version }}
