136 lines
5.7 KiB
YAML
136 lines
5.7 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 }}
|
|
|
|
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
|
|
|
|
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"]
|
|
}
|
|
console.log("schedule trigger")
|
|
}
|
|
if(issue_number>0){
|
|
let start = 0
|
|
if (title.includes("[PORTER]")){
|
|
start = 8
|
|
}
|
|
gcr_image = title.substring(start).trim()
|
|
|
|
const issuesComment = await github.issues.createComment({
|
|
owner: '${{ env.GH_USER }}',
|
|
repo: '${{ env.REPO_NAME }}',
|
|
issue_number: issue_number,
|
|
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 }})'
|
|
});
|
|
console.log("create issues comment resp:",issuesComment["status"]);
|
|
console.log("gcr_image from issues is ", gcr_image,", issue_number is ",issue_number)
|
|
}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 tag ${{ steps.pullIssuesPorter.outputs.GCR_IMAGE }} ${{ steps.transferImage.outputs.MY_DOCKER_IMAGE_NAME }}
|
|
docker push ${{ steps.transferImage.outputs.MY_DOCKER_IMAGE_NAME }}
|
|
|
|
- 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 }})"
|
|
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```"
|
|
}
|
|
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" )
|
|
}
|