Skip to content

Commit

Permalink
github action
Browse files Browse the repository at this point in the history
Signed-off-by: weizhoublue <weizhou.lan@daocloud.io>
  • Loading branch information
weizhoublue committed Dec 25, 2024
1 parent f5d41c5 commit cdbef1f
Show file tree
Hide file tree
Showing 318 changed files with 204,505 additions and 36 deletions.
217 changes: 217 additions & 0 deletions .github/workflows/auto-cherrypick.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
name: Auto CherryPick PR

permissions: write-all

env:
PR_LABEL_PREFIX_CHERRYPICK: "cherrypick-"
CHERRYPICK_LABEL: "robot-cherrypick"
DEFAULT_REVIEWER: "weizhoublue"

on:
push:
branches:
- 'release-*'
- 'main'
workflow_dispatch:
inputs:
prNumber:
description: 'pr number'
required: true
destBranch:
description: 'dest branch, if empty, follow the pr label'
required: false

jobs:
cherry_pick:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0

- uses: crazy-max/ghaction-import-gpg@v6
with:
gpg_private_key: ${{ secrets.GPG_PRIVATE_KEY }}
passphrase: ${{ secrets.GPG_PASSPHRASE }}
git_user_signingkey: true
git_commit_gpgsign: true

- name: cherry pick
env:
GITHUB_TOKEN: ${{ secrets.WELAN_PAT }}
JSON: ${{ toJSON(github) }}
run: |
set -x
echo "============ print var ======== "
if ${{ github.event_name == 'workflow_dispatch' }}; then
PR_NUMBER=${{ github.event.inputs.prNumber }}
else
grep -Eio "Merge pull request #[0-9]+ " <<< "${JSON}" || true
echo "try to get PR from commit"
COMMIT=` echo "${JSON}" | jq '.event.commits[0].id' | tr -d '"' ` || true
if [ -n "${COMMIT}" ]; then
PR_NUMBER=`curl --retry 10 -s -H "Accept: application/vnd.github.groot-preview+json" https://api.github.com/repos/${{ github.repository }}/commits/${COMMIT}/pulls | jq -r '.[].number' `
else
echo "error, failed to get any commit ID"
fi
if [ -z "${PR_NUMBER}" ] ; then
PR_NUMBER=` grep -Eio "Merge pull request #[0-9]+ " <<< "${JSON}" | grep -Eo "[0-9]+" | uniq ` || true
fi
if [ -z "${PR_NUMBER}" ] ; then
echo "error, failed to get PR NUMBER"
exit 1
fi
# grep -Ei "https://github.com/.*/commit" <<< "${JSON}"
#PR_COMMITS=` grep -Ei "https://github.com/.*/commit" <<< "${JSON}" | awk -F'"' '{print $4}' | uniq -c | awk '{ if ($1 == 1 ) print $2}' | awk -F'/' '{print $NF}' | tr '\n' ' ' `
# grep '"username":' <<< "${JSON}"
#PR_AUTHOR=` grep '"username":' <<< "${JSON}" | awk -F'"' '{print $4}' | sort | uniq | grep -v "web-flow" | head -1 | tr -d '\n' `
# [ -n "${PR_AUTHOR}" ] || { echo "no PR_AUTHOR, ignore" ; }
#
fi
RP_API_BODY=` curl --retry 10 https://api.github.com/repos/${{ github.repository }}/pulls/${PR_NUMBER} -s -H "Accept: application/vnd.github.groot-preview+json" `
PR_COMMITS=` echo "${RP_API_BODY}" | jq ' .merge_commit_sha ' | tr -d '"' `
PR_AUTHOR=` echo "${RP_API_BODY}" | jq ' .user.login ' | tr -d '"' `
PR_URL="https://github.com/${{ github.repository }}/pull/${PR_NUMBER}"
ACTION_URL="${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
#gh pr view ${PR_NUMBER} || { echo "failed to get pr ${PR_NUMBER}" ; exit 1 ; }
#PR_LABEL=` gh pr view ${PR_NUMBER} | grep -i "^labels:" | tr ',' ' ' | tr -s ' ' | sed 's/labels://g' `
# [ -n "${PR_LABEL}" ] || { echo "no PR_LABEL, ignore" ; }
PR_LABEL=` echo "${RP_API_BODY}" | jq ' .labels[].name ' | tr -d '"' | tr '\n' ' ' `
#PR_TITLE=`gh pr view ${PR_NUMBER} | sed -n '1 p' `
# [ -n "${PR_TITLE}" ] || { echo "error, no PR_TITLE " ; exit 1 ; }
PR_TITLE=` echo "${RP_API_BODY}" | jq ' .title ' | tr -d '"' `
#
if [ -z "${PR_COMMITS}" ]; then
echo "error, failed to get pr commits"
fi
echo "number: ${PR_NUMBER}"
echo "action url: ${ACTION_URL}"
echo "PR_COMMITS: ${PR_COMMITS}"
echo "author: ${PR_AUTHOR}"
echo "url: ${PR_URL}"
echo "PR_LABEL: ${PR_LABEL}"
echo "PR_TITLE: ${PR_TITLE}"
#
#
echo "=============================== get dest branch from labels ======== "
WANT_MERGE_BRANCH_LIST=""
INITIAL_LABEL=""
if ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.destBranch != '' }}; then
WANT_MERGE_BRANCH_LIST=${{ github.event.inputs.destBranch }}
else
for LABEL in ${PR_LABEL} ; do
echo "checking label $LABEL "
PREFIX="${{ env.PR_LABEL_PREFIX_CHERRYPICK }}"
if ! grep -E "^${PREFIX}" <<< "${LABEL}" &>/dev/null; then
INITIAL_LABEL+="${LABEL},"
continue
fi
BRANCH_NAME=` sed 's?'"${PREFIX}"'??' <<< "$LABEL" `
WANT_MERGE_BRANCH_LIST+=" $BRANCH_NAME "
done
fi
[ -z "$WANT_MERGE_BRANCH_LIST" ] && echo "no branch to cherry pick" && exit 0
echo "cherry pick to $WANT_MERGE_BRANCH_LIST "
INITIAL_LABEL="${INITIAL_LABEL%,}"
echo "INITIAL_LABEL: ${INITIAL_LABEL}"
#
#
echo "============ begin to cherry pick ============ "
FINAL_FAILURE=false
git branch
git config user.email "tao.yang@daocloud.io"
git config user.name "robot"
gh label create ${{ env.CHERRYPICK_LABEL }} || true
for BRANCH in $WANT_MERGE_BRANCH_LIST ; do
echo "************** cherry for branch ${BRANCH}"
if ! git ls-remote --exit-code --heads origin ${BRANCH} ; then
# todo: create issue
echo "error, branch $BRANCH does not exist"
gh issue create \
--body "reason: the branch $BRANCH does not exist. PR <${PR_URL}> , action <${ACTION_URL}> " \
--title "failed to auto cherry pick PR ${PR_NUMBER} to branch ${BRANCH}" \
--label "${{ env.CHERRYPICK_LABEL }}" \
--assignee "${PR_AUTHOR},${{ env.DEFAULT_REVIEWER }}"
if (($?!=0)) ; then
echo "!!!! error, failed to create issue"
FINAL_FAILURE=true
fi
continue
fi
git fetch origin ${BRANCH}:${BRANCH} || true
if ! git checkout ${BRANCH} ; then
echo "error, failed to checkout to branch $BRANCH"
gh issue create \
--body "reason: failed to get the branch $BRANCH. PR <${PR_URL}> , action <${ACTION_URL}> " \
--title "failed to auto cherry pick PR ${PR_NUMBER} to branch ${BRANCH}" \
--label "${{ env.CHERRYPICK_LABEL }}" \
--assignee "${PR_AUTHOR},${{ env.DEFAULT_REVIEWER }}"
if (($?!=0)) ; then
echo "!!!! error, failed to create issue"
FINAL_FAILURE=true
fi
continue
fi
PR_BRANCH=robot/cherrypick/pr${PR_NUMBER}/${BRANCH}
git checkout -b ${PR_BRANCH}
git branch --show-current
FAIL=false
UPDATE=false
ERROR_MESSAGE=""
for COMMIT in $PR_COMMITS; do
if ! ERROR_MESSAGE=`git cherry-pick -m 1 $COMMIT 2>&1` ; then
echo ">>>>>>>>>>>> fail when cherry pick $COMMIT to branch $BRANCH "
echo "$ERROR_MESSAGE"
echo "---- failuire detail"
git status
git diff
echo "<<<<<<<<<<<<<"
if git diff --exit-code --quiet ; then
echo "no changes happen, ignore cherry pick $COMMIT "
git cherry-pick --abort || true
git reset --hard HEAD || true
continue
else
FAIL=true
echo "error, failed to cherry pick $COMMIT "
git cherry-pick --abort || true
git reset --hard HEAD || true
break
fi
else
UPDATE=true
echo "succeed to cherry pick $COMMIT to branch $BRANCH "
fi
done
if [ "$FAIL" == "true" ] ; then
echo "failed to cherry pick to branch $BRANCH "
FINAL_FAILURE=true
echo -e "commits $PR_COMMITS of ${PR_AUTHOR} conflict when merging to branch $BRANCH, please manually cherry pick it by yourself. \n PR <${PR_URL}> , action <${ACTION_URL}> \n\n \`\`\`\n${ERROR_MESSAGE}\n\`\`\` " | \
gh issue create \
--title "failed to cherry pick PR ${PR_NUMBER} from ${PR_AUTHOR}, to branch ${BRANCH}" \
--label "${{ env.CHERRYPICK_LABEL }}" \
--assignee "${PR_AUTHOR},${{ env.DEFAULT_REVIEWER }}" \
--body-file -
fi
if [ "$UPDATE" == "true" ] ; then
echo "succeeded to cherry pick to branch $BRANCH "
# create a pr
git commit -s -S --amend --no-edit
git push origin ${PR_BRANCH}:${PR_BRANCH} -f
gh pr create --title "${PR_TITLE}" \
--assignee "${PR_AUTHOR},${{ env.DEFAULT_REVIEWER }}" \
--label "${{ env.CHERRYPICK_LABEL }},${INITIAL_LABEL}" \
--body "robot cherry pick PR <${PR_URL}> from ${PR_AUTHOR},to branch ${BRANCH}, action <${ACTION_URL}> , commits $PR_COMMITS " \
--base ${BRANCH}
else
echo "no changes happened for commits $PR_COMMITS, ignore create pr"
fi
done
echo "=========== check result ============"
if [ "$FINAL_FAILURE" == "true" ] ; then
# if failed to create issue or pr, fails
echo "error, failuire happened"
else
echo "all done"
fi
166 changes: 166 additions & 0 deletions .github/workflows/auto-release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
name: Auto Release Version

