From 78d3f1267882fabd15dee77a62a6160d2c902fb1 Mon Sep 17 00:00:00 2001 From: Gabor Pichner Date: Sat, 6 Jan 2024 21:55:30 +0100 Subject: [PATCH] feat(action): init --- .github/workflows/integration.yaml | 35 +++++++++++ README.md | 18 ++++++ action.yaml | 30 ++++++++++ src/index.js | 17 ++++++ src/updater.sh | 93 ++++++++++++++++++++++++++++++ test/home-assistant.yaml | 14 +++++ 6 files changed, 207 insertions(+) create mode 100644 .github/workflows/integration.yaml create mode 100644 README.md create mode 100644 action.yaml create mode 100644 src/index.js create mode 100755 src/updater.sh create mode 100644 test/home-assistant.yaml diff --git a/.github/workflows/integration.yaml b/.github/workflows/integration.yaml new file mode 100644 index 0000000..abce763 --- /dev/null +++ b/.github/workflows/integration.yaml @@ -0,0 +1,35 @@ +name: integration + +on: + pull_request: + branches: + - main + paths: + - '.github/workflows/**' + - 'src/**' + - 'action.yaml' + push: + branches: + - main + paths: + - '.github/workflows/**' + - 'src/**' + - 'action.yaml' + +jobs: + simple: + name: Simple test with specified folders, recursive and colors + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: [ubuntu-latest, ubuntu-22.04] + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Simple test + uses: ./ + with: + main_director: ./test + recursive: true + color_logs: true diff --git a/README.md b/README.md new file mode 100644 index 0000000..cf33bbd --- /dev/null +++ b/README.md @@ -0,0 +1,18 @@ +# ArgoCD Helm chart version updater GitHub Action + +## Usage + +```yaml +jobs: + argo-update: + name: Argo Helm chart version updater + runs-on: ubuntu-22.04 + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Simple test + uses: 95gabor/argocd-helm-updater + with: + main_director: ./infra +``` diff --git a/action.yaml b/action.yaml new file mode 100644 index 0000000..695acc1 --- /dev/null +++ b/action.yaml @@ -0,0 +1,30 @@ +# https://help.github.com/en/articles/metadata-syntax-for-github-actions +name: ArgoCD Helm Chart Updater +description: Updates Helm chart versions in ArgoCD Application descriptors +author: Gabor Pichner + +inputs: + main_directory: + description: 'The main directory for the script.' + required: false + default: '.' + + recursive: + description: 'Enable recursive search.' + required: false + default: 'true' + + color_logs: + description: 'Enable colored logs.' + required: false + default: 'true' + + dry_run: + description: 'Enable dry run mode.' + required: false + default: 'false' + +runs: + using: 'node20' + + main: src/index.js diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..32460de --- /dev/null +++ b/src/index.js @@ -0,0 +1,17 @@ +const { join } = require('path'); +const { spawnSync } = require('child_process'); + +function main() { + const updater = join(__dirname, 'updater.sh'); + const { status } = spawnSync(updater, { stdio: 'inherit' }); + + if (typeof status === 'number') { + process.exit(status) + } + + process.exit(1) +} + +if (require.main === module) { + main(); +} diff --git a/src/updater.sh b/src/updater.sh new file mode 100755 index 0000000..ef402b2 --- /dev/null +++ b/src/updater.sh @@ -0,0 +1,93 @@ +#!/bin/bash + +main_directory="${1:-.}" +recursive="${2:-true}" +color_logs="${3:-true}" +dry_run="${4:-false}" + +color_red="\033[1;31m" +color_green="\033[1;32m" +color_reset="\033[0m" + +log_error() { + local message="$1" + if [ "$color_logs" = true ]; then + echo -e "${color_red}${message}${color_reset}" + else + echo "$message" + fi +} + +log_success() { + local message="$1" + if [ "$color_logs" = true ]; then + echo -e "${color_green}${message}${color_reset}" + else + echo "$message" + fi +} + +check_command() { + command -v "$1" >/dev/null 2>&1 || { + log_error "Error: The '$1' command is required but not installed. Please install the appropriate package." + exit 1 + } +} + +check_command "helm" +check_command "jq" +check_command "yq" + +if [ "$recursive" = true ]; then + find_command="find \"$main_directory\" -type f \( -name \"*.yaml\" -o -name \"*.yml\" \) -print0" +else + find_command="find \"$main_directory\" -maxdepth 1 -type f \( -name \"*.yaml\" -o -name \"*.yml\" \) -print0" +fi + +eval "$find_command" | while IFS= read -r -d $'\0' file; do + kind=$(yq eval '.kind' "$file") + + if [ "$kind" != "Application" ]; then + continue + fi + + repo_url=$(yq eval ".spec.source.repoURL" "$file") + if [ -z "$repo_url" ]; then + log_error "Missing .spec.source.repoURL in $file. Skipping..." + continue + fi + + chart=$(yq eval ".spec.source.chart" "$file") + if [ -z "$chart" ]; then + log_error "Missing chart in $file. Skipping..." + continue + fi + + current_revision=$(yq eval ".spec.source.targetRevision" "$file") + if [ -z "$current_revision" ]; then + log_error "Missing .spec.source.targetRevision in $file. Skipping..." + continue + fi + + app_name=$(yq eval ".metadata.name" "$file") + if [ -z "$app_name" ]; then + log_error "Missing .metadata.name in $file. Skipping..." + continue + fi + + helm repo add "$app_name" "$repo_url" + helm repo update "$app_name" + + # Use helm search with output as json and jq to filter the result + latest_version=$(helm search repo "$app_name/$chart" --output json | + jq -r "max_by(.version) | .version // empty") + + if [ -n "${latest_version}" ]; then + if [ "$dry_run" != true ]; then + yq eval -i ".spec.source.targetRevision = \"$latest_version\"" "$file" + fi + log_success "Updated targetRevision in $file to $latest_version." + else + log_error "Failed to retrieve the latest version of the Helm Chart for $file." + fi +done diff --git a/test/home-assistant.yaml b/test/home-assistant.yaml new file mode 100644 index 0000000..faf45aa --- /dev/null +++ b/test/home-assistant.yaml @@ -0,0 +1,14 @@ +apiVersion: argoproj.io/v1alpha1 +kind: Application +metadata: + name: home-assistant + namespace: argocd +spec: + project: default + source: + repoURL: https://95gabor.github.io/home-charts + chart: home-assistant + targetRevision: 0.2.1 + destination: + server: https://kubernetes.default.svc + namespace: smart-home