Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 60 additions & 41 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@
name: Release

on:
push:
tags:
- 'v*'
workflow_dispatch:
inputs:
version:
description: 'Version that should be released, format: v0.0.0'
required: true
type: string
tagMessage:
description: 'Message that will be added to the version tag'
required: true
type: string

permissions:
contents: write
Expand All @@ -25,50 +32,52 @@ jobs:
git config --global user.email "github-actions[bot]@users.noreply.github.com"
git config --global user.name "github-actions[bot]"

- name: Get tag information
id: tag_data
- name: Update kustomization.yaml
run: |
# Get tag name and message
TAG_NAME=${GITHUB_REF#refs/tags/}
TAG_MESSAGE=$(git tag -n1 "$TAG_NAME" | sed "s|^$TAG_NAME[[:space:]]*||")

# If tag message is empty, use a default
if [ -z "$TAG_MESSAGE" ]; then
TAG_MESSAGE="Release $TAG_NAME"
# Fail if tag already exists
if [ -n "$(git tag --list "${{ inputs.version }}")" ]; then
echo "Tag "${{ inputs.version }}" already exists. Exit."
exit 1
else
echo "Tag "${{ inputs.version }}" does not exist. Continue."
fi

echo "TAG_NAME=$TAG_NAME" >> $GITHUB_OUTPUT
echo "TAG_MESSAGE<<EOF" >> $GITHUB_OUTPUT
echo "$TAG_MESSAGE" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
echo "VERSION=${TAG_NAME#v}" >> $GITHUB_OUTPUT
# Update image tag
cd config/manager
kustomize edit set image controller="netbox-operator:${{inputs.version}}"
cd ../..

- name: Create GitHub Release
- name: Generate release notes for changelog
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
# Determine if this is a prerelease
PRERELEASE=""
if [[ "${{ steps.tag_data.outputs.TAG_NAME }}" =~ (alpha|beta|rc) ]]; then
PRERELEASE="--prerelease"
# Get the previous tag to determine commit range
PREVIOUS_TAG=$(git describe --tags --abbrev=0 HEAD^ 2>/dev/null || echo "")

# Generate release notes using GitHub API without creating a release
if [ -n "$PREVIOUS_TAG" ]; then
RELEASE_NOTES=$(gh api repos/:owner/:repo/releases/generate-notes \
-f tag_name="${{ inputs.version }}" \
-f target_commitish="$(git rev-parse HEAD)" \
-f previous_tag_name="$PREVIOUS_TAG" \
--jq '.body')
else
# If no previous tag, generate notes from all commits
RELEASE_NOTES=$(gh api repos/:owner/:repo/releases/generate-notes \
-f tag_name="${{ inputs.version }}" \
-f target_commitish="$(git rev-parse HEAD)" \
--jq '.body')
fi

# Create release with auto-generated notes
gh release create "${{ steps.tag_data.outputs.TAG_NAME }}" \
--title "${{ steps.tag_data.outputs.TAG_NAME }}" \
--notes "${{ steps.tag_data.outputs.TAG_MESSAGE }}" \
--generate-notes \
$PRERELEASE

- name: Update kustomization.yaml
run: |
./scripts/bump-version.sh "${{ steps.tag_data.outputs.TAG_NAME }}"
# Save release notes to a file for use in changelog
echo "$RELEASE_NOTES" > /tmp/release_notes.md

- name: Update CHANGELOG.md
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
./scripts/update-changelog-from-release.sh "${{ steps.tag_data.outputs.TAG_NAME }}"
# Update changelog with the generated notes
./scripts/update-changelog-from-release.sh "${{ inputs.version }}"

- name: Commit and push changes
env:
Expand All @@ -81,23 +90,33 @@ jobs:
fi

# Create a new branch for the changes
BRANCH_NAME="release-updates-${{ steps.tag_data.outputs.TAG_NAME }}"
BRANCH_NAME="release-updates-${{ inputs.version }}"
git checkout -b "$BRANCH_NAME"

git add config/manager/kustomization.yaml CHANGELOG.md
git commit -m "[skip ci] chore: bump version to ${{ steps.tag_data.outputs.TAG_NAME }} and update changelog"

git commit -m "[skip ci] chore: bump version to ${{ inputs.version }} and update changelog"
# Push the branch and create PR

git push origin "$BRANCH_NAME"
PR_URL=$(gh pr create \
--title "chore: bump version to ${{ steps.tag_data.outputs.TAG_NAME }} and update changelog" \
--body "Automated version bump and changelog update for release ${{ steps.tag_data.outputs.TAG_NAME }}" \
--title "chore: bump version to ${{ inputs.version }} and update changelog" \
--body "Automated version bump and changelog update for release ${{ inputs.version }}" \
--head "$BRANCH_NAME" \
--base main)

# Extract PR number from URL
PR_NUMBER=$(echo "$PR_URL" | sed 's/.*\/pull\///')
echo "Created PR #$PR_NUMBER: $PR_URL"

# Enable auto-merge and merge the PR
gh pr merge "$PR_NUMBER" --auto --squash --delete-branch
# Merge the PR
gh pr merge "$PR_NUMBER" --squash --delete-branch

- name: Create GitHub Release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
# Create release with auto-generated notes
gh release create "${{ inputs.version }}" \
--title "${{ inputs.version }}" \
--notes "${{ inputs.tagMessage }}" \
--generate-notes
27 changes: 0 additions & 27 deletions scripts/bump-version.sh

This file was deleted.

16 changes: 8 additions & 8 deletions scripts/update-changelog-from-release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ fi

echo "Updating CHANGELOG.md for release $TAG_NAME..."

# Get release data from GitHub
RELEASE_DATA=$(gh release view "$TAG_NAME" --json tagName,publishedAt,body,url)
# Get release data from file
RELEASE_NOTES=$(cat /tmp/release_notes.md)

# Extract fields
TAG=$(echo "$RELEASE_DATA" | jq -r '.tagName')
DATE=$(echo "$RELEASE_DATA" | jq -r '.publishedAt' | cut -d'T' -f1)
BODY=$(echo "$RELEASE_DATA" | jq -r '.body')
URL=$(echo "$RELEASE_DATA" | jq -r '.url')
# Use current date and tag name from parameter
TAG="$TAG_NAME"
DATE=$(date +%Y-%m-%d)
BODY="$RELEASE_NOTES"
URL="https://github.com/netbox-community/netbox-operator/releases/tag/$TAG_NAME"

# Validate extracted fields
if [ -z "$TAG" ] || [ -z "$DATE" ] || [ -z "$URL" ]; then
Expand Down Expand Up @@ -67,4 +67,4 @@ awk '/^## \[/ {found=1} found {print}' CHANGELOG.md >> "$TEMP_FILE"
# Replace the original file
mv "$TEMP_FILE" CHANGELOG.md

echo "CHANGELOG.md updated successfully!"
echo "CHANGELOG.md updated successfully!"
Loading