GitHub Actions 是什么?
GitHub Actions 的官方定义是:Makes it easy to automate all your software workflows, now with world-class CI/CD.
所以可以理解为一个 GitHub 集成的 CI/CD 工具。
P.S. 本视频中使用的 GitHub Action Marketplace 中的插件实测无法使用,已做替换,详见下方 yaml 代码
如何收费
GitHub Actions 为用户提供了 2000 分钟的免费额度:
以我个人的经验来看,一次运行的时长约在 1-4 mins 这个范围,所以只要不是一天跑 20 次以上仅仅免费额度是足够了的。
实操
首先我们准备好一个已经启动好 Nginx 服务的环境,如果可以推荐使用 Docker 快捷部署 Nginx,参考文章:https://www.ruanyifeng.com/blog/2018/02/nginx-docker.html。
此外,我们的服务器还需要是公网可访问的,这有很多方法,例如内网穿透、申请公网 ip 后的 DDNS……这是必须条件,要不然 GitHub Actions 启动的虚机无法将文件传到你这。
准备好上述环境后,我们会有一个 http://<host>:<port>
的链接,可以直接访问 Nginx 的欢迎页。
我们编辑 GitHub Actions 的 workflow 的思路就是,每次 push 代码至 master 分支,自动触发传送代码至服务器中 Nginx 映射的文件夹。
所以进入你的 repo,在 Actions 中找到 New workflow
:
我的 workflow
配置文件为:
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
# This is a basic workflow to help you get started with Actions
name: DeployToNas
# Controls when the workflow will run
on:
# Triggers the workflow on push or pull request events but only for the master branch
push:
branches: [ master ]
workflow_dispatch:
# 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
timeout-minutes: 5
# 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
- uses: actions/checkout@v3
- name: another scp
uses: cross-the-world/ssh-scp-ssh-pipelines@v1.1.4
with:
# ssh remote host
host: $
# ssh remote port
port: $ # optional, default is 22
# ssh remote user
user: $
# ssh remote password
pass: $ # optional
connect_timeout: 10s
scp: |
./<想传输的目录>/ => /<远端服务器目录>/
注意,这里出现了很多类似 $
这种环境变量,这是为了防止一些私密信息明文写出不安全,在 Setting-Secrets
中可以设置:
设置好后提交代码即可触发自动化流程,并且可以看到执行的历史记录:
Tips
设置时长限制
https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idtimeout-minutes