Auto Upgrade Chart #718
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Auto Upgrade Chart | |
env: | |
ISSUE_LABEL_PREFIX: "robot-upgrade" | |
DEFAULT_REVIEWER: "weizhoublue" | |
on: | |
schedule: | |
# each day | |
- cron: "0 20 * * *" | |
workflow_dispatch: | |
inputs: | |
project: | |
description: 'project names seperated by comma, or else empty for all projects' | |
required: false | |
jobs: | |
check_project: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v3 | |
with: | |
fetch-depth: 0 | |
- name: check project | |
run: | | |
#get all existed project | |
TMP=`ls charts` | |
PROJECT_LIST="" | |
for ITEM in $TMP ; do | |
if [ -d "charts/${ITEM}/${ITEM}" ] ; then | |
PROJECT_LIST+=" ${ITEM} " | |
fi | |
done | |
echo "all existed projects: ${PROJECT_LIST}" | |
[ -n "$PROJECT_LIST" ] || { echo "error, found no projects" ; exit 1 ; } | |
if ${{ github.event_name == 'workflow_dispatch' }} ; then | |
echo "workflow_dispatch event" | |
var=${{ github.event.inputs.project }} | |
echo "input $var" | |
if [ -n "${var}" ] ; then | |
for ITEM in ${var} ; do | |
grep " $ITEM " <<< "$PROJECT_LIST" &>/dev/null && continue | |
echo "error, project $ITEM is forbidden to upgrade" | |
exit 1 | |
done | |
echo "ENV_UPGRADE_PROJECT_LIST=${var}" >> $GITHUB_ENV | |
else | |
echo "empty input, upgrade all projects" | |
echo "ENV_UPGRADE_PROJECT_LIST=${PROJECT_LIST}" >> $GITHUB_ENV | |
fi | |
else | |
echo "schedule event, upgrade all projects" | |
echo "ENV_UPGRADE_PROJECT_LIST=${PROJECT_LIST}" >> $GITHUB_ENV | |
fi | |
- name: Set up Helm | |
uses: azure/[email protected] | |
- name: upgrade | |
env: | |
GITHUB_TOKEN: ${{ secrets.WELAN_PAT}} | |
run: | | |
set -x | |
PROJECTS="${{ env.ENV_UPGRADE_PROJECT_LIST }}" | |
echo "begin to upgrade for projects ${PROJECTS}" | |
FAILED_MESSAGE="" | |
SUCCEED_PROJECT="" | |
git branch | |
git config user.email "[email protected]" | |
git config user.name "robot" | |
for PROJECT in $PROJECTS ; do | |
echo "====================== checking project $PROJECT =================" | |
CONFIG_FILE="charts/${PROJECT}/config" | |
REPO_URL=` source ${CONFIG_FILE} && echo ${REPO_URL} ` | |
REPO_NAME=` source ${CONFIG_FILE} && echo ${REPO_NAME} ` | |
CHART_NAME=` source ${CONFIG_FILE} && echo ${CHART_NAME} ` | |
VERSION=` source ${CONFIG_FILE} && echo ${VERSION} ` | |
METHOD=` source ${CONFIG_FILE} && echo ${UPGRADE_METHOD} ` | |
UPGRADE_REVIWER=` source ${CONFIG_FILE} && echo ${UPGRADE_REVIWER} ` | |
[ -n "$METHOD" ] || METHOD="none" | |
if [ "${METHOD}" == "none" ]; then | |
echo "no need to upgrade for project $PROJECT " | |
continue | |
fi | |
[ -n "$REPO_URL" ] || { MSG="error, project ${PROJECT} miss REPO_URL" ; echo "${MSG}"; FAILED_MESSAGE+="${MSG} \n"; continue ; } | |
[ -n "$REPO_NAME" ] || { MSG="error, project ${PROJECT} miss REPO_NAME" ; echo "${MSG}"; FAILED_MESSAGE+="${MSG} \n"; continue ; } | |
[ -n "$CHART_NAME" ] || { MSG="error, project ${PROJECT} miss CHART_NAME" ; echo "${MSG}"; FAILED_MESSAGE+="${MSG} \n"; continue ; } | |
[ -n "$VERSION" ] || { MSG="error, project ${PROJECT} miss VERSION" ; echo "${MSG}"; FAILED_MESSAGE+="${MSG} \n"; continue ; } | |
[ -n "$UPGRADE_REVIWER" ] || UPGRADE_REVIWER=${{ env.DEFAULT_REVIEWER }} | |
echo "${PROJECT} REPO_URL=${REPO_URL}" | |
echo "${PROJECT} REPO_NAME=${REPO_NAME}" | |
echo "${PROJECT} CHART_NAME=${CHART_NAME}" | |
echo "${PROJECT} VERSION=${VERSION}" | |
echo "${PROJECT} METHOD=${METHOD}" | |
echo "${PROJECT} UPGRADE_REVIWER=${UPGRADE_REVIWER}" | |
helm repo remove $REPO_NAME || true | |
helm repo add $REPO_NAME $REPO_URL || { MSG="error, failed to add REPO_URL $REPO_URL for project ${PROJECT} " ; echo "${MSG}"; FAILED_MESSAGE+="${MSG} \n"; continue ; } | |
helm repo update $REPO_NAME | |
helm search repo ${REPO_NAME}/${CHART_NAME} || true | |
LATEST_VERSION=` helm search repo ${REPO_NAME}/${CHART_NAME} | sed '1 d' | grep -E "^${REPO_NAME}\/${CHART_NAME}[[:space:]]" | awk '{print \$2}' ` || true | |
[ -n "${LATEST_VERSION}" ] || { MSG="error, failed to find latest chart version for project ${PROJECT} " ; FAILED_MESSAGE+="${MSG} \n"; continue ; } | |
echo "latest chart version of project ${PROJECT} is $LATEST_VERSION" | |
if [ "${LATEST_VERSION}" == "${VERSION}" ] ; then | |
echo "no chart change for project $PROJECT " | |
continue | |
fi | |
echo "begin to upgrade for project $PROJECT " | |
LABEL="${{ env.ISSUE_LABEL_PREFIX }}/${PROJECT}" | |
gh label create "${LABEL}" --force || true | |
git reset --hard HEAD || true | |
git checkout main | |
if [ "${METHOD}" == "issue" ]; then | |
echo "upgrade to issue" | |
TITLE_PREFIX="Robot Upgrade: ${PROJECT} chart upgrade" | |
FINAL_TITLE="${TITLE_PREFIX} from $VERSION to ${LATEST_VERSION}" | |
gh issue list --label "${LABEL}" | |
HIS=`gh issue list --label "${LABEL}"` | |
if grep "${FINAL_TITLE}" <<< "$HIS" &>/dev/null ; then | |
echo "an issue has been submitted for ${PROJECT} ${LATEST_VERSION}, ignore" | |
continue | |
fi | |
if grep "${TITLE_PREFIX}" <<< "$HIS" &>/dev/null ; then | |
LIST=`grep "${TITLE_PREFIX}" <<< "$HIS" | awk '{print \$1}' | tr '\n' ' ' ` || true | |
echo "remove historical robot ISSUE for project ${PROJECT}: ${LIST}" | |
for ISSUE_NUM in ${LIST} ; do | |
gh issue close ${ISSUE_NUM} -c "robot: found newer version ${LATEST_VERSION}" | |
done | |
fi | |
gh issue create \ | |
--body "I am robot, ${PROJECT} chart could upgrade from $VERSION to ${LATEST_VERSION} " \ | |
--title "${FINAL_TITLE}" \ | |
--label "${LABEL}" \ | |
--assignee "${UPGRADE_REVIWER}" | |
if (( $? != 0 )) ; then | |
MSG="error, failed to create PR for project ${PROJECT} for LATEST_VERSION=$LATEST_VERSION" | |
FAILED_MESSAGE+="${MSG} \n" | |
git reset --hard HEAD || true | |
continue | |
fi | |
SUCCEED_PROJECT+=" new ${PROJECT} commits to issue \n" | |
continue | |
else | |
sed -iE 's?export VERSION=.*?export VERSION='"${LATEST_VERSION}"'?' ${CONFIG_FILE} | |
rm -f ${CONFIG_FILE}E || true | |
if ! make build_chart -e PROJECT=${PROJECT} ; then | |
MSG="error, failed to build_chart for project ${PROJECT} for LATEST_VERSION=$LATEST_VERSION" | |
FAILED_MESSAGE+="${MSG} \n" | |
git reset --hard HEAD || true | |
continue | |
fi | |
git status | |
if git diff --exit-code --quiet ; then | |
MSG="error, no code change, when build_chart for project ${PROJECT} for LATEST_VERSION=$LATEST_VERSION" | |
FAILED_MESSAGE+="${MSG} \n" | |
git reset --hard HEAD || true | |
continue | |
fi | |
PR_BRANCH=upgrade/${PROJECT}/${LATEST_VERSION} | |
git checkout -b ${PR_BRANCH} | |
git add . | |
git commit -s -m "robot: project ${PROJECT} chart upgrades from $VERSION to ${LATEST_VERSION}" | |
git push origin ${PR_BRANCH}:${PR_BRANCH} -f | |
TITLE_PREFIX="Robot Upgrade: ${PROJECT} chart upgrade" | |
FINAL_TITLE="${TITLE_PREFIX} from $VERSION to ${LATEST_VERSION}" | |
gh pr list --label "${LABEL}" | |
HIS=`gh pr list --label "${LABEL}"` | |
if grep "${FINAL_TITLE}" <<< "$HIS" &>/dev/null ; then | |
echo "an PR has been submitted for ${PROJECT} ${LATEST_VERSION}, ignore" | |
continue | |
fi | |
if grep "${TITLE_PREFIX}" <<< "$HIS" &>/dev/null ; then | |
LIST=`grep "${TITLE_PREFIX}" <<< "$HIS" | awk '{print \$1}' | tr '\n' ' ' ` || true | |
echo "remove historical robot PR for project ${PROJECT}: ${LIST}" | |
for PR_NUM in ${LIST} ; do | |
gh pr close ${PR_NUM} -c "robot: found newer version ${LATEST_VERSION}" | |
done | |
fi | |
gh pr create --title "${FINAL_TITLE}" \ | |
--assignee "${UPGRADE_REVIWER}" \ | |
--label "${LABEL}" \ | |
--body "I am robot, upgrade: project ${PROJECT} chart upgrade from $VERSION to ${LATEST_VERSION}" \ | |
--base main | |
if (( $? != 0 )) ; then | |
MSG="error, failed to create PR for project ${PROJECT} for LATEST_VERSION=$LATEST_VERSION" | |
FAILED_MESSAGE+="${MSG} \n" | |
git reset --hard HEAD || true | |
continue | |
fi | |
SUCCEED_PROJECT+=" new ${PROJECT} commits to pr\n" | |
echo "succeeded to upgrade project ${PROJECT} chart upgrade from $VERSION to ${LATEST_VERSION}" | |
fi | |
done | |
echo "ENV_FAILED_MESSAGE=${FAILED_MESSAGE}" >> $GITHUB_ENV | |
echo "SUCCEED_PROJECT=${SUCCEED_PROJECT}" >> $GITHUB_ENV | |
- name: result | |
env: | |
GITHUB_TOKEN: ${{ secrets.WELAN_PAT}} | |
run: | | |
SUCCEED_PROJECT="${{ env.SUCCEED_PROJECT }}" | |
FAILED_MESSAGE="${{ env.ENV_FAILED_MESSAGE }}" | |
echo -e "SUCCEED_PROJECT=${SUCCEED_PROJECT}" | |
echo -e "FAILED_MESSAGE=${FAILED_MESSAGE}" | |
if [ -n "$FAILED_MESSAGE" ] ; then | |
exit 1 | |
else | |
exit 0 | |
fi | |