Skip to main content

功能模板

常用模块列表

模块说明备注
softprops/action-gh-release@v1
actions/checkout@v2、v3
actions/setup-node@v3
softprops/action-gh-release@v1
thedoctor0/zip-release@master

release 发布文件

模块名:softprops/action-gh-release@v1

- name: Zip the Build
run: zip -r ${{ secrets.ReleaseZipName }} ./AppName/bin/Release/netcoreapp3.1/

- name: Create Release and Upload Release Asset
uses: softprops/action-gh-release@v1
if: startsWith(github.ref, 'refs/tags/')
with:
tag_name: ${{ github.ref }}
name: Release ${{ github.ref }}
body: TODO New Release.
draft: false
prerelease: false
files: |
${{ secrets.ReleaseZipName }}.zip
LICENSE

打包成zip并发布

模块名:thedoctor0/zip-release@master

官方地址

name: Create Archive
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- name: Archive Release
uses: thedoctor0/zip-release@master
with:
type: 'zip'
filename: 'release.zip'
exclusions: '*.git* /*node_modules/* .editorconfig'

注意事项:

  • filename 指定的文件名不能带空格

下载仓库文件

模块名:actions/checkout@v2

name: Download Repo

jobs:
download_repo:
name: start download repo files
runs-on: ubuntu-latest
steps:
- name: start ...
uses: actions/checkout@v2 # 这里也可以使用 v3版本 2022年截至

上传文件

模块名:actions/upload-artifact@v3

steps:
- uses: actions/checkout@v2 # 下载仓库文件

- run: mkdir -p path/to/artifact # 创建一个空目录
- run: echo hello > path/to/artifact/world.txt # 创建一个新文件

- uses: actions/upload-artifact@v3 # 上传新的文件到仓库
with:
name: my-artifact
path: path/to/artifact/world.txt

配置node环境

模块名:actions/setup-node@v3

steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '14'
- run: npm install
- run: npm test

配置node环境,指定版本

name: Install Nodejs
env:
NODE_VERSION: "14"
jobs:
job-name:
runs-on: ubuntu-latest
steps:
- name: Setup Node.js (安装node环境)
uses: actions/setup-node@v1 # 第三方actions
with:
node-version: ${{env.NODE_VERSION}} # 选择要使用的 node 版本

- name: Cache dependencies # 缓存 node_modules
uses: actions/cache@v2
id: yarn-cache
with:
path: |
**/node_modules
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-

# 如果缓存没有命中,安装依赖
- name: Install dependencies
if: steps.yarn-cache.outputs.cache-hit != 'true'
run: yarn --frozen-lockfile

# 运行构建脚本
- name: Build VuePress site
run: yarn docs:build # 执行构建命令

配置 python 环境

模块名:actions/setup-python@v3

jobs:
job-name:
steps:
- name: install Python
uses: actions/setup-python@v3

发布到 Git Pages


发布npm包

name: Publish Package to npmjs
on:
release:
types: [created]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Publish Package to npmjs
- uses: actions/checkout@v3
# Setup .npmrc file to publish to npm
- uses: actions/setup-node@v3
with:
node-version: '16.x' # 指定版本
registry-url: 'https://registry.npmjs.org' # 指定npm源
- run: npm ci
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

发布release

release:
name: Release Repo
runs-on: ubuntu-latest
steps:
- name: start
uses: actions/download-artifact@v2 # 下载指定的仓库文件

build node 项目

name: Release
on:
push:
tags:
- 'v*.*.*' # 所有带tag的推送时

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: install
run: |
npm i
npm run build

- name: Release
uses: softprops/action-gh-release@v1
with:
files: ./dist/*
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

发布Github Pages

name: CI

on: [push]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Run build and test
run: echo build and test
- name: Run a multi-line script
run: |
npm i
npm run build
npm run test


pages:
runs-on: ubuntu-latest
needs: [build]
steps:
- uses: actions/checkout@v2
- name: post pages
run: echo post pages
- name: Run a multi-line script
run: |
npm i
npm run build
npm run pages
- name: GitHub Pages
uses: crazy-max/ghaction-github-pages@v1.3.0
with:
build_dir: pages
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Matrix 矩阵 基础模板

jobs:
build:
strategy:
matrix:
# 指定node版本 矩阵指定多版本
node-version: [12.x, 14.x, 15.x]
# 指定os
os: ['ubuntu-latest', 'windows-latest', 'macos-latest']
# 用变量的形式,自动分配
runs-on: ${{ matrix.os }}

# 用变量的形式,自动分配
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}

#有些任务是不能在其他环境运行的
# 比如GabrielBB/xvfb-action@v1 只能在linux 下使用
# 用if条件判断一下就好
- name: Run headless unit test
if: matrix.os == 'ubuntu-latest'
uses: GabrielBB/xvfb-action@v1
with:
run: |
yarn test
yarn e2e

# node 后续任务