ci(workflows): support manual depoly and stop pr (#1132)

* ci(workflows): support manual depoly and stop pr

* ci(workflows): support auto depoly pr and stop pr

* ci(workflows): depoly pr script moves to server side

* ci(workflows): support pr sent text msg to feishu bot

* ci(workflows): fix auto deploy pr script

* ci(workflows): support auto deploy main and develop branch
This commit is contained in:
lyf-coder 2022-11-30 16:10:12 +08:00 committed by GitHub
parent b16a764f19
commit c94f29a8ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 197 additions and 0 deletions

View File

@ -60,3 +60,43 @@ jobs:
COMMIT_HASH=${GITHUB_SHA}
push: true
tags: ${{ secrets.ALI_DOCKER_REGISTRY }}/${{ steps.meta.outputs.tags }}
- name: Save PR number
env:
PR_NUMBER: ${{ github.event.number }}
run: |
mkdir -p ./pr
echo $PR_NUMBER > ./pr/pr_number
- uses: actions/upload-artifact@v3
with:
name: pr_number
path: pr/
- name: Save image tag
env:
IMAGE_TAG: ${{ steps.meta.outputs.tags }}
run: |
mkdir -p ./image
echo $IMAGE_TAG > ./image/image_tag
- uses: actions/upload-artifact@v3
with:
name: image_tag
path: image/
- name: text message
if: github.event_name == 'pull_request'
uses: foxundermoon/feishu-action@v2
with:
url: ${{ secrets.PR_FEISHU_BOT_WEBHOOK_URL }}
msg_type: text
content: |
text: |
地址:${{ github.event.pull_request.html_url }}
标题:${{ github.event.pull_request.title }}
内容:${{ github.event.pull_request.body }}
分支:${{ github.event.pull_request.head.ref }}
触发者:${{ github.triggering_actor }}
----------------------------------------------
正在自动部署环境-稍后访问地址http://${{ secrets.deploy_host }}:1${{ github.event.number }}
-----------------------------------------------
请及时更新任务系统的任务状态https://tasks.nocobase.com/

View File

@ -0,0 +1,69 @@
name: Auto Deploy Main
# deploy_host
# deploy_host_username
# deploy_host_password
# https://docs.github.com/cn/actions/using-workflows/events-that-trigger-workflows#workflow_run
env:
nocobase_main_deploy_path: /home/ecs-user/nocobase/main
nocobase_develop_deploy_path: /home/ecs-user/nocobase/develop
on:
workflow_run:
workflows: [ "Aliyun Container Registry" ]
types:
- "completed"
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: 'Download image tag artifact'
uses: actions/github-script@v6
with:
script: |
let allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: context.payload.workflow_run.id,
});
let matchArtifact = allArtifacts.data.artifacts.filter((artifact) => {
return artifact.name == "image_tag"
})[0];
let download = await github.rest.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: matchArtifact.id,
archive_format: 'zip',
});
let fs = require('fs');
fs.writeFileSync(`${process.env.GITHUB_WORKSPACE}/image_tag.zip`, Buffer.from(download.data));
- name: 'Unzip image tag artifact'
run: unzip image_tag.zip
- name: 'set image_tag to env'
run: |
image_tag=`cat ./image_tag`
echo "image_tag=$image_tag" >> $GITHUB_ENV
- name: ssh deploy main
if: env.image_tag == 'nocobase/nocobase:main'
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.deploy_host }}
username: ${{ secrets.deploy_host_username }}
password: ${{ secrets.deploy_host_password }}
script: |
cd ${{ env.nocobase_main_deploy_path }}
sudo docker compose pull && sudo docker compose up -d
- name: ssh deploy develop
if: env.image_tag == 'nocobase/nocobase:develop'
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.deploy_host }}
username: ${{ secrets.deploy_host_username }}
password: ${{ secrets.deploy_host_password }}
script: |
cd ${{ env.nocobase_develop_deploy_path }}
sudo docker compose pull && sudo docker compose up -d

56
.github/workflows/auto_deploy_pr.yml vendored Normal file
View File

@ -0,0 +1,56 @@
name: Auto Deploy Pr
# deploy_host
# deploy_host_username
# deploy_host_password
# https://docs.github.com/cn/actions/using-workflows/events-that-trigger-workflows#workflow_run
env:
nocobase_deploy_sh: /home/ecs-user/nocobase/auto-deploy-pr/nocobase-deploy.sh
on:
workflow_run:
workflows: [ "Aliyun Container Registry" ]
types:
- "completed"
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: 'Download pr number artifact'
uses: actions/github-script@v6
with:
script: |
let allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: context.payload.workflow_run.id,
});
let matchArtifact = allArtifacts.data.artifacts.filter((artifact) => {
return artifact.name == "pr_number"
})[0];
let download = await github.rest.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: matchArtifact.id,
archive_format: 'zip',
});
let fs = require('fs');
fs.writeFileSync(`${process.env.GITHUB_WORKSPACE}/pr_number.zip`, Buffer.from(download.data));
- name: 'Unzip pr number artifact'
run: unzip pr_number.zip
- name: 'set pr_number to env'
run: |
pr_number=`cat ./pr_number`
echo "pr_number=$pr_number" >> $GITHUB_ENV
- name: ssh deploy
if: ${{ env.pr_number }}
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.deploy_host }}
username: ${{ secrets.deploy_host_username }}
password: ${{ secrets.deploy_host_password }}
script: |
${{ env.nocobase_deploy_sh }} up ${{ env.pr_number }}

View File

@ -0,0 +1,32 @@
name: Auto down deploy
# deploy_host
# deploy_host_username
# deploy_host_password
# https://docs.github.com/cn/actions/using-workflows/events-that-trigger-workflows#workflow_run
env:
nocobase_deploy_sh: /home/ecs-user/nocobase/auto-deploy-pr/nocobase-deploy.sh
on:
pull_request:
types:
- closed
branches:
- '**'
paths:
- 'packages/**'
jobs:
down_deploy:
runs-on: ubuntu-latest
steps:
- name: ssh down deploy
if: github.event.pull_request.merged == true
uses: appleboy/ssh-action@master
env:
pr_number: ${{ github.event.number }}
with:
host: ${{ secrets.deploy_host }}
username: ${{ secrets.deploy_host_username }}
password: ${{ secrets.deploy_host_password }}
script: |
${{ env.nocobase_deploy_sh }} down ${{ env.pr_number }}