Skip to content

add kustomize workflow + test #14

add kustomize workflow + test

add kustomize workflow + test #14

name: Deploy to GitOps Repo
on:
push:
branches:
# - main
- add_kustomize_workflow
paths:
- 'k8s/helm/*/**'
- 'k8s/kustomize/*/**'
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Install Helm
uses: azure/setup-helm@v1
with:
version: v3.13.1
- name: Setup Kustomize
uses: multani/action-setup-kustomize@v1
with:
version: 5.1.1
- name: Detect Changed App Dirs
id: changed-apps
run: |
# Get the list of changed paths
CHANGED_PATHS=$(git diff --name-only HEAD^ HEAD)
# Filter for paths that are exactly four folders deep and prefixed with 'k8s/helm/' and 'k8s/kustomize/'
CHANGED_KUSTOMIZE_DIRS=$(echo "$CHANGED_PATHS" | grep '^k8s/kustomize/' | awk -F'/' '{print $1"/"$2"/"$3"/"$4}' | uniq)
CHANGED_HELM_DIRS=$(echo "$CHANGED_PATHS" | grep '^k8s/helm/' | awk -F'/' '{print $1"/"$2"/"$3"/"$4}' | uniq)
# Convert the list to a space-separated string
CHANGED_HELM_DIRS_STR=$(echo $CHANGED_HELM_DIRS | tr '\n' ' ')
CHANGED_KUSTOMIZE_DIRS_STR=$(echo $CHANGED_KUSTOMIZE_DIRS | tr '\n' ' ')
echo "Changed helm directories string: $CHANGED_HELM_DIRS_STR"
echo "Changed kustomize directories string: $CHANGED_KUSTOMIZE_DIRS_STR"
# Set the output variable for the next steps
echo "::set-output name=changed-helm::$CHANGED_HELM_DIRS_STR"
echo "::set-output name=changed-kustomize::$CHANGED_KUSTOMIZE_DIRS_STR"
- name: Template Helm App Manifests
if: steps.changed-apps.outputs.changed-helm != ''
run: |
# Reading the list of changed charts
CHANGED_HELM_DIRS=(${{ steps.changed-apps.outputs.changed-helm }})
# Directory to store the generated manifests
MANIFESTS_DIR=./generated-manifests
mkdir -p $MANIFESTS_DIR
# Loop through each changed directory and run helm template
for chart_dir in "${CHANGED_HELM_DIRS[@]}"; do
if [ -d "$chart_dir" ]; then
echo "Templating chart in $chart_dir"
# Extract app name
APP_NAME=$(basename $chart_dir)
# Extract chart namespace
TEMP="${chart_dir%/*}"
APP_NAMESPACE="${TEMP##*/}"
# Running helm template
mkdir -p $MANIFESTS_DIR/$APP_NAME
helm template $APP_NAME $chart_dir --namespace $APP_NAMESPACE > "$MANIFESTS_DIR/$APP_NAME/generated.yaml"
else
echo "Skipping $chart_dir, not a directory"
fi
done
- name: Template Kustomize App Manifests
if: steps.changed-apps.outputs.changed-kustomize != ''
run: |
# Reading the list of changed charts
CHANGED_KUSTOMIZE_DIRS=(${{ steps.changed-apps.outputs.changed-kustomize }})
# Directory to store the generated manifests
MANIFESTS_DIR=./generated-manifests
mkdir -p $MANIFESTS_DIR
# Loop through each changed directory and run helm template
for kustomize_dir in "${CHANGED_KUSTOMIZE_DIRS[@]}"; do
if [ -d "$kustomize_dir" ]; then
echo "Templating kustomize app in $kustomize_dir"
# Extract app name
APP_NAME=$(basename $chart_dir)
# Running kustomize build
mkdir -p $MANIFESTS_DIR/$APP_NAME
kustomize build $kustomize_dir > "$MANIFESTS_DIR/$APP_NAME/generated.yaml"
else
echo "Skipping $kustomize_dir, not a directory"
fi
done
- name: Commit to GitOps Repository
if: |
steps.changed-apps.outputs.changed-helm != ''
|| steps.changed-apps.outputs.changed-kustomize != ''
run: |
# Configuration
MANIFESTS_DIR="./generated-manifests"
GITHUB_WORKFLOW_URL=$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID
COMMIT_MESSAGE="Update manifests from workflow -- $GITHUB_WORKFLOW_URL"
# Setup Git Config
git config --global user.name "$GITHUB_ACTOR"
git config --global user.email "[email protected]"
# Clone the GitOps repository using the token for authentication
echo "Cloning GitOps repository..."
git clone https://${{ secrets.GITOPS_TOKEN }}:x-oauth-basic@${GITOPS_REPO#"https://"} gitops
# Copy the generated manifests to the GitOps repository
echo "Copying manifests..."
cp -r $MANIFESTS_DIR/* gitops/
# Check for changes
echo "Checking for changes..."
cd gitops
git status
if [ -z "$(git status --porcelain)" ]; then
echo "No changes to commit, exiting gracefully."
exit 0
fi
# Commit and Push changes
echo "Committing and pushing changes..."
git add .
git commit -m "$COMMIT_MESSAGE"
# Push using the token for authentication
git push https://${{ secrets.GITOPS_TOKEN }}:x-oauth-basic@${GITOPS_REPO#"https://"} HEAD:main
env:
GITOPS_REPO: https://github.com/zbialik/gitops.git
GITOPS_TOKEN: ${{ secrets.GITOPS_TOKEN }}