Skip to content

Commit

Permalink
ci: allowd deploy and preview pr
Browse files Browse the repository at this point in the history
  • Loading branch information
tea-artist committed Nov 15, 2024
1 parent 6cdc5cc commit 2f72331
Show file tree
Hide file tree
Showing 3 changed files with 729 additions and 0 deletions.
150 changes: 150 additions & 0 deletions .github/workflows/manual-preview.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
name: Preview PR

permissions:
contents: read
pull-requests: write

on:
issue_comment:
types: [created]

jobs:
check-comment:
if: |
github.event.issue.pull_request &&
contains(github.event.comment.body, 'preview')
runs-on: ubuntu-latest
steps:
- name: Get PR details
id: pr
uses: actions/github-script@v6
with:
script: |
const comment = context.payload.comment.body.trim().toLowerCase();
if (comment !== 'preview') {
return { shouldDeploy: false };
}
// 获取 PR 详情
const { data: pullRequest } = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.name,
pull_number: context.issue.number
});
return {
shouldDeploy: true,
prNumber: pullRequest.number,
headRef: pullRequest.head.ref
};
outputs:
should_deploy: ${{ fromJSON(steps.pr.outputs.result).shouldDeploy }}
pr_number: ${{ fromJSON(steps.pr.outputs.result).prNumber }}
head_ref: ${{ fromJSON(steps.pr.outputs.result).headRef }}

build-push:
needs: check-comment
if: needs.check-comment.outputs.should_deploy == 'true'
runs-on: ubuntu-latest
strategy:
matrix:
include:
- image: teable
file: Dockerfile
- image: teable-db-migrate
file: Dockerfile.migrate
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
ref: ${{ needs.check-comment.outputs.head_ref }}

- name: Login to Ali container registry
uses: docker/login-action@v3
with:
registry: registry.cn-shenzhen.aliyuncs.com
username: ${{ secrets.ALI_DOCKER_USERNAME }}
password: ${{ secrets.ALI_DOCKER_PASSWORD }}

- uses: actions/setup-node@v4
with:
node-version: 20.9.0
- name: ⚙️ Install zx
run: npm install -g zx

- name: ⚙️ Docker meta
id: meta
uses: docker/metadata-action@v5
with:
images: |
registry.cn-shenzhen.aliyuncs.com/teable/${{ matrix.image }}
tags: |
type=raw,value=alpha-pr-${{ needs.check-comment.outputs.pr_number }}
type=sha,format=long
- name: 📦 Build and push
run: |
zx scripts/build-image.mjs --file=dockers/teable/${{ matrix.file }} \
--build-arg="ENABLE_CSP=false" \
--tag="${{ steps.meta.outputs.tags }}" \
--platform="linux/amd64" \
--push
deploy:
needs: [check-comment, build-push]
if: needs.check-comment.outputs.should_deploy == 'true'
runs-on: ubuntu-latest
env:
INSTANCE_NAME: pr-${{ needs.check-comment.outputs.pr_number }}
INSTANCE_DOMAIN: pr-${{ needs.check-comment.outputs.pr_number }}
DISPLAY_NAME: "teable-pr-${{ needs.check-comment.outputs.pr_number }}"
MAIN_IMAGE_REPOSITORY: registry.cn-shenzhen.aliyuncs.com/teable/teable
DB_MIGRATE_IMAGE_REPOSITORY: registry.cn-shenzhen.aliyuncs.com/teable/teable-db-migrate
IMAGE_TAG: alpha-pr-${{ needs.check-comment.outputs.pr_number }}
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
ref: ${{ needs.check-comment.outputs.head_ref }}

- name: Create deployment YAML
run: |
cp .github/workflows/templates/preview-template.yaml deploy.yaml
sed -i "s/__INSTANCE_NAME__/${{ env.INSTANCE_NAME }}/g" deploy.yaml
sed -i "s/__INSTANCE_DOMAIN__/${{ env.INSTANCE_DOMAIN }}/g" deploy.yaml
sed -i "s/__MAIN_IMAGE_REPOSITORY__/${{ env.MAIN_IMAGE_REPOSITORY }}/g" deploy.yaml
sed -i "s/__IMAGE_TAG__/${{ env.IMAGE_TAG }}/g" deploy.yaml
sed -i "s/__DISPLAY_NAME__/${{ env.DISPLAY_NAME }}/g" deploy.yaml
- name: Apply deploy job
uses: actions-hub/kubectl@master
env:
KUBE_CONFIG: ${{ secrets.KUBE_CONFIG }}
with:
args: apply -f deploy.yaml

- name: Rollout status
uses: actions-hub/kubectl@master
env:
KUBE_CONFIG: ${{ secrets.KUBE_CONFIG }}
with:
args: rollout status deployment/teable-${{ env.INSTANCE_NAME }} --timeout=300s

- name: Create deployment status comment
if: always()
uses: actions/github-script@v6
with:
script: |
const success = ${{ job.status == 'success' }};
const deploymentUrl = 'https://${{ env.INSTANCE_DOMAIN }}.sealosgzg.site';
const status = success ? '✅ Success' : '❌ Failed';
const commentBody = `## Deployment Status: ${status}
${success ? `🔗 Preview URL: ${deploymentUrl}` : ''}`;
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.name,
issue_number: ${{ needs.check-comment.outputs.pr_number }},
body: commentBody
});
54 changes: 54 additions & 0 deletions .github/workflows/preview-cleanup.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
name: Cleanup Preview Environment

on:
pull_request:
types: [closed]

env:
INSTANCE_NAME: pr-${{ github.event.pull_request.number }}
INSTANCE_DOMAIN: pr-${{ github.event.pull_request.number }}
DISPLAY_NAME: "Teable PR #${{ github.event.pull_request.number }}"
MAIN_IMAGE_REPOSITORY: registry.cn-shenzhen.aliyuncs.com/teable/teable
DB_MIGRATE_IMAGE_REPOSITORY: registry.cn-shenzhen.aliyuncs.com/teable/teable-db-migrate
IMAGE_TAG: alpha-pr-${{ github.event.pull_request.number }}

jobs:
cleanup:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Create deployment YAML
run: |
cp .github/workflows/templates/preview-template.yaml deploy.yaml
sed -i "s/__INSTANCE_NAME__/${{ env.INSTANCE_NAME }}/g" deploy.yaml
sed -i "s/__INSTANCE_DOMAIN__/${{ env.INSTANCE_DOMAIN }}/g" deploy.yaml
sed -i "s/__MAIN_IMAGE_REPOSITORY__/${{ env.MAIN_IMAGE_REPOSITORY }}/g" deploy.yaml
sed -i "s/__IMAGE_TAG__/${{ env.IMAGE_TAG }}/g" deploy.yaml
- name: Delete deployment
uses: actions-hub/kubectl@master
env:
KUBE_CONFIG: ${{ secrets.KUBE_CONFIG }}
with:
args: delete -f deploy.yaml --ignore-not-found=true

- name: Create cleanup status comment
uses: actions/github-script@v6
with:
script: |
const prNumber = ${{ github.event.pull_request.number }};
const mergeStatus = ${{ github.event.pull_request.merged }} ? 'Merged' : 'Closed';
const commentBody = `## 🧹 Preview Environment Cleanup
* PR #${prNumber} has been ${mergeStatus}
* Preview environment has been deleted
* Cleanup time: ${new Date().toISOString()}`;
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.name,
issue_number: prNumber,
body: commentBody
});
Loading

0 comments on commit 2f72331

Please sign in to comment.