Skip to content

Commit

Permalink
feat(action): init
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabor Pichner committed Jan 6, 2024
0 parents commit 78d3f12
Show file tree
Hide file tree
Showing 6 changed files with 207 additions and 0 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/integration.yaml
Original file line number Diff line number Diff line change
@@ -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
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
```
30 changes: 30 additions & 0 deletions action.yaml
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -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();
}
93 changes: 93 additions & 0 deletions src/updater.sh
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions test/home-assistant.yaml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 78d3f12

Please sign in to comment.