Action 根底
罕用性能
实际
一 每天发送天气邮件
二 打包构建 & 文件上传到服务器
场景:在 github 上提交代码之后主动打包部署到近程服务器(腾讯云 / 阿里云等)
实现:
# workflow 名
name: deploy to tencentCloud
on: # 此 CI/CD 触发时的事件
push: # 在代码提交时主动触发
branches:
- main
# 一个 CI/CD 的工作流有许多 jobs 组成,比方最典型的 job 是 lint,test,build。jobs:
build: # 构建 job
runs-on: ubuntu-latest # 跑 workflow 的服务器零碎
steps: # job 的一系列动作
# 切换分支获取源码
- name: Checkout # step 的名称,将会在 github action 的控制台中显示
# 抉择一个 action,能够了解为若干 steps.run,有利于代码复用
uses: actions/checkout@v2
# 装置应用 node:14
- name: use Node.js 14
uses: actions/setup-node@v1
with:
node-version: 14
# 运行命令,npm install && npm run build
- name: npm install and build
run: |
npm install
npm run build
env:
CI: true
# 部署到腾讯云服务器
- name: 上传到腾讯云
uses: easingthemes/ssh-deploy@main
env:
# 本地.ssh 文件下的私钥 id_rsa,存在 secrets 的 TOKEN 中
SSH_PRIVATE_KEY: ${{secrets.TOKEN}}
# 复制操作的参数。"-avzr --delete" 象征部署时清空服务器目标目录下的文件
ARGS: "-avzr --delete"
# 源目录,绝对于仓库内容根目录的门路
SOURCE: "dist/"
# 近程服务器地址
REMOTE_HOST: ${{secrets.REMOTE_HOST}}
# 近程服务器用户名
REMOTE_USER: "root"
# 目标目录(近程服务器门路)TARGET: "/data/www"
阐明:
- 在 main 分支上提交代码的时候会触发 workflow
- 应用 node v14 下打包构建,待实现之后将打包产物上传到近程服务器
三 push 之后主动 release
场景:Release 之后,能够应用 jsDelivr 实现收费 CDN 的性能。能够参考这里:收费 CDN:jsDelivr+Github 应用办法
实现:
name: release CI
# Controls when the workflow will run
on:
# Triggers the workflow on push or pull request events but only for the main branch
push:
branches:
- main
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
build:
# The type of runner that the job will run on
runs-on: ubuntu-latest
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- name: Checkout
uses: actions/checkout@v2
# 读取 package.json 文件内容
- name: read version
id: version
uses: ashley-taylor/read-json-property-action@v1.0
with:
path: ./package.json
property: version
# 执行 Release
- name: Release
uses: actions/create-release@master
env:
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
with:
tag_name: v${{steps.version.outputs.value}}
release_name: v${{steps.version.outputs.value}}
body: Release v${{steps.version.outputs.value}}
draft: false
prerelease: false
阐明:
- 在 main 分支上提交代码的时候会触发 workflow
- 每次部署都是正式版本