env:
LABEL_PREFIX_CHERRYPICK: "cherrypick-"

on:
push:
tags:
- v[0-9]+.[0-9]+.[0-9]+
- v[0-9]+.[0-9]+.[0-9]+-rc[0-9]+
workflow_dispatch:
inputs:
tag:
description: 'Tag'
required: true
default: v1.0.0

permissions: write-all

jobs:
get-tag:
runs-on: ubuntu-latest
outputs:
tag: ${{ env.RUN_TAG }}
steps:
- name: Get Ref
id: get_ref
run: |
if ${{ github.event_name == 'workflow_dispatch' }} ; then
echo "call by self workflow_dispatch"
echo "RUN_TAG=${{ github.event.inputs.tag }}" >> $GITHUB_ENV
YBranchName=` grep -Eo "v[0-9]+\.[0-9]+" <<< "${{ github.event.inputs.tag }}" `
elif ${{ github.event_name == 'push' }} ; then
echo "call by push tag"
echo "RUN_TAG=${GITHUB_REF##*/}" >> $GITHUB_ENV
YBranchName=` grep -Eo "v[0-9]+\.[0-9]+" <<< "${GITHUB_REF##*/}" `
else
echo "unexpected event: ${{ github.event_name }}"
exit 1
fi
echo "YBranchName=${YBranchName}"
if [ -n "$YBranchName" ] ; then
echo "RUN_YBranchName=${YBranchName}" >> $GITHUB_ENV
else
echo "error, failed to find y branch"
exit 1
fi
- name: Checkout
uses: actions/checkout@v3
with:
ref: ${{ env.RUN_TAG }}

