自動部署:GitHub Actions
阮一峰關於 GitHub Actions 的教程: http://www.ruanyifeng.com/blog/2019/09/getting-started-with-github-actions.html
注意:因為部署插件不斷更新需要根據新的插件改相應配置
生成 token 秘鑰
官網:https://docs.github.com/en/actions/reference/encrypted-secrets
注意:github-pages-deploy-action V4 開始不需要添加 token
添加配置文件
- 在項目目錄下新建
.github\workflows
- 隨便命名一個 yml 配置文件 我的命名
main.yml
- yml文件 具體配置填寫見 文檔 或下面示例
填寫配置
示例
# 工作流名稱,不設置的話默認取配置文件名
name: Build and Deploy
# 指定觸發 workflow 的條件
# 指定觸發事件時,可以限定分支或標簽
# 當前是 只有 branches 分支上觸發 push 事件時才執行工作流任務
on:
push:
branches:
- main
# 工作流執行的一個或多個任務
jobs:
# 任務名稱
build-and-deploy:
# 任務運行的容器類型(虛擬機環境)
runs-on: ubuntu-latest
# 任務執行的步驟
steps:
# 步驟名稱
- name: Checkout ️ # 拉取代碼
# 使用的操作 actions,可以使用公共倉庫,本地倉庫,別人的倉庫的action
uses: actions/checkout@v2 # 將代碼拷貝到虛機中
# 設置 nodejs 版本
- name: SetNodeVersion
uses: actions/setup-node@v2
with:
node-version: '14'
# nodejs 版本檢查
- name: NodeCheck
run: node -v
- name: Install and Build # This example project is built using npm and outputs the result to the 'build' folder. Replace with the commands required to build your project, or remove this step entirely if your site is pre-built.
run: |
npm ci
npm run build
# 構建發布 Github pages
- name: Deploy
# 使用github-pages-deploy-action: https://github.com/JamesIves/github-pages-deploy-action/tree/master
uses: JamesIves/github-pages-deploy-action@v4.2.2
with:
# 存儲庫中要部署的文件夾。
# 該步驟會將項目中 FOLDER 指定文件夾下的文件推送到 BRANCH 分支,作為Github Pages 部署的內容。
branch: gh-pages # The branch the action should deploy to.
folder: dist # The folder the action should deploy.
關於 github-pages-deploy-action
注意:
-
JamesIves/github-pages-deploy-action@master 無法使用,繼續使用會報錯
-
github-pages-deploy-action V4 開始不需要添加 token ,如果添加會報 128 錯誤
-
github 關於 128 錯誤的解答:https://github.com/JamesIves/github-pages-deploy-action/issues/624
github-pages-deploy-action@v2
name: GitHub Actions Build and Deploy Demo
on:
push:
branches:
- master
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Build and Deploy
uses: JamesIves/github-pages-deploy-action@releases/v2
env:
ACCESS_TOKEN: ${{ secrets.ACCESS_TOKEN }}
BASE_BRANCH: master
BRANCH: gh-pages
FOLDER: public
BUILD_SCRIPT: npm install && npm run build
github-pages-deploy-action@v4
name: GitHub Actions Build and Deploy Demo
on:
push:
branches:
- master
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Install and Build
run: |
npm install
npm run build
- name: Deploy
# JamesIves/github-pages-deploy-action@4.1.1 not need token
uses: JamesIves/github-pages-deploy-action@4.1.1
with:
branch: gh-pages
folder: public
GitHub Pages
創建
https://docs.github.com/en/pages/getting-started-with-github-pages/creating-a-github-pages-site
常用 GitHub-Actions 配置
通過 ssh 命令 連接遠程服務器並部署
use :appleboy/ssh-action@master
# This is a basic workflow to help you get started with Actions
name: CICD
# 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 ]
# Allows you to run this workflow manually from the Actions tab
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"
cicd_job:
# 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
- uses: actions/checkout@v3
# ssh link remote server and executing script
- name: executing remote ssh commands using password
uses: appleboy/ssh-action@master
env:
WELCOME: "executing remote ssh commands using password"
SERVER_HOME: "node-express-server"
with:
host: ${{ secrets.DC_HOST }}
username: ${{ secrets.DC_USER }}
password: ${{ secrets.DC_PASS }}
port: 22
envs: WELCOME,SERVER_HOME
script: |
echo $WELCOME
echo whoami
whoami
echo git version
git --version
echo node version
node -v
echo npm version
npm -v
cd ~
echo ~ ls
ls -la
echo $SERVER_HOME ls
cd $SERVER_HOME
ls -la
npm run deploy
問題
使用 appleboy/ssh-action@master 有可能會報 npm 或 node 命令找不到的情況
最好在 usr/bin 建立一個node相關命令的軟連接