GithubAction

Introduction

Automate, customize, and execute your software development workflows right in your repository with GitHub Actions. You can discover, create, and share actions to perform any job you’d like, including CI/CD, and combine actions in a completely customized workflow.

Quick Start

在根目录下创建目录.github/workflows

Create action

创建文件:action.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
name: GitHub Actions Demo
on: [push]
jobs:
Explore-GitHub-Actions:
runs-on: ubuntu-latest
steps:
- run: echo "🎉 The job was automatically triggered by a ${{ github.event_name }} event."
- run: echo "🐧 This job is now running on a ${{ runner.os }} server hosted by GitHub!"
- run: echo "🔎 The name of your branch is ${{ github.ref }} and your repository is ${{ github.repository }}."
- name: Check out repository code
uses: actions/checkout@v2
- run: echo "💡 The ${{ github.repository }} repository has been cloned to the runner."
- run: echo "🖥️ The workflow is now ready to test your code on the runner."
- name: List files in the repository
run: |
ls ${{ github.workspace }}
- run: echo "🍏 This job's status is ${{ job.status }}."

Param Info

action参数介绍

1
2
3
4
5
6
7
8
name:Action名称
on:当指定推送到制定分支,触发aciton
jobs:工作流程
Explore-GitHub-Actions:工作名称
runs-on:运行在镜像中
steps:步骤
run:在镜像中运行的命令
uses:使用包装好的Action

Example

下面是我当前Blog的action配置信息,注释比较详细

主要参数

使用别人已创建好的Action:easingthemes/[email protected]

SERVER_SSH_KEY:服务器连接生成的SSH

REMOTE_PORT:服务器SSH端口

REMOTE_HOST:服务器ip地址

REMOTE_USER:登录用户名

REMOTE_TARGET:推送的指定目录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# 工作流名称
name: BlogDeploy

env:
# 镜像时区
TZ: Asia/Shanghai

# 总开关
on:
push:
branches:
# 当master提交时启动
- master
paths-ignore:
# 以下文件更改是,不触发部署
- README.md

# 开始任务流程
jobs:
# 工作名称
deploy:
# 调用编译镜像
runs-on: ubuntu-latest
# 开始步骤,一个name分为一步
steps:
# 使用actions第二个版本
- uses: actions/checkout@v2
# 在镜像中安装Nodejs
- name: Instlal Node.js
uses: actions/setup-node@v2
# 指定node版本
with:
node-version: "14"
# 安装依赖并编译
- name: Install and build
run: |
yarn
yarn build

# 远程推送服务器
- name: ssh deploy
# 使用Actions
uses: easingthemes/[email protected]
env:
# 服务器ssh的key
SSH_PRIVATE_KEY: ${{ secrets.SERVER_SSH_KEY }}
ARGS: "-rltgoDzvO --delete"
# 指定当前镜像目录
SOURCE: "public/"
# 服务器端口
REMOTE_PORT: ${{ secrets.REMOTE_PORT }}
# 服务器主机
REMOTE_HOST: ${{ secrets.REMOTE_HOST }}
# 服务器用户
REMOTE_USER: ${{ secrets.REMOTE_USER }}
# 推送到服务器指定目录
TARGET: ${{ secrets.REMOTE_TARGET }}
# EXCLUDE: "/public/, /node_modules/"

image-20220313093052893

image-20220313093120346

参考来源

GithubActions:https://docs.github.com/en/actions/quickstart