打开 https://github.com/settings/tokens ,点击右上方 Generate new token,应该要输入你的密码。之后把 Note 起一个名字,比如 github-actions-deploy,然后把 repo 的候选框选中。最后点下面的绿色按钮,跳转页面后获得一个 key,这个 key 只会出现一次,先复制下来,一会要用。
打开你项目的 Settings,点击侧栏 Secrets,点击右上角 New secrets,输入名称 ACCESS_TOKEN,这个名字可以自定义,但是之后的配置里要用到这个名字。然后把刚才复制的 key 粘贴到 value 里,保存。
name: GitHub Actions Build and Deploy
on:
push:
branches:
- master
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout 🛎️
uses: actions/checkout@v2.3.1
with:
persist-credentials: false
- name: Install and Build 🔧
run: |
yarn
yarn build
- name: Deploy 🚀
uses: JamesIves/github-pages-deploy-action@3.7.1
with:
ACCESS_TOKEN: ${{ secrets.ACCESS_TOKEN }}
BRANCH: gh-pages
FOLDER: dist
CLEAN: true
# deploy.yml
name: deploy to aliyun
on:
push:
branches:
- master
jobs:
build:
runs-on: ubuntu-latest
steps:
# 切换分支
- name: Checkout
uses: actions/checkout@master
# 使用 node
- name: Build
uses: actions/setup-node@v1
# npm install
- name: npm install and build
run: |
npm install
npm run build
env:
CI: true
# Deploy
- name: Deploy
uses: easingthemes/ssh-deploy@v2.0.7
env:
SSH_PRIVATE_KEY: ${{ secrets.ACCESS_TOKEN }} //密钥 id_rsa的内容 cat id_rsa获取
ARGS: "-avz --delete"
SOURCE: "dist/"
REMOTE_HOST: ${{ secrets.REMOTE_HOST }} //ip或者域名
REMOTE_USER: ${{ secrets.REMOTE_USER }}// root
TARGET: /home/www/next-demo //部署的服务器文件夹地址
选择一 :每次都拉取所有的代码过来。
# This is a basic workflow to help you get started with Actions
name: CI
# Controls when the action will run.
on:
# Triggers the workflow on push or pull request events but only for the main branch
push:
branches: [ main ]
pull_request:
branches: [ main ]
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2 # 第一步,下载代码仓库
- name: Install and Build
env:
TOKEN: ${{secrets.ACCESS_TOKEN}}
REF: github.com/${{github.repository}}
MYEMAIL: 15279279713@163.com
MYNAME: ${{github.repository_owner}}
run: echo 'test'
- name: server
uses: AEnterprise/rsync-deploy@v1.0 # 使用别人包装好的步骤镜像
env:
DEPLOY_KEY: ${{ secrets.DEPLOY_KEY }} # 引用配置,SSH私钥
ARGS: -avz --delete --exclude='*.pyc' # rsync参数,排除.pyc文件
SERVER_PORT: "22" # SSH端口
FOLDER: ./ # 要推送的文件夹,路径相对于代码仓库的根目录
SERVER_IP: ${{ secrets.SSH_HOST }} # 引用配置,服务器的host名(IP或者域名domain.com)
USERNAME: ${{ secrets.SSH_USERNAME }} # 引用配置,服务器登录名
SERVER_DESTINATION: /www/wwwroot/next-demo/ # 部署到目标文件夹
# 对应第6步,重启安装依赖、后端服务2
- name: Server Reload
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USERNAME }}
key: ${{ secrets.DEPLOY_KEY }}
script: |
cd /www/wwwroot/next-demo/
npm i
npm run build
选择二 下载部分的代码
# This is a basic workflow to help you get started with Actions
name: CI
# Controls when the action will run.
on:
# Triggers the workflow on push or pull request events but only for the main branch
push:
branches: [ main ]
pull_request:
branches: [ main ]
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: Server Build
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USERNAME }}
key: ${{ secrets.DEPLOY_KEY }}
script: |
cd /www/wwwroot/
git clone git@github.com:gujiwuqing/next-demo.git
cd next-demo
npm i
npm run build
这个时候需要第一次克隆全部的代码 ,然后去用pm2手动部署项目 然后再修改, 然后再pull代码并且自动打包重启项目就行
# This is a basic workflow to help you get started with Actions
name: CI
# Controls when the action will run.
on:
# Triggers the workflow on push or pull request events but only for the main branch
push:
branches: [ main ]
pull_request:
branches: [ main ]
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: Server Build
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USERNAME }}
key: ${{ secrets.DEPLOY_KEY }}
script: |
cd /www/wwwroot/next-demo
git pull origin main
npm i
npm run build
npm restart 项目名或者pm2 restart id