Action 基础
常用Action介绍
actions/checkout@v2
actions/checkout@v2: 进入到被推送的代码下仓库内容。通常需要以来仓库内容的时候加入,比如:打包构建、读取 package.json
文件等
actions/setup-node@v2
actions/setup-node@v2: 安装设置 node 版本,接下来可以使用 with
指明版本。
也可以设置使用多版本 node:
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node: [6, 8, 10]
steps:
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node }}
appleboy/ssh-action
appleboy/ssh-action: 通过SSH, 在远程主机上执行命令。仅支持 Linux Docker。
actions/upload-release-asset
actions/upload-release-asset: 上传 Release 下的文件
同一 workflow 中,一个 job 使用另一个 job 的内容
Storing workflow data as artifacts
- 上传:actions/upload-artifact@v2
- 下载:actions/download-artifact@v2
常用操作
创建一个 cron job
on:
schedule:
# * is a special character in YAML so you have to quote this string
- cron: '30 5,17 * * *'
cron 语法:
设置变量
通过设置
env
为变量的值jobs: example-job: steps: - name: Connect to PostgreSQL run: node client.js env: POSTGRES_HOST: postgres POSTGRES_PORT: 5432
- 通过secrets
jobs:
example-job:
steps:
- name: Connect to PostgreSQL
run: node client.js
env:
POSTGRES_HOST: ${{ secrets.POSTGRES_HOST }
POSTGRES_PORT: ${{ secrets.POSTGRES_PORT }
读取文件内容
读取JSON
文件: ashley-taylor/read-json-property-action@v1.0
- name: 读取当前版本号
id: version
uses: ashley-taylor/read-json-property-action@v1.0
with:
path: ./package.json
property: version
读取文件文本内容:juliangruber/read-file-action@v1
- name: 读取描述文件
id: description
uses: juliangruber/read-file-action@v1
with:
path: ./description.txt
如何使用:
- name: 创建GitHub Release
id: create_release
uses: actions/create-release@latest
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: v${{steps.version.outputs.value}}
release_name: v${{steps.version.outputs.value}}
body: ${{steps.description.outputs.content}}
draft: false
prerelease: false
读取某一个 step 产生的内容
read json value
on:
push:[main]
run-on: ubuntu-latest
steps:
# 读取 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}}
...
总结:
- 首先使用
id
标记这一 step 然后使用
${{steps.version.outputs.xxx}}
读取结果里面xxx字段的值执行命令
- 执行
bin
命令
- name: Build
uses: actions/setup-node@master
- run: npm install # 安装第三方包
- run: npm run build # 打包
- run: tar -zcvf release.tgz
或者:
- name: Build
uses: actions/setup-node@master
- run: |
npm install # 安装第三方包
npm run build # 打包
tar -zcvf release.tgz
执行仓库下的文件内容:run + 文件路径
jobs: example-job: steps: - name: Run build script run: ./.github/scripts/build.sh shell: bash
实践
BUILD AND TEST
每天发送天气邮件
参考教程: https://www.ruanyifeng.com/bl...
name: 'Beijing Weather Bot'
on:
push:
schedule:
- cron: '25 2 * * *'
jobs:
bot:
runs-on: ubuntu-latest
steps:
- name: Checkout codes
uses: actions/checkout@v2
# 执行命令,生成带有天气情况的 html 文件:result.html
- name: Get Weather
run: bash ./weather.sh
- name: Get Date
run: echo "REPORT_DATE=$(TZ=':Asia/Beijing' date '+%Y-%m-%d %T')" >> $GITHUB_ENV
- name: Send mail
uses: dawidd6/action-send-mail@v2
with:
server_address: smtp.163.com
server_port: 465
username: ${{secrets.MAIL_USERNAME}}
password: ${{secrets.MAIL_PASSWORD}}
subject: Beijing Weather Report (${{env.REPORT_DATE}})
body: file://result.html
to: ${{ secrets.TARGET_MAIL }}
from: Weather-Beijing
content_type: text/html
weather.sh:
#!/bin/sh
set -eux
CITY=beijing
LANGUAGE="zh-CN"
UA="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36"
UNIT=m
curl \
-H "Accept-Language: $LANGUAGE" \
-H "User-Agent: $UA" \
-o result.html \
wttr.in/$CITY?$UNIT
打包构建 & 文件上传到服务器
场景:在github上提交代码之后自动打包部署到远程服务器(腾讯云/阿里云等)
前置工作:
- 确保远程服务器的公钥内容在
~/.ssh/authorized_keys
的文件里 - 在 git > setting > Secrets > Action 下 添加TOKEN,内容是远程服务器的私钥内容
实现:
# 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 下打包构建,待完成之后将打包产物上传到远程服务器
注意:
这里
easingthemes/ssh-deploy@main
的SSH_PRIVATE_KEY
格式有要求,不然会报错。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
- 每次部署都是正式版本
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。