charts/.forgejo/workflows/release-charts.yaml

147 lines
4.2 KiB
YAML
Raw Permalink Normal View History

2024-11-12 17:00:34 +00:00
name: Release Charts
2024-11-14 21:33:51 +00:00
2024-11-12 17:00:34 +00:00
on:
2024-11-14 21:33:51 +00:00
pull_request:
paths:
- 'charts/**'
push:
branches:
- 'main'
paths:
- 'charts/**'
2024-11-12 17:00:34 +00:00
jobs:
charts-changed:
2024-11-14 21:33:51 +00:00
name: Get Charts Being Changed
2024-11-12 17:00:34 +00:00
runs-on: ci-os
2024-11-12 17:00:34 +00:00
steps:
- name: Checkout Repository
2024-11-12 17:00:34 +00:00
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Identify Merge Commit and List Changed Charts
2024-11-12 17:00:34 +00:00
id: changed-charts
run: |
# Get the current commit SHA
CURRENT_SHA=$(git rev-parse HEAD)
# Get parent commits
PARENT_COUNT=$(git rev-list --parents -n 1 $CURRENT_SHA | wc -w)
if [ "$PARENT_COUNT" -gt 2 ]; then
echo "This is a merge commit with $((PARENT_COUNT - 1)) parents."
PARENT_SHA=$(git rev-parse HEAD^1)
elif [ "$PARENT_COUNT" -eq 2 ]; then
PARENT_SHA=$(git rev-parse HEAD^1)
else
echo "Single commit with no parents. Listing all changes."
PARENT_SHA=HEAD~1
fi
echo "Current Commit SHA: $CURRENT_SHA"
echo "Parent Commit SHA: $PARENT_SHA"
# List changed files and trim any extra line breaks
CHANGED_FILES=$(git diff --name-only $PARENT_SHA $CURRENT_SHA | tr -d '\r')
echo "Changed files:"
echo "$CHANGED_FILES"
# Extract chart directories
CHANGED_CHARTS=$(echo "$CHANGED_FILES" | grep '^charts/' | awk -F/ '{print $2}' | sort -u | tr -d '\r')
echo "Changed charts:"
echo "$CHANGED_CHARTS"
# Convert to JSON array
if [ -z "$CHANGED_CHARTS" ]; then
CHANGED_CHARTS_JSON="[]"
else
CHANGED_CHARTS_JSON=$(echo "$CHANGED_CHARTS" | jq -R -s -c 'split("\n") | map(select(length > 0))')
fi
echo "reposChanged=$CHANGED_CHARTS_JSON" >> $GITHUB_OUTPUT
if [ -n "$CHANGED_CHARTS" ]; then
2024-11-12 17:00:34 +00:00
echo "changesExist=true" >> $GITHUB_ENV
2024-11-14 21:33:51 +00:00
else
echo "changesExist=false" >> $GITHUB_ENV
2024-11-12 17:00:34 +00:00
fi
2024-11-12 17:00:34 +00:00
outputs:
changesExist: ${{ env.changesExist }}
2024-11-14 21:33:51 +00:00
reposChanged: ${{ env.reposChanged }}
2024-11-12 17:00:34 +00:00
helm-publish:
name: "Publish Helm Chart"
needs: charts-changed
if: github.event_name == 'push' && github.ref == 'refs/heads/main' && needs.charts-changed.outputs.changesExist == 'true'
2024-11-12 17:00:34 +00:00
runs-on: ci-os
2024-11-12 17:00:34 +00:00
steps:
- name: Checkout Repository
2024-11-12 17:00:34 +00:00
uses: actions/checkout@v4
with:
fetch-depth: 0
2024-11-14 21:33:51 +00:00
- name: Publish Helm Charts
2024-11-12 17:00:34 +00:00
env:
FORGEJO_TOKEN: ${{ secrets.REPO_TOKEN }}
OCI_URL: oci://code.252.no/tommy/charts
2024-11-12 17:00:34 +00:00
run: |
2024-11-14 21:33:51 +00:00
charts="${{ needs.charts-changed.outputs.reposChanged }}"
2024-11-12 17:00:34 +00:00
# Iterate over each changed chart
2024-11-14 21:33:51 +00:00
for chart in $(echo "$charts" | jq -r '.[]'); do
echo "Processing chart: $chart"
# Ensure the chart directory exists
if [ ! -d "$chart" ]; then
echo "Directory $chart does not exist. Skipping."
continue
fi
2024-11-12 17:00:34 +00:00
rm -rf dist || true
mkdir dist || true
2024-11-12 18:52:00 +00:00
2024-11-12 17:00:34 +00:00
CHART_NAME=$(yq -r .name "$chart/Chart.yaml")
CHART_VERSION=$(yq -r .version "$chart/Chart.yaml")
2024-11-14 21:33:51 +00:00
TAG="$CHART_NAME-$CHART_VERSION"
REPO="${{ github.repository }}"
RELEASE_DIR="dist"
RELEASE_NOTES="/var/ci-os/templates/release-notes-template.md"
SHA="${{ github.sha }}"
echo "=================="
echo "Chart: $chart"
echo "Chart Name: $CHART_NAME"
echo "Chart Version: $CHART_VERSION"
echo "Tag: $TAG"
echo "Uploading to: $OCI_URL"
# Package the Helm chart
2024-11-12 18:52:00 +00:00
helm package "$chart" -d dist
# Login to Helm registry
echo "${FORGEJO_TOKEN}" | helm registry login code.252.no -u tommy --password-stdin
# Push the Helm chart to OCI registry
2024-11-14 21:33:51 +00:00
helm push "dist/$TAG.tgz" "$OCI_URL"
2024-11-12 18:52:00 +00:00
echo "------------------"
# Create a release (Assuming forgejo-release is properly configured)
echo "Creating release"
echo "Repository: $REPO"
echo "Tag: $TAG"
echo "Release notes: $RELEASE_NOTES"
echo "Release dir: $RELEASE_DIR"
echo "SHA: $SHA"
forgejo-release create
echo "=================="
2024-11-12 17:00:34 +00:00
done