Skip to content

Commit

Permalink
feat: 增加 issue 回复提示
Browse files Browse the repository at this point in the history
Co-authored-by: ygqygq2 <[email protected]>
  • Loading branch information
ygqygq2 committed Jun 28, 2024
1 parent 87ed98b commit 8744404
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 123 deletions.
257 changes: 137 additions & 120 deletions .github/workflows/docker-image-mirror.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,130 +22,147 @@ jobs:
SUCCESS: ${{ steps.successCheck.outputs.SUCCESS }}

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Get porter issues
id: pullIssuesPorter
uses: actions/github-script@v7
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
// 使用 title 获取镜像名和tag
const title = context?.payload?.issue?.title;
// 使用 body 获取其它参数
const body = context?.payload?.issue?.body || '';
const reg = new RegExp("\\[PORTER\\]", "g");
let docker_image = title.replace(reg, "").trim();
const issues_author = context?.payload?.issue?.user?.login;
// 为了防止 image 不带tag,自动添加 latest
if(!docker_image.includes(":")) {
docker_image = `${docker_image}:latest`
}
let comment_body = '';
let is_error = false;
if( docker_image.includes("@")){
is_error = true;
comment_body = '@' + issues_author +' 拉取镜像不支持带摘要信息,请去除 @部分'
}else{
comment_body = `构建进展,详见 [构建任务](https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${{github.run_id}})`
}
const issuesComment = await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: comment_body
});
console.log("create issues comment resp:", issuesComment["status"]);
if(is_error){
core.setFailed("Error");
}else if (!docker_image){
core.setFailed("No Images");
}
core.setOutput('DOCKER_IMAGE', docker_image);
core.setOutput('BUILD_ARGS', body);
- name: Retrieve transfer image name
id: transferImage
run: |
echo "${{ steps.pullIssuesPorter.outputs.DOCKER_IMAGE }}" > docker_images.list
- name: Sync image
id: syncImage
env:
DEST_HARBOR_URL: ${{ vars.DEST_HARBOR_URL }}
shell: bash
run: |
echo DEST_HARBOR_URL $DEST_HARBOR_URL
bash jenkins_sync_docker_images.sh docker_images.list
- name: Success check
id: successCheck
uses: actions/github-script@v7
if: ${{ success() }}
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
core.setOutput('SUCCESS', true);
- name: Close Porter Issues
id: closePorterIssues
uses: actions/github-script@v7
if: ${{ always() }}
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
const issuesResponse = await github.rest.issues.update({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
state: 'closed'
});
console.log("update issues resp:", issuesResponse["status"] == 200 ? "success" : "failed" );
let comment_body = `转换失败,详见 [构建任务](https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${{github.run_id}})`;
let success = String(${{ steps.successCheck.outputs.SUCCESS }}).toLowerCase() == "true";
console.log("is success?", success);
let labels = [];
if(success){
comment_body = "转换完成,请到你的 Harbor 仓库愉快使用吧 <br/>\n"
labels=['success']
}else{
const jobsResponse = await github.request(`GET /repos/${context.repo.owner}/${context.repo.repo}/actions/runs/${{github.run_id}}/jobs`, {
owner: context.repo.owner,
repo: context.repo.repo,
run_id: ${{ github.run_id }}
});
console.log("jobs",jobsResponse['data']);
comment_body += "\n\n 日志:\n\n";
for(let job of jobsResponse['data']['jobs']){
comment_body += "- [" + job.name + "](" + job.html_url +")";
- name: Checkout
uses: actions/checkout@v4

- name: Get porter issues
id: pullIssuesPorter
uses: actions/github-script@v7
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
// 使用 title 获取镜像名和tag
const title = context?.payload?.issue?.title;
// 使用 body 获取其它参数
const body = context?.payload?.issue?.body || '';
const reg = new RegExp("\\[PORTER\\]", "g");
let docker_image = title.replace(reg, "").trim();
const issues_author = context?.payload?.issue?.user?.login;
// 为了防止 image 不带tag,自动添加 latest
if(!docker_image.includes(":")) {
docker_image = `${docker_image}:latest`
}
labels = ['failure'];
}
// 创建 issues comment
const issuesComment = await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: comment_body
});
console.log("create issues comment resp:", issuesComment["status"] == 201 ? "success" : "failed" );
let comment_body = '';
let is_error = false;
// 更新 issues label
if(labels){
await github.rest.issues.addLabels({
if( docker_image.includes("@")){
is_error = true;
comment_body = '@' + issues_author +' 拉取镜像不支持带摘要信息,请去除 @部分'
}else{
comment_body = `构建进展,详见 [构建任务](https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${{github.run_id}})`
}
const issuesComment = await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: comment_body
});
console.log("create issues comment resp:", issuesComment["status"]);
if(is_error){
core.setFailed("Error");
}else if (!docker_image){
core.setFailed("No Images");
}
core.setOutput('DOCKER_IMAGE', docker_image);
core.setOutput('BUILD_ARGS', body);
- name: Retrieve transfer image name
id: transferImage
run: |
echo "${{ steps.pullIssuesPorter.outputs.DOCKER_IMAGE }}" > docker_images.list
- name: Sync image
id: syncImage
env:
DEST_HARBOR_URL: ${{ vars.DEST_HARBOR_URL }}
shell: bash
run: |
bash jenkins_sync_docker_images.sh docker_images.list
originalImageUrl="${{ steps.pullIssuesPorter.outputs.DOCKER_IMAGE }}"
# 计算原始镜像URL中 '/' 的数量
slashCount=$(grep -o "/" <<< "$originalImageUrl" | wc -l)
# 根据 '/' 的数量提取镜像名称和标签
if [ $slashCount -gt 1 ]; then
imageNameWithTag=$(echo $originalImageUrl | awk -F/ '{print $NF}')
else
imageNameWithTag=$originalImageUrl
fi
# 构建新的镜像URL
newImageUrl="$DEST_HARBOR_URL/library/$imageNameWithTag"
echo "New Image URL: $newImageUrl"
# 将新的镜像URL设置为GitHub Actions环境变量
echo "NEW_IMAGE_URL=$newImageUrl" >> $GITHUB_ENV
- name: Success check
id: successCheck
uses: actions/github-script@v7
if: ${{ success() }}
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
core.setOutput('SUCCESS', true);
- name: Close Porter Issues
id: closePorterIssues
uses: actions/github-script@v7
if: ${{ always() }}
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
const issuesResponse = await github.rest.issues.update({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
labels: labels
state: 'closed'
});
}
console.log("update issues resp:", issuesResponse["status"] == 200 ? "success" : "failed" );
let comment_body = `转换失败,详见 [构建任务](https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${{github.run_id}})`;
let success = String(${{ steps.successCheck.outputs.SUCCESS }}).toLowerCase() == "true";
console.log("is success?", success);
let labels = [];
if(success){
comment_body = "转换完成 <br/>\n```bash \n#原镜像\n${{ steps.pullIssuesPorter.outputs.DOCKER_IMAGE }}\n\n\n#转换后镜像\n${{ env.NEW_IMAGE_URL }}\n\n\n```"
labels=['success']
}else{
const jobsResponse = await github.request(`GET /repos/${context.repo.owner}/${context.repo.repo}/actions/runs/${{github.run_id}}/jobs`, {
owner: context.repo.owner,
repo: context.repo.repo,
run_id: ${{ github.run_id }}
});
console.log("jobs",jobsResponse['data']);
comment_body += "\n\n 日志:\n\n";
for(let job of jobsResponse['data']['jobs']){
comment_body += "- [" + job.name + "](" + job.html_url +")";
}
labels = ['failure'];
}
// 创建 issues comment
const issuesComment = await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: comment_body
});
console.log("create issues comment resp:", issuesComment["status"] == 201 ? "success" : "failed" );
// 更新 issues label
if(labels){
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
labels: labels
});
}
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@

## 使用 Jenkins

修改 Jenkinsfile,结合你自己的 jenkins 使用。

## 使用 Github action

请设置 action 环境变量 DEST_HARBOR_URL , secret DEST_HARBOR_CRE_USR 和 DEST_HARBOR_CRE_PSW
通过新建 Issuse 触发

>标题建议为 `[PORTER]镜像名:tag` 的格式,例如`[PORTER]k8s.gcr.io/pause:3.6`
>issues的内容设定为`skopeo copy`的参数,默认为空
> 注意:
>
> - 标题建议为 `[PORTER]镜像名:tag` 的格式,例如`[PORTER]k8s.gcr.io/pause:3.6`
> - issues 的内容设定为`skopeo copy`的参数,默认为空
其它参数可以参考:[skopeo copy](https://github.com/containers/skopeo/blob/main/docs/skopeo-copy.1.md)
2 changes: 1 addition & 1 deletion jenkins_sync_docker_images.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env bash
set -x
# set -x

cd "$(dirname "$0")" || return 1
SH_DIR=$(pwd)
Expand Down

0 comments on commit 8744404

Please sign in to comment.