this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

CI: Stole onedr0p's action.

+311 -121
+175
.github/.scripts/helm-release-differ.sh
··· 1 + #!/usr/bin/env bash 2 + 3 + # shellcheck source=/dev/null 4 + source "$(dirname "${0}")/lib/functions.sh" 5 + 6 + set -o errexit 7 + set -o nounset 8 + set -o pipefail 9 + shopt -s lastpipe 10 + 11 + show_help() { 12 + cat << EOF 13 + Usage: $(basename "$0") <options> 14 + -h, --help Display help 15 + --source-file Original helm release 16 + --target-file New helm release 17 + --remove-common-labels Remove common labels from manifests 18 + EOF 19 + } 20 + 21 + main() { 22 + local source_file= 23 + local target_file= 24 + local remove_common_labels= 25 + parse_command_line "$@" 26 + check "helm" 27 + check "yq" 28 + entry 29 + } 30 + 31 + parse_command_line() { 32 + while :; do 33 + case "${1:-}" in 34 + -h|--help) 35 + show_help 36 + exit 37 + ;; 38 + --source-file) 39 + if [[ -n "${2:-}" ]]; then 40 + source_file="$2" 41 + shift 42 + else 43 + echo "ERROR: '--source-file' cannot be empty." >&2 44 + show_help 45 + exit 1 46 + fi 47 + ;; 48 + --target-file) 49 + if [[ -n "${2:-}" ]]; then 50 + target_file="$2" 51 + shift 52 + else 53 + echo "ERROR: '--target-file' cannot be empty." >&2 54 + show_help 55 + exit 1 56 + fi 57 + ;; 58 + --remove-common-labels) 59 + remove_common_labels=true 60 + ;; 61 + *) 62 + break 63 + ;; 64 + esac 65 + shift 66 + done 67 + 68 + if [[ -z "${source_file}" ]]; then 69 + echo "ERROR: '--source-file' is required." >&2 70 + show_help 71 + exit 1 72 + fi 73 + 74 + if [[ $(yq eval .kind "${source_file}" 2>/dev/null) != "HelmRelease" ]]; then 75 + echo "ERROR: '--source-file' is not a HelmRelease" 76 + show_help 77 + exit 1 78 + fi 79 + 80 + if [[ -z "${target_file}" ]]; then 81 + echo "ERROR: '--target-file' is required." >&2 82 + show_help 83 + exit 1 84 + fi 85 + 86 + if [[ $(yq eval .kind "${target_file}" 2>/dev/null) != "HelmRelease" ]]; then 87 + echo "ERROR: '--target-file' is not a HelmRelease" 88 + show_help 89 + exit 1 90 + fi 91 + 92 + if [[ -z "$remove_common_labels" ]]; then 93 + remove_common_labels=false 94 + fi 95 + } 96 + 97 + _resources() { 98 + local chart_name=${1} 99 + local chart_version=${2} 100 + local chart_registry_url=${3} 101 + local chart_values=${4} 102 + local resources= 103 + 104 + helm repo add main "${chart_registry_url}" > /dev/null 2>&1 105 + pushd "$(mktemp -d)" > /dev/null 2>&1 106 + helm pull "main/${chart_name}" --untar --version "${chart_version}" 107 + resources=$(echo "${chart_values}" | helm template "${chart_name}" "${chart_name}" --version "${chart_version}" -f -) 108 + if [[ "${remove_common_labels}" == "true" ]]; then 109 + labels='.metadata.labels."helm.sh/chart"' 110 + labels+=',.metadata.labels.chart' 111 + labels+=',.metadata.labels."app.kubernetes.io/version"' 112 + labels+=',.spec.template.metadata.labels."helm.sh/chart"' 113 + labels+=',.spec.template.metadata.labels.chart' 114 + labels+=',.spec.template.metadata.labels."app.kubernetes.io/version"' 115 + echo "${resources}" | yq eval "del($labels)" - 116 + else 117 + echo "${resources}" 118 + fi 119 + popd > /dev/null 2>&1 120 + helm repo remove main > /dev/null 2>&1 121 + } 122 + 123 + entry() { 124 + local comments= 125 + 126 + source_chart_name=$(chart_name "${source_file}") 127 + source_chart_version=$(chart_version "${source_file}") 128 + source_chart_registry_url=$(chart_registry_url "${source_file}") 129 + source_chart_values=$(chart_values "${source_file}") 130 + source_resources=$(_resources "${source_chart_name}" "${source_chart_version}" "${source_chart_registry_url}" "${source_chart_values}") 131 + echo "${source_resources}" > /tmp/source_resources 132 + 133 + target_chart_version=$(chart_version "${target_file}") 134 + target_chart_name=$(chart_name "${target_file}") 135 + target_chart_registry_url=$(chart_registry_url "${target_file}") 136 + target_chart_values=$(chart_values "${target_file}") 137 + target_resources=$(_resources "${target_chart_name}" "${target_chart_version}" "${target_chart_registry_url}" "${target_chart_values}") 138 + echo "${target_resources}" > /tmp/target_resources 139 + 140 + # Diff the files and always return true 141 + diff -u /tmp/source_resources /tmp/target_resources > /tmp/diff || true 142 + # Remove the filenames 143 + sed -i -e '1,2d' /tmp/diff 144 + 145 + # Store the comment in an array 146 + comments=() 147 + 148 + # shellcheck disable=SC2016 149 + comments+=( "$(printf 'Path: `%s`' "${target_file}")" ) 150 + if [[ "${source_chart_name}" != "${target_chart_name}" ]]; then 151 + # shellcheck disable=SC2016 152 + comments+=( "$(printf 'Chart: `%s` -> `%s`' "${source_chart_name}" "${target_chart_name}")" ) 153 + fi 154 + if [[ "${source_chart_version}" != "${target_chart_version}" ]]; then 155 + # shellcheck disable=SC2016 156 + comments+=( "$(printf 'Version: `%s` -> `%s`' "${source_chart_version}" "${target_chart_version}")" ) 157 + fi 158 + if [[ "${source_chart_registry_url}" != "${target_chart_registry_url}" ]]; then 159 + # shellcheck disable=SC2016 160 + comments+=( "$(printf 'Registry URL: `%s` -> `%s`' "${source_chart_registry_url}" "${target_chart_registry_url}")" ) 161 + fi 162 + comments+=( "$(printf '\n\n')" ) 163 + if [[ -f /tmp/diff && -s /tmp/diff ]]; then 164 + # shellcheck disable=SC2016 165 + comments+=( "$(printf '```diff\n%s\n```' "$(cat /tmp/diff)")" ) 166 + else 167 + # shellcheck disable=SC2016 168 + comments+=( "$(printf '```\nNo changes in detected in resources\n```')" ) 169 + fi 170 + 171 + # Join the array with a new line and print it 172 + printf "%s\n" "${comments[@]}" 173 + } 174 + 175 + main "$@"
+47
.github/.scripts/lib/functions.sh
··· 1 + #!/usr/bin/env bash 2 + 3 + set -o errexit 4 + set -o nounset 5 + set -o pipefail 6 + shopt -s lastpipe 7 + 8 + check() { 9 + command -v "${1}" >/dev/null 2>&1 || { 10 + echo >&2 "ERROR: ${1} is not installed or not found in \$PATH" >&2 11 + exit 1 12 + } 13 + } 14 + 15 + chart_registry_url() { 16 + local helm_release= 17 + local chart_id= 18 + helm_release="${1}" 19 + chart_id=$(yq eval .spec.chart.spec.sourceRef.name "${helm_release}" 2>/dev/null) 20 + # Discover all HelmRepository 21 + find . -iname '*-charts.yaml' -type f -print0 | while IFS= read -r -d '' file; do 22 + # Skip non HelmRepository 23 + [[ $(yq eval .kind "${file}" 2>/dev/null) != "HelmRepository" ]] && continue 24 + # Skip unrelated HelmRepository 25 + [[ "${chart_id}" != $(yq eval .metadata.name "${file}" 2>/dev/null) ]] && continue 26 + yq eval .spec.url "${file}" 27 + break 28 + done 29 + } 30 + 31 + chart_name() { 32 + local helm_release= 33 + helm_release="${1}" 34 + yq eval .spec.chart.spec.chart "${helm_release}" 2>/dev/null 35 + } 36 + 37 + chart_version() { 38 + local helm_release= 39 + helm_release="${1}" 40 + yq eval .spec.chart.spec.version "${helm_release}" 2>/dev/null 41 + } 42 + 43 + chart_values() { 44 + local helm_release= 45 + helm_release="${1}" 46 + yq eval .spec.values "${helm_release}" 2>/dev/null 47 + }
+89
.github/workflows/helm-release-differ.yaml
··· 1 + --- 2 + name: "Helm Release Differ" 3 + 4 + on: # yamllint disable-line rule:truthy 5 + pull_request: 6 + branches: 7 + - main 8 + paths: 9 + - "cluster/**.yaml" 10 + 11 + env: 12 + # Currently no way to detect automatically 13 + DEFAULT_BRANCH: main 14 + BOT_USERNAME: "samip5-bot[bot]" 15 + 16 + jobs: 17 + detect-file-changes: 18 + name: Detect File Changes 19 + runs-on: ubuntu-latest 20 + steps: 21 + - name: Checkout 22 + uses: actions/checkout@v3 23 + 24 + - name: Filter Helm Releases 25 + uses: dorny/paths-filter@v2 26 + id: filter 27 + with: 28 + list-files: json 29 + filters: | 30 + yaml: 31 + - added|modified: "**/helm-release.yaml" 32 + outputs: 33 + yaml_files: ${{ steps.filter.outputs.yaml_files }} 34 + 35 + helm-release-differ: 36 + name: Helm Release Differ 37 + runs-on: ubuntu-latest 38 + needs: detect-file-changes 39 + strategy: 40 + matrix: 41 + file: ${{ fromJSON(needs.detect-file-changes.outputs.yaml_files) }} 42 + steps: 43 + - name: Checkout 44 + uses: actions/checkout@v3 45 + 46 + - name: Generate Token 47 + uses: tibdex/github-app-token@v1 48 + id: generate-token 49 + with: 50 + app_id: ${{ secrets.BOT_APP_ID }} 51 + private_key: ${{ secrets.BOT_APP_PRIVATE_KEY }} 52 + 53 + - name: Checkout default branch 54 + uses: actions/checkout@v3 55 + with: 56 + ref: "${{ env.DEFAULT_BRANCH }}" 57 + path: default 58 + 59 + - name: Install Helm 60 + uses: azure/setup-helm@v3 61 + with: 62 + version: latest 63 + 64 + - name: Helm Release Differ 65 + id: diff 66 + run: | 67 + diff=$(.github/scripts/helm-release-differ.sh --source-file "default/${{ matrix.file }}" --target-file "${{ matrix.file }}" --remove-common-labels) 68 + echo "${diff}" 69 + diff="${diff//'%'/'%25'}" 70 + diff="${diff//$'\n'/'%0A'}" 71 + diff="${diff//$'\r'/'%0D'}" 72 + echo "::set-output name=diff::$(echo ${diff})" 73 + 74 + - name: Find Comment 75 + uses: peter-evans/find-comment@v2 76 + id: find-comment 77 + with: 78 + issue-number: "${{ github.event.pull_request.number }}" 79 + comment-author: "${{ env.BOT_USERNAME }}" 80 + body-includes: "${{ matrix.file }}" 81 + 82 + - name: Create or update comment 83 + uses: peter-evans/create-or-update-comment@v2 84 + with: 85 + token: "${{ steps.generate-token.outputs.token }}" 86 + comment-id: "${{ steps.find-comment.outputs.comment-id }}" 87 + issue-number: "${{ github.event.pull_request.number }}" 88 + body: "${{ steps.diff.outputs.diff }}" 89 + edit-mode: replace
-121
.github/workflows/helmrelese-pr.yaml
··· 1 - name: Create diff on updated Helm Releases 2 - 3 - on: # yamllint disable-line rule:truthy 4 - pull_request: 5 - branches: 6 - - main 7 - paths: 8 - - "cluster/**.yaml" 9 - 10 - env: 11 - conf_live_branch: main 12 - conf_ignore_known_labels_containing_versions: true 13 - 14 - jobs: 15 - changes: 16 - name: Detect changes 17 - runs-on: ubuntu-20.04 18 - outputs: 19 - files: "${{ steps.extract.outputs.files }}" 20 - steps: 21 - - name: Checkout 22 - uses: actions/checkout@v3 23 - - name: Get changes 24 - uses: dorny/paths-filter@v2 25 - id: filter 26 - with: 27 - list-files: shell 28 - filters: | 29 - changed: 30 - - '**' 31 - - name: Keep HelmReleases only 32 - id: extract 33 - run: | 34 - filtered=$(grep -zl "kind: HelmRelease.*registryUrl=" ${{ steps.filter.outputs.changed_files }} \ 35 - | jq -nR '[inputs | select(length>0)]') 36 - echo ::set-output name=files::${filtered} 37 - helm: 38 - name: Template HelmReleases 39 - runs-on: ubuntu-20.04 40 - if: ${{ needs.changes.outputs.files != '[]' }} 41 - needs: 42 - - changes 43 - strategy: 44 - matrix: 45 - file: ${{ fromJson(needs.changes.outputs.files) }} 46 - fail-fast: false 47 - steps: 48 - - name: Setup Kubernetes Tools 49 - uses: yokawasa/action-setup-kube-tools@v0.9.2 50 - with: 51 - setup-tools: | 52 - helm 53 - yq 54 - - name: Checkout live branch 55 - uses: actions/checkout@v3 56 - with: 57 - ref: ${{ env.conf_live_branch }} 58 - path: live 59 - - name: Checkout PR branch 60 - uses: actions/checkout@v3 61 - with: 62 - path: pr 63 - - name: Create diff 64 - id: diff 65 - run: | 66 - hr_live_url=$(sed -nr 's|.*registryUrl=(.+)$|\1|p' live/${{ matrix.file }}) 67 - hr_live_chart=$(yq e .spec.chart.spec.chart live/${{ matrix.file }}) 68 - hr_live_version=$(yq e .spec.chart.spec.version live/${{ matrix.file }}) 69 - hr_live_values=$(yq e .spec.values live/${{ matrix.file }}) 70 - hr_pr_url=$(sed -nr 's|.*registryUrl=(.+)$|\1|p' pr/${{ matrix.file }}) 71 - hr_pr_chart=$(yq e .spec.chart.spec.chart pr/${{ matrix.file }}) 72 - hr_pr_version=$(yq e .spec.chart.spec.version pr/${{ matrix.file }}) 73 - hr_pr_values=$(yq e .spec.values pr/${{ matrix.file }}) 74 - helm repo add live "$hr_live_url" 75 - helm repo add pr "$hr_pr_url" 76 - resources_live=$(echo "$hr_live_values" | \ 77 - helm template "$hr_live_chart" \ 78 - live/"$hr_live_chart" \ 79 - --version "$hr_live_version" -f - || true) 80 - echo "$resources_live" 81 - echo "#####################################################" 82 - resources_pr=$(echo "$hr_pr_values" | \ 83 - helm template "$hr_pr_chart" \ 84 - pr/"$hr_pr_chart" \ 85 - --version "$hr_pr_version" -f -) 86 - echo "$resources_pr" 87 - echo "#####################################################" 88 - if [ "$conf_ignore_known_labels_containing_versions" = "true" ]; then 89 - labels='.metadata.labels."helm.sh/chart"' 90 - labels+=',.metadata.labels.chart' 91 - labels+=',.metadata.labels."app.kubernetes.io/version"' 92 - labels+=',.spec.template.metadata.labels."helm.sh/chart"' 93 - labels+=',.spec.template.metadata.labels.chart' 94 - labels+=',.spec.template.metadata.labels."app.kubernetes.io/version"' 95 - resources_live=$(echo "$resources_live" | yq e "del($labels)" -) 96 - resources_pr=$(echo "$resources_pr" | yq e "del($labels)" -) 97 - fi 98 - diff=$((diff -u <(echo "$resources_live") <(echo "$resources_pr") || true) | tail +3) 99 - echo "$diff" 100 - message="Path: \`${{ matrix.file }}\`" 101 - if [ "$hr_live_chart" != "$hr_pr_chart" ]; then 102 - message="$message"$'\n'"Chart: \`$hr_live_chart\` -> \`$hr_pr_chart\`" 103 - fi 104 - if [ "$hr_live_version" != "$hr_pr_version" ]; then 105 - message="$message"$'\n'"Version: \`$hr_live_version\` -> \`$hr_pr_version\`" 106 - fi 107 - if [ "$hr_live_url" != "$hr_pr_url" ]; then 108 - message="$message"$'\n'"Repo: \`$hr_live_url\` -> \`$hr_pr_url\`" 109 - fi 110 - message="$message"$'\n'$'\n' 111 - if [ -z "$diff" ]; then 112 - message="$message"'```'$'\n'"No changes in detected in resources"$'\n''```' 113 - else 114 - message="$message"'```diff'$'\n'"$diff"$'\n''```' 115 - fi 116 - echo "::set-output name=message::$(echo "$message" | jq --raw-input --slurp)" 117 - - name: Create comment 118 - uses: peter-evans/create-or-update-comment@v2 119 - with: 120 - issue-number: ${{ github.event.pull_request.number }} 121 - body: "${{ fromJSON(steps.diff.outputs.message) }}"