name: Release Charts on: pull_request: paths: - 'charts/**' push: branches: - 'main' paths: - 'charts/**' jobs: charts-changed: name: Get Charts Being Changed runs-on: ci-os steps: - name: Checkout Repository uses: actions/checkout@v4 with: fetch-depth: 0 - name: Identify Merge Commit and List Changed Charts 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 echo "changesExist=true" >> $GITHUB_ENV else echo "changesExist=false" >> $GITHUB_ENV fi outputs: changesExist: ${{ env.changesExist }} reposChanged: ${{ env.reposChanged }} 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' runs-on: ci-os steps: - name: Checkout Repository uses: actions/checkout@v4 with: fetch-depth: 0 - name: Publish Helm Charts env: FORGEJO_TOKEN: ${{ secrets.REPO_TOKEN }} OCI_URL: oci://code.252.no/tommy/charts run: | charts="${{ needs.charts-changed.outputs.reposChanged }}" # Iterate over each changed chart 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 rm -rf dist || true mkdir dist || true CHART_NAME=$(yq -r .name "$chart/Chart.yaml") CHART_VERSION=$(yq -r .version "$chart/Chart.yaml") 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 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 helm push "dist/$TAG.tgz" "$OCI_URL" 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 "==================" done