- name: Check Version
run: |
TagVersion="${{ env.RUN_TAG }}"
RecordVersion=` cat VERSION | tr -d ' ' | tr -d '\n' `
if [ "$RecordVersion" != "$TagVersion" ] ; then
echo "error, version $RecordVersion of '/VERSION' is different with Tag $TagVersion "
exit 1
fi
#no need to check chart version, which will auto update to /VERSION by CI
#- name: release Z branch
# uses: peterjgrainger/action-create-branch@v2.4.0
# env:
# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# with:
# branch: 'release-${{ env.RUN_TAG }}'
# sha: '${{ github.sha }}'

# if branch exists, the action will no fail, and it output created=false
- name: release Y branch
uses: peterjgrainger/action-create-branch@v2.4.0
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
branch: 'release-${{ env.RUN_YBranchName }}'
sha: '${{ github.sha }}'

# for auto-cherrypick.yaml, create a label
- name: Create cherry-pick label
continue-on-error: true
run: |
echo ${{ secrets.WELAN_PAT }} | gh auth login --with-token
branchLabelName="${{ env.LABEL_PREFIX_CHERRYPICK }}release-${{ env.RUN_YBranchName }}"
mainLabelName="${{ env.LABEL_PREFIX_CHERRYPICK }}main"
echo "try to create label "
gh label create ${branchLabelName} --force || echo "error, failed to create label ${branchLabelName}"
gh label create ${mainLabelName} --force || echo "error, failed to create label ${mainLabelName}"
build-release-image:
needs: get-tag
uses: ./.github/workflows/build-image-release.yaml
with:
ref: ${{ needs.get-tag.outputs.tag }}
secrets: inherit

