215 lines
9.2 KiB
YAML
215 lines
9.2 KiB
YAML
name: gcr.io_mirror
|
||
|
||
on:
|
||
schedule:
|
||
- cron: "0 0 * * *"
|
||
issues:
|
||
types: [opened]
|
||
label:
|
||
types: [created]
|
||
workflow_dispatch:
|
||
|
||
env:
|
||
REPO_NAME: ${{ github.event.repository.name }}
|
||
GH_USER: anjia0532
|
||
|
||
jobs:
|
||
build:
|
||
runs-on: ubuntu-latest
|
||
|
||
outputs:
|
||
GCR_IMAGE: ${{ steps.pullIssuesPorter.outputs.GCR_IMAGE }}
|
||
# ISSUE_NUMBER: ${{ steps.pullIssuesPorter.outputs.ISSUE_NUMBER }}
|
||
# MY_DOCKER_IMAGE_NAME: ${{ steps.transferImage.outputs.MY_DOCKER_IMAGE_NAME }}
|
||
SUCCESS: ${{ steps.successCheck.outputs.SUCCESS }}
|
||
|
||
steps:
|
||
- name: Log into docker hub
|
||
uses: docker/login-action@28218f9b04b4f3f62068d7b6ce6ca5b26e35336c
|
||
with:
|
||
username: ${{ secrets.DOCKER_HUB_USERNAME }}
|
||
password: ${{ secrets.DOCKER_HUB_PASSWORD }}
|
||
|
||
- name: get porter issues
|
||
id: pullIssuesPorter
|
||
uses: actions/github-script@v3.1.0
|
||
with:
|
||
github-token: ${{secrets.GITHUB_TOKEN}}
|
||
script: |
|
||
const fs = require('fs')
|
||
|
||
let gcr_image
|
||
let title
|
||
let issues_author
|
||
|
||
const ev = JSON.parse(
|
||
fs.readFileSync(process.env.GITHUB_EVENT_PATH, 'utf8')
|
||
)
|
||
|
||
let issue_number = (ev.issues || {'number': -1})['number']
|
||
|
||
if(issue_number>0){
|
||
const issuesResponse = await github.issues.get({
|
||
owner: '${{ env.GH_USER }}',
|
||
repo: '${{ env.REPO_NAME }}',
|
||
issue_number: issue_number
|
||
})
|
||
title = issuesResponse.title
|
||
console.log('issues opened trigger')
|
||
}else{
|
||
|
||
const issuesResponse = await github.issues.listForRepo({
|
||
owner: '${{ env.GH_USER }}',
|
||
repo: '${{ env.REPO_NAME }}',
|
||
state: "open",
|
||
labels: "porter",
|
||
sort: "created",
|
||
direction: "desc",
|
||
per_page: 1
|
||
})
|
||
|
||
if (Array.isArray(issuesResponse["data"]) && issuesResponse["data"].length) {
|
||
title = issuesResponse["data"][0]["title"]
|
||
issue_number = issuesResponse["data"][0]["number"]
|
||
issues_author = issuesResponse["data"][0]["user"]["login"]
|
||
}
|
||
console.log("schedule trigger")
|
||
}
|
||
if(issue_number>0){
|
||
let start = 0
|
||
if (title.includes("[PORTER]")){
|
||
start = 8
|
||
}
|
||
gcr_image = title.substring(start).trim()
|
||
issues_body=''
|
||
is_error=false
|
||
if( gcr_image.includes("@")){
|
||
// 不支持带摘要 k8s.gcr.io/ingress-nginx/kube-webhook-certgen:v1.1.1@sha256:64d8c73dca984af206adf9d6d7e46aa550362b1d7a01f3a0a91b20cc67868660
|
||
is_error=true
|
||
issues_body='@'+issues_author+' 拉取镜像不支持带摘要信息,请去除 @部分'
|
||
}else if( !gcr_image.includes("gcr.io")){
|
||
// 只支持 k8s.gcr.io 和 gcr.io
|
||
is_error=true
|
||
issues_body='@'+issues_author+' 不是说了么,只支持 k8s.gcr.io 和 gcr.io,其他源请自己想办法'
|
||
}else{
|
||
issues_body='构建进展 [https://github.com/${{ env.GH_USER }}/${{ env.REPO_NAME }}/actions/runs/${{ github.run_id }}](https://github.com/${{ env.GH_USER }}/${{ env.REPO_NAME }}/actions/runs/${{ github.run_id }})'
|
||
}
|
||
const issuesComment = await github.issues.createComment({
|
||
owner: '${{ env.GH_USER }}',
|
||
repo: '${{ env.REPO_NAME }}',
|
||
issue_number: issue_number,
|
||
body: issues_body
|
||
});
|
||
console.log("create issues comment resp:", issuesComment["status"]);
|
||
console.log("gcr_image from issues is ", gcr_image,", issue_number is ",issue_number, ",issues_author is ", issues_author)
|
||
if(is_error){
|
||
core.setFailed("Error");
|
||
}
|
||
}else{
|
||
core.setFailed("No Images");
|
||
}
|
||
core.setOutput('GCR_IMAGE', gcr_image)
|
||
core.setOutput('ISSUE_NUMBER', issue_number)
|
||
|
||
- name: Retrieve transfer image name
|
||
run: |
|
||
echo "::set-output name=MY_DOCKER_IMAGE_NAME::$(echo ${{ steps.pullIssuesPorter.outputs.GCR_IMAGE }} | sed 's/k8s\.gcr\.io/${{ secrets.DOCKER_HUB_USERNAME }}\/google-containers/g;s/gcr\.io/${{ secrets.DOCKER_HUB_USERNAME }}/g;s/\//\./g;s/ /\n/g;s/${{ secrets.DOCKER_HUB_USERNAME }}\./${{ secrets.DOCKER_HUB_USERNAME }}\//g')"
|
||
id: transferImage
|
||
|
||
- name: pull from gcr.io and push to docker hub
|
||
shell: bash
|
||
run: |
|
||
docker pull ${{ steps.pullIssuesPorter.outputs.GCR_IMAGE }}
|
||
echo ${{ steps.transferImage.outputs.MY_DOCKER_IMAGE_NAME }}
|
||
docker images
|
||
docker tag ${{ steps.pullIssuesPorter.outputs.GCR_IMAGE }} ${{ steps.transferImage.outputs.MY_DOCKER_IMAGE_NAME }}
|
||
docker push ${{ steps.transferImage.outputs.MY_DOCKER_IMAGE_NAME }}
|
||
|
||
- name: success check
|
||
id: successCheck
|
||
uses: actions/github-script@v3.1.0
|
||
if: ${{ success() }}
|
||
with:
|
||
github-token: ${{secrets.GITHUB_TOKEN}}
|
||
script: |
|
||
core.setOutput('SUCCESS', true)
|
||
|
||
- name: Close Porter Issues
|
||
id: closePorterIssues
|
||
uses: actions/github-script@v3.1.0
|
||
if: ${{ always() }}
|
||
with:
|
||
github-token: ${{secrets.GITHUB_TOKEN}}
|
||
script: |
|
||
if (${{ steps.pullIssuesPorter.outputs.ISSUE_NUMBER }} > 0){
|
||
const issuesResponse = await github.issues.update({
|
||
owner: '${{ env.GH_USER }}',
|
||
repo: '${{ env.REPO_NAME }}',
|
||
issue_number: ${{ steps.pullIssuesPorter.outputs.ISSUE_NUMBER }},
|
||
state: 'closed'
|
||
})
|
||
console.log("update issues resp:", issuesResponse["status"] == 200 ? "success" : "failed" )
|
||
let body = "转换失败,详见 [构建任务](https://github.com/${{ env.GH_USER }}/${{ env.REPO_NAME }}/actions/runs/${{ github.run_id }})"
|
||
let success = String(${{ steps.successCheck.outputs.SUCCESS }}).toLowerCase() == "true"
|
||
console.log("is success?", success)
|
||
let labels = []
|
||
if(success){
|
||
body = "转换完成 <br/>\n```bash \n#原镜像\n${{ steps.pullIssuesPorter.outputs.GCR_IMAGE }}\n\n\n#转换后镜像\n${{ steps.transferImage.outputs.MY_DOCKER_IMAGE_NAME }}\n\n\n#下载并重命名镜像\ndocker pull ${{ steps.transferImage.outputs.MY_DOCKER_IMAGE_NAME }}\ndocker tag ${{ steps.transferImage.outputs.MY_DOCKER_IMAGE_NAME }} ${{ steps.pullIssuesPorter.outputs.GCR_IMAGE }}\ndocker images | grep $(echo ${{ steps.pullIssuesPorter.outputs.GCR_IMAGE }}|awk -F':' '{print $1}')\n\n\n# 也可以用我写的脚本\nwget https://raw.githubusercontent.com/anjia0532/gcr.io_mirror/master/pull-k8s-image.sh && chmod +x pull-k8s-image.sh\n./pull-k8s-image.sh ${{ steps.pullIssuesPorter.outputs.GCR_IMAGE }}\n```"
|
||
labels=['success']
|
||
}else{
|
||
const jobsResponse = await github.actions.listJobsForWorkflowRun({
|
||
owner: '${{ env.GH_USER }}',
|
||
repo: '${{ env.REPO_NAME }}',
|
||
run_id: ${{ github.run_id }}
|
||
})
|
||
console.log("jobs",jobsResponse['data'])
|
||
body+="\n\n 日志:\n\n"
|
||
for(let job of jobsResponse['data']['jobs']){
|
||
body+="- ["+job.name+"]("+job.html_url+")"
|
||
}
|
||
labels=['failed']
|
||
}
|
||
|
||
let gcrImg = "${{ steps.pullIssuesPorter.outputs.GCR_IMAGE }}"
|
||
|
||
let colonIndex = gcrImg.indexOf(":")
|
||
|
||
|
||
if (colonIndex > 0) {
|
||
gcrImg = gcrImg.substr(0,colonIndex)
|
||
}
|
||
|
||
let names = gcrImg.split("/")
|
||
let registry = names[0]
|
||
names=names.splice(1,5)
|
||
|
||
if("k8s.gcr.io" == registry){
|
||
body+="\n\n\n k8s.gcr.io 镜像标签 Api(需梯子) <https://k8s.gcr.io/v2/"+names.join("/")+"/tags/list>\n\n k8s.gcr.io 镜像标签 UI(需梯子) <https://console.cloud.google.com/gcr/images/k8s-artifacts-prod/us/"+names.join("/")+">\n\n"
|
||
}else{
|
||
let imgNames = names.splice(1,5)
|
||
let uri=names[0]+"/global/"+imgNames.join("/")
|
||
|
||
console.log(imgNames,uri)
|
||
if(names.length==1){
|
||
uri="global/"+names[0]
|
||
}
|
||
body+="\n\n\n gcr.io 镜像标签 Api(需梯子) <https://gcr.io/v2/"+names.join("/")+"/tags/list>\n\n gcr.io 镜像标签 UI(需梯子) <https://console.cloud.google.com/gcr/images/"+uri+">\n\n"
|
||
}
|
||
|
||
const issuesComment = await github.issues.createComment({
|
||
owner: '${{ env.GH_USER }}',
|
||
repo: '${{ env.REPO_NAME }}',
|
||
issue_number: ${{ steps.pullIssuesPorter.outputs.ISSUE_NUMBER }},
|
||
body: body
|
||
});
|
||
console.log("create issues comment resp:", issuesComment["status"] == 201 ? "success" : "failed" )
|
||
if(labels){
|
||
await github.issues.addLabels({
|
||
owner: '${{ env.GH_USER }}',
|
||
repo: '${{ env.REPO_NAME }}',
|
||
issue_number: ${{ steps.pullIssuesPorter.outputs.ISSUE_NUMBER }},
|
||
labels: labels
|
||
});
|
||
}
|
||
}
|