Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: #836 PR合并后自动清除PR在线预览相关资源 #842

Merged
merged 1 commit into from
Jul 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions .github/workflows/pr-merge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
name: PR Closed

on:
pull_request_target:
types:
- closed

jobs:
remove_assets:
runs-on: ubuntu-latest

steps:
# 检出仓库代码
- name: Checkout repository
uses: actions/checkout@v2

# 打印 PR 详细信息
- name: Print PR details
run: |
echo "The PR ID is ${{ github.event.pull_request.id }}"
echo "The PR number is ${{ github.event.pull_request.number }}"
echo "The PR title is ${{ github.event.pull_request.title }}"
echo "The PR branch is ${{ github.event.pull_request.head.ref }}"

# 安装 cos-nodejs-sdk-v5
- name: Install cos-nodejs-sdk-v5
run: npm install cos-nodejs-sdk-v5

# 删除对应的资源
- name: Delete Resources On COS
uses: actions/github-script@v7
with:
script: |
const COS = require('cos-nodejs-sdk-v5');

const cos = new COS({
SecretId: process.env.COS_SECRETID,
SecretKey: process.env.COS_SECRETKEY,
});

const bucket = 'cherrymd-1301618266';
const region = 'ap-singapore';
const prNumber = '${{ github.event.pull_request.number }}';
const prefix = `pr${prNumber}/`;

// List objects in the bucket with the specified prefix
cos.getBucket({
Bucket: bucket,
Region: region,
Prefix: prefix,
}, (err, data) => {
if (err) {
console.error('Error listing objects:', err);
return;
}

const objectsToDelete = data.Contents.map(item => ({ Key: item.Key }));

if (objectsToDelete.length === 0) {
console.log('No objects to delete.');
return;
}

// Delete the listed objects
cos.deleteMultipleObject({
Bucket: bucket,
Region: region,
Objects: objectsToDelete,
}, (err, data) => {
if (err) {
console.error('Error deleting objects:', err);
} else {
console.log('Successfully deleted objects:', data);
}
});
});
env:
COS_SECRETID: ${{ secrets.COS_SECRETID }}
COS_SECRETKEY: ${{ secrets.COS_SECRETKEY }}
Loading