Skip to content

Auto Release Chart #998

Auto Release Chart

Auto Release Chart #998

Workflow file for this run

name: Auto Release Chart
env:
DEFAULT_FAIL_REVIEWER: "weizhoublue"
DEFAULT_SUCCEED_REVIEWER: "liyuerich,ty-dc"
on:
push:
branches:
- 'main'
# auto trigger, in case push failure sometimes
schedule:
# each day
- cron: "0 20 * * *"
jobs:
get_chart:
runs-on: ubuntu-latest
outputs:
ENV_charts_changed: ${{ env.ENV_charts_changed }}
ENV_PR_NUMBER: ${{ env.ENV_PR_NUMBER }}
ENV_PR_AUTHOR: ${{ env.ENV_PR_AUTHOR }}
ENV_JUST_PUSH: ${{ env.ENV_JUST_PUSH }}
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: get chart when schedule
run : |
if ${{ github.event_name == 'push' }} ; then
exit 0
fi
echo "ENV_charts_changed=all" >> $GITHUB_ENV
echo "ENV_JUST_PUSH=true" >> $GITHUB_ENV
- name: get chart when push
env:
GITHUB_TOKEN: ${{ secrets.WELAN_PAT}}
JSON: ${{ toJSON(github) }}
run: |
set -x
if ${{ github.event_name != 'push' }} ; then
exit 0
fi
echo "============ print var ======== "
grep -Eio "Merge pull request #[0-9]+ " <<< "${JSON}" || true
PR_NUMBER=` grep -Eio "Merge pull request #[0-9]+ " <<< "${JSON}" | grep -Eo "[0-9]+" | uniq ` || true
[ -n "${PR_NUMBER}" ] || { echo "no PR number, ignore" ; exit 0 ; }
PR_URL="https://github.com/${{ github.repository }}/pull/${PR_NUMBER}"
ACTION_URL="${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
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" ; }
gh pr view ${PR_NUMBER}
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_TITLE=`gh pr view ${PR_NUMBER} | sed -n '1 p' `
[ -n "${PR_TITLE}" ] || { echo "error, no PR_TITLE " ; exit 1 ; }
#
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' ' ' `
[ -n "${PR_COMMITS}" ] || { echo "no PR_COMMITS, ignore" ; }
#
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 changed chart========"
URL="https://api.github.com/repos/${{ github.repository }}/pulls/${PR_NUMBER}/files"
files_changed_data=$(curl -s --header 'authorization: Bearer ${{ secrets.GITHUB_TOKEN }}' -X GET -G "$URL")
echo "files_changed_data: $files_changed_data"
files_changed="$(echo $files_changed_data | jq -r '.[] | .filename')"
echo "files_changed: $files_changed"
#--------------------
# Adding || true to avoid "Process exited with code 1" errors
charts_changed="$(echo "$files_changed" | xargs dirname | grep -E -o "charts/([^/]*)/\1" | grep -o "charts/[^/]*" | sort | uniq | awk -F'/' '{print $2}' | tr '\n' ' ' || true)"
echo "charts_changed: $charts_changed"
echo "ENV_charts_changed=${charts_changed}" >> $GITHUB_ENV
echo "ENV_PR_AUTHOR=${PR_AUTHOR}" >> $GITHUB_ENV
echo "ENV_PR_NUMBER=${PR_NUMBER}" >> $GITHUB_ENV
call_release:
needs: [get_chart]
if: ${{ needs.get_chart.outputs.ENV_charts_changed != '' }}
uses: ./.github/workflows/release-chart.yml
with:
project: "${{ needs.get_chart.outputs.ENV_charts_changed }}"
ref: "main"
justpush: "${{ needs.get_chart.outputs.ENV_JUST_PUSH }}"
secrets: inherit
create_issue:
needs: [get_chart,call_release]
if: ${{ needs.get_chart.outputs.ENV_JUST_PUSH != 'true' && needs.get_chart.outputs.ENV_charts_changed != '' && always() }}
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: create issue
env:
GITHUB_TOKEN: ${{ secrets.WELAN_PAT}}
run : |
set -x
PROJECT_LIST="${{ needs.get_chart.outputs.ENV_charts_changed }}"
ACTION_URL="${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
PR_NUMBER="${{ needs.get_chart.outputs.ENV_PR_NUMBER }}"
PR_URL="https://github.com/${{ github.repository }}/pull/${PR_NUMBER}"
PROJECT_INFO=""
for PROJECT in $PROJECT_LIST ; do
VERSION=`source charts/${PROJECT}/config && echo ${VERSION} `
PROJECT_INFO+=" ${PROJECT}<${VERSION}> "
done
echo "privious result: ${{ needs.call_release.result }}"
if ${{ needs.call_release.result != 'success' }} ; then
echo "failed to release chart ${PROJECT_INFO} "
LABEL="robot/release-chart-fail"
gh label create "${LABEL}" --force || true
#
REVIWER=${{ needs.get_chart.outputs.ENV_PR_AUTHOR }}
[ -n "${REVIWER}" ] || REVIWER=${{ env.DEFAULT_FAIL_REVIEWER }}
#
gh issue create \
--body "I am robot, failed to release chart ${PROJECT_INFO} to daocloud chart registery. Trigger by PR <${PR_URL}>. Refer to Github Action <${ACTION_URL}>" \
--title "robot: failed to release ${PROJECT_INFO} by PR ${PR_NUMBER}" \
--label "${LABEL}" \
--assignee "${REVIWER}"
else
echo "succeeded to release chart ${PROJECT_INFO} "
LABEL="robot/release-chart-succeed"
gh label create "${LABEL}" --force || true
FAILED=""
for PROJECT in $PROJECT_LIST ; do
VERSION=`source charts/${PROJECT}/config && echo ${VERSION} `
TEST_ASSIGNER=`source charts/${PROJECT}/config && echo ${TEST_ASSIGNER} ` || true
if [ -n "$TEST_ASSIGNER" ] ; then
echo "valid assigner $TEST_ASSIGNER"
else
echo "invalid assigner $TEST_ASSIGNER, assign to default"
TEST_ASSIGNER=${{ env.DEFAULT_SUCCEED_REVIEWER }}
fi
echo "---------- create issue for $PROJECT -------"
# maybe the TEST_ASSIGNER is wrong or not existed, failed to execute
gh issue create \
--body "I am robot, Trigger by PR <${PR_URL}>, charts ${PROJECT} ${VERSION} has been released with new version to daocloud chart registery. Refer to Github Action <${ACTION_URL}>" \
--title "robot: test new version ${PROJECT} ${VERSION} by PR ${PR_NUMBER}" \
--label "${LABEL}" \
--assignee "${TEST_ASSIGNER}" || FAILED="true"
done
if [ -n "$FAILED" ] ; then
echo "failed to create issue for some project"
exit 1
else
echo "all succeed"
fi
fi