Action Basics
Introduction to Common Actions
actions/checkout@v2
actions/checkout@v2 : Enter the repository content under the pushed code. Usually need to be added when the content of the warehouse, such as: packaging and building, reading package.json
files, etc.
actions/setup-node@v2
actions/setup-node@v2 : Install and set the node version, then use with
specify the version.
You can also set up multiple versions of 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 : Execute commands on a remote host via SSH. Only Linux Docker is supported.
actions/upload-release-asset
actions/upload-release-asset : Upload files under Release
In the same workflow, one job uses the content of another job
Storing workflow data as artifacts
- Upload: actions/upload-artifact@v2
- Download: actions/download-artifact@v2
Common operations
Create a cron job
on:
schedule:
# * is a special character in YAML so you have to quote this string
- cron: '30 5,17 * * *'
cron syntax:
set variable
By setting
env
as the value of the variablejobs: example-job: steps: - name: Connect to PostgreSQL run: node client.js env: POSTGRES_HOST: postgres POSTGRES_PORT: 5432
- by secrets
jobs:
example-job:
steps:
- name: Connect to PostgreSQL
run: node client.js
env:
POSTGRES_HOST: ${{ secrets.POSTGRES_HOST }
POSTGRES_PORT: ${{ secrets.POSTGRES_PORT }
read file content
Read JSON
file: 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
Read file text content: juliangruber/read-file-action@v1
- name: 读取描述文件
id: description
uses: juliangruber/read-file-action@v1
with:
path: ./description.txt
how to use:
- 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
Read the content generated by a 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}}
...
Summarize:
- First mark this step
id
Then use
${{steps.version.outputs.xxx}}
read the value of the xxx field in the resultExcuting an order
- Execute the
bin
command
- name: Build
uses: actions/setup-node@master
- run: npm install # 安装第三方包
- run: npm run build # 打包
- run: tar -zcvf release.tgz
or:
- name: Build
uses: actions/setup-node@master
- run: |
npm install # 安装第三方包
npm run build # 打包
tar -zcvf release.tgz
Execute the file content under the warehouse: run + file path
jobs: example-job: steps: - name: Run build script run: ./.github/scripts/build.sh shell: bash
practice
BUILD AND TEST
Send weather emails daily
Reference tutorial: https://www.ruanyifeng.com/blog/2019/12/github_actions.html
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
Package build & upload files to server
Scenario: After submitting code on github, it is automatically packaged and deployed to a remote server (Tencent Cloud/Alibaba Cloud, etc.)
Preliminary work:
- Make sure the content of the remote server's public key is in the
~/.ssh/authorized_keys
file - Add TOKEN under git > setting > Secrets > Action, the content is the private key content of the remote server
accomplish:
# 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"
instruction:
- The workflow is triggered when the code is submitted on the main branch
- Use node v14 to package and build, and upload the packaged product to the remote server after completion
Notice:
Here
easingthemes/ssh-deploy@main
ofSSH_PRIVATE_KEY
format requested, or will be error.Automatic release after push
Scenario: After Release, you can use jsDelivr to implement the function of free CDN. You can refer to here: Free CDN: jsDelivr+Github How to use
accomplish:
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
instruction:
- The workflow is triggered when the code is submitted on the main branch
- Every deployment is an official release
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。