|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +repo_root_path="$(cd "$(dirname "$0")"; cd ..; pwd -P)" |
| 6 | +makefile_path="${repo_root_path}/Makefile" |
| 7 | + |
| 8 | +if ! command -v gh >/dev/null; then |
| 9 | + echo "error: required executable 'gh' not found" >&2 |
| 10 | + exit 1 |
| 11 | +fi |
| 12 | + |
| 13 | +github_nth_full_repo_name="$(make -s -f "${makefile_path}" repo-full-name)" |
| 14 | +nth_version="$(make -s -f "${makefile_path}" version)" |
| 15 | + |
| 16 | +helm_chart_name="aws-node-termination-handler-2" |
| 17 | +helm_chart_path="${repo_root_path}/charts/${helm_chart_name}" |
| 18 | + |
| 19 | +github_eks_charts_full_repo_name="aws/eks-charts" |
| 20 | +github_username="${GITHUB_USERNAME:-}" |
| 21 | +github_token="${GITHUB_TOKEN:-}" |
| 22 | + |
| 23 | +include_notes=0 |
| 24 | + |
| 25 | +usage=$(cat << EOM |
| 26 | +usage: $(basename $0) -h | [-n] [-r REPOSITORY] [-e REPOSITORY] |
| 27 | +
|
| 28 | + Open Pull Request to eks-charts repository with the latest ${helm_chart_name} Helm chart. |
| 29 | +
|
| 30 | + Options: |
| 31 | + -h Display this help message then exit. |
| 32 | + -n Include application release node in the created Pull Request. |
| 33 | + -r REPOSITORY Node Termination Handler GitHub repository. Defaults to "${github_nth_full_repo_name}". |
| 34 | +
|
| 35 | + Options for testing: |
| 36 | + -e REPOSITORY EKS Charts GitHub repository. Defaults to "${github_eks_charts_full_repo_name}". |
| 37 | + -u USERNAME GitHub username. Defaults to the value of the environment variable, GITHUB_USERNAME. |
| 38 | + -t TOKEN GitHub token. Defaults to the value of the environment variable, GITHUB_TOKEN . |
| 39 | + -v VERSION NTH version. Defaults to "${nth_version}". |
| 40 | +
|
| 41 | +EOM |
| 42 | +) |
| 43 | + |
| 44 | +while getopts "nr:e:u:t:v:h" opt; do |
| 45 | + case "${opt}" in |
| 46 | + n ) include_notes=1 |
| 47 | + ;; |
| 48 | + r ) github_nth_full_repo_name="${OPTARG}" |
| 49 | + ;; |
| 50 | + e ) github_eks_charts_full_repo_name="${OPTARG}" |
| 51 | + ;; |
| 52 | + u ) github_username="${OPTARG}" |
| 53 | + ;; |
| 54 | + t ) github_token="${OPTARG}" |
| 55 | + ;; |
| 56 | + v ) nth_version="${OPTARG}" |
| 57 | + ;; |
| 58 | + h ) echo "${usage}" |
| 59 | + exit 0 |
| 60 | + ;; |
| 61 | + \?) echo "${usage}" >&2 |
| 62 | + exit 1 |
| 63 | + ;; |
| 64 | + esac |
| 65 | +done |
| 66 | + |
| 67 | +assert_not_empty() { |
| 68 | + if [[ -z "${!1}" ]]; then |
| 69 | + echo "error: missing argument ${1}" >&2 |
| 70 | + echo "${usage}" >&2 |
| 71 | + exit 1 |
| 72 | + fi |
| 73 | +} |
| 74 | + |
| 75 | +assert_not_empty github_nth_full_repo_name |
| 76 | +assert_not_empty github_eks_charts_full_repo_name |
| 77 | +assert_not_empty github_username |
| 78 | +assert_not_empty github_token |
| 79 | +assert_not_empty nth_version |
| 80 | + |
| 81 | +github_eks_charts_repo_name="$(echo "${github_eks_charts_full_repo_name}" | cut -d '/' -f2)" |
| 82 | + |
| 83 | +################################################# |
| 84 | + |
| 85 | +echo -e "🥑 Configure gh cli" |
| 86 | + |
| 87 | +gh_client_config_dir="${HOME}/.config/gh" |
| 88 | +gh_client_config_path="${gh_client_config_dir}/config.yml" |
| 89 | +gh_client_config_backup_path="${gh_client_config_dir}/config.yml.backup" |
| 90 | + |
| 91 | +restore_gh_config() { |
| 92 | + if [[ -f "${gh_client_config_backup_path}" ]]; then |
| 93 | + echo -e "🥑 Restore gh cli configuration" |
| 94 | + mv -f "${gh_client_config_backup_path}" "${gh_client_config_path}" || : |
| 95 | + fi |
| 96 | +} |
| 97 | + |
| 98 | +trap restore_gh_config EXIT |
| 99 | + |
| 100 | +echo "Backing up existing configuration" |
| 101 | +mkdir -p "${gh_client_config_dir}" |
| 102 | +mv -f "${gh_client_config_path}" "${gh_client_config_backup_path}" |
| 103 | + |
| 104 | +echo "Writing custom configuration" |
| 105 | +cat << EOF > "${gh_client_config_path}" |
| 106 | +hosts: |
| 107 | + github.com: |
| 108 | + oauth_token: "${github_token}" |
| 109 | + user: "${github_username}" |
| 110 | +EOF |
| 111 | + |
| 112 | +################################################# |
| 113 | + |
| 114 | +echo -e "🥑 Clone ${github_eks_charts_full_repo_name}" |
| 115 | + |
| 116 | +eks_sync_path="${repo_root_path}/build/eks-sync" |
| 117 | +rm -rf "${eks_sync_path}" |
| 118 | +mkdir -p "${eks_sync_path}" |
| 119 | + |
| 120 | +cd "${eks_sync_path}" |
| 121 | +gh repo fork "${github_eks_charts_full_repo_name}" --clone --remote |
| 122 | +eks_charts_repo_path="${eks_sync_path}/${github_eks_charts_repo_name}" |
| 123 | + |
| 124 | +cd "${eks_charts_repo_path}" |
| 125 | +git_default_branch="$(git rev-parse --abbrev-ref HEAD | tr -d '\n')" |
| 126 | +git merge --ff-only "upstream/${git_default_branch}" |
| 127 | +git remote set-url origin "https://${github_username}:${github_token}@github.com/${github_username}/${github_eks_charts_repo_name}.git" |
| 128 | +git push origin "${git_default_branch}" |
| 129 | + |
| 130 | +################################################# |
| 131 | + |
| 132 | +echo -e "🥑 Check whether chart is in sync" |
| 133 | + |
| 134 | +eks_charts_nth_path="${eks_charts_repo_path}/stable/${helm_chart_name}" |
| 135 | +if diff -x ".*" -r "${helm_chart_path}/" "${eks_charts_nth_path}/" &>/dev/null ; then |
| 136 | + echo " ✅ Charts already in sync; no updates needed" |
| 137 | + exit |
| 138 | +fi |
| 139 | + |
| 140 | +echo -e "🚨 Charts are NOT in sync" |
| 141 | + |
| 142 | +################################################# |
| 143 | + |
| 144 | +echo -e "🥑 Copy updates to chart" |
| 145 | + |
| 146 | +rm -rf "${eks_charts_nth_path}" |
| 147 | +cp -R "${helm_chart_path}/" "${eks_charts_nth_path}/" |
| 148 | + |
| 149 | +################################################# |
| 150 | + |
| 151 | +echo -e "🥑 Commit updates" |
| 152 | + |
| 153 | +helm_chart_version="$(cat "${helm_chart_path}/Chart.yaml" | grep "version:" | cut -d ' ' -f2 | tr -d '"')" |
| 154 | +pr_id="$(uuidgen | cut -d '-' -f1)" |
| 155 | +git_release_branch="${helm_chart_name}-${helm_chart_version}-${pr_id}" |
| 156 | +git checkout -b "${git_release_branch}" |
| 157 | + |
| 158 | +git add --all |
| 159 | +git commit --author= "ec2-bot 🤖 <[email protected]>" -m "${helm_chart_name}: ${helm_chart_version}" |
| 160 | +git push -u origin "${git_release_branch}" |
| 161 | + |
| 162 | +################################################# |
| 163 | + |
| 164 | +echo -e "🥑 Create pull request" |
| 165 | + |
| 166 | +format_release_notes() { |
| 167 | + echo "## ${helm_chart_name} ${helm_chart_version} Automated Chart Sync! 🤖🤖" |
| 168 | + |
| 169 | + if [[ ${include_notes} -ne 1 ]]; then |
| 170 | + return 0 |
| 171 | + fi |
| 172 | + |
| 173 | + local authorization_header="Authorization: token ${GITHUB_TOKEN}" |
| 174 | + local release_id="$(curl -s -H "${authorization_header}" \ |
| 175 | + https://api.github.com/repos/${github_nth_full_repo_name}/releases | \ |
| 176 | + jq --arg ver "${nth_version}" '.[] | select(.tag_name==$ver) | .id')" |
| 177 | + |
| 178 | + echo |
| 179 | + echo "### 📝 Release Notes 📝" |
| 180 | + echo |
| 181 | + echo "$(curl -s -H "${authorization_header}" \ |
| 182 | + https://api.github.com/repos/${github_nth_full_repo_name}/releases/${release_id} | \ |
| 183 | + jq -r '.body')" |
| 184 | + echo |
| 185 | +} |
| 186 | + |
| 187 | +if ! gh pr create \ |
| 188 | + --title "🥳 ${helm_chart_name} ${helm_chart_version} Automated Release! 🥑" \ |
| 189 | + --body "$(format_release_notes)" \ |
| 190 | + --repo "${github_eks_charts_full_repo_name}" \ |
| 191 | + --base "master"; then |
| 192 | + echo -e "❌ Failed to create pull request" |
| 193 | + exit 1 |
| 194 | +fi |
| 195 | + |
| 196 | +echo -e "✅ Pull request created" |
| 197 | + |
| 198 | +echo -e "✅ EKS charts sync complete" |
0 commit comments