Skip to content

Commit

Permalink
add deploy to server workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
liaohui5 committed Dec 6, 2023
1 parent 15d0f1f commit 585e154
Showing 1 changed file with 98 additions and 0 deletions.
98 changes: 98 additions & 0 deletions .github/workflows/deploy-to-server.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
name: Deploy VitePress site to Server

on:
push:
tags: ["v*"] # 推送 tag(以v开头) 的时候才执行

workflow_dispatch:

permissions:
contents: read
pages: write
id-token: write

concurrency:
group: pages
cancel-in-progress: false

jobs:
#### 构建流程 ####
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0

# 安装 node 环境
- name: Setup Node
uses: actions/setup-node@v3
with:
node-version: 18
cache: npm

# 安装项目需要的依赖
- name: Install dependencies
run: npm ci

# 执行打包
- name: Build with VitePress
run: |
npm run docs:build
touch docs/.vitepress/dist/.nojekyll
# 发布 Release
- name: Create Release
id: create_release
uses: actions/create-release@master
env:
# 注意需要创建这个 Repository secrets 变量
# 值就是 Github 的 Token:
GITHUB_TOKEN: ${{ secrets.TOKEN }}
with:
tag_name: ${{ github.ref }}
release_name: Release ${{ github.ref }}
draft: false
prerelease: false

# 上传打包结果到 Release
- name: Upload Release Asset
id: upload-release-asset
uses: actions/upload-release-asset@master
env:
GITHUB_TOKEN: ${{ secrets.TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ./release.tgz
asset_name: release.tgz
asset_content_type: application/x-tgz

#### 部署到服务器流程 ####
deploy:
needs: build # 需要等待构建完成
runs-on: ubuntu-latest # 运行在一个 unbuntu 服务器上
name: Deploy
steps:
- name: Deploy to Server
id: deployment
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.SERVER_ADDR }}
username: ${{ secrets.SERVER_USER }}
password: ${{ secrets.SERVER_PASS }}
port: ${{ secrets.SERVER_PORT }}
script: |
rm -rf ~/learn-github-actions-demo # 清除上次打包缓存
mkdir -p ~/learn-github-actions-demo # 创建目录
cd ~/learn-github-actions-demo # 进入目录
# 下载打包结果
# 注意这个链接: 必须是你自己的 github 账号名字 和 代码仓库名字
wget https://github.com/lh5sa/learn-github-actions-demo/releases/latest/download/release.tgz -O release.tgz
tar -zxvf release.tgz # 解压
# 如果要运行 docker 也是同理, 直接用命令运行即可
docker run -dp 80:80 -v ~/learn-github-actions-demo:/usr/local/nginx/html --name learn-github-action-demo nginx:stable
# 如果你服务器上有 nginx, 只需要配置下 nginx 即可, 或者直接将代码移动过去, 类似这样
# mv ~/learn-github-actions-demo /usr/local/nginx/wwwroot

0 comments on commit 585e154

Please sign in to comment.