release-changelog:
needs: [build-release-image, get-tag]
uses: ./.github/workflows/call-release-changelog.yaml
with:
dest_tag: ${{ needs.get-tag.outputs.tag }}
secrets: inherit

release-chart:
needs: [build-release-image, get-tag]
uses: ./.github/workflows/call-release-chart.yaml
with:
ref: ${{ needs.get-tag.outputs.tag }}
secrets: inherit

create-release:
needs: [ release-changelog, get-tag, release-chart]
name: create release
runs-on: ubuntu-latest
steps:
- name: Download Chart Artifact
uses: actions/download-artifact@v3
with:
name: ${{ needs.release-chart.outputs.artifact }}
path: chart-package/

- name: Download Changelog Artifact
uses: actions/download-artifact@v3
with:
name: ${{ needs.release-changelog.outputs.artifact }}
path: changelog-result/

- name: Get Downloaded Files
id: download_file
run: |
# ========= chart
if ! ls chart-package/*.tgz &>/dev/null ; then
echo "error, failed to find any chart "
exit 1
fi
chart_path=$( ls chart-package/*.tgz )
echo "RUN_CHART_PATH=${chart_path}" >> $GITHUB_ENV
# ========== changelog
if ! ls changelog-result/*.md &>/dev/null ; then
echo "error, failed to find changelog "
exit 2
fi
ls changelog-result/
cat changelog-result/*.md
changelog_file=$( ls changelog-result/ )
changelog_path=./changelog-result/${changelog_file}
echo "RUN_CHANGELOG_PATH=${changelog_path}" >> $GITHUB_ENV
cp ./changelog-result/${changelog_file} ./changelog-result/changelog.md
- name: Create Release
id: create_release
continue-on-error: false
uses: ncipollo/release-action@v1.12.0
with:
artifacts: "chart-package/*"
allowUpdates: true
removeArtifacts: true
replacesArtifacts: true
artifactErrorsFailBuild: true
bodyFile: "./changelog-result/changelog.md"
token: ${{ secrets.GITHUB_TOKEN }}
tag: ${{ needs.get-tag.outputs.tag }}
name: "Release ${{ needs.get-tag.outputs.tag }}"
Loading

0 comments on commit cdbef1f

Please sign in to comment.