|
| 1 | +--- |
| 2 | +name: "Prepare Release" |
| 3 | +on: # yamllint disable-line rule:truthy rule:comments |
| 4 | + workflow_dispatch: |
| 5 | + inputs: |
| 6 | + bump_rule: |
| 7 | + description: "Select the version bump type" |
| 8 | + required: true |
| 9 | + default: "patch" |
| 10 | + type: "choice" |
| 11 | + options: |
| 12 | + - "prerelease" |
| 13 | + - "patch" |
| 14 | + - "minor" |
| 15 | + - "major" |
| 16 | + target_branch: |
| 17 | + description: "Create the release from this branch (default: main)." |
| 18 | + required: true |
| 19 | + default: "main" |
| 20 | + date: |
| 21 | + description: "Date of the release YYYY-MM-DD (defaults to today's date in the US Eastern TZ)." |
| 22 | + required: false |
| 23 | + default: "" |
| 24 | + |
| 25 | +jobs: |
| 26 | + prepare-release: |
| 27 | + permissions: |
| 28 | + contents: "write" |
| 29 | + pull-requests: "write" |
| 30 | + name: "Prepare Release" |
| 31 | + runs-on: "ubuntu-latest" |
| 32 | + steps: |
| 33 | + - name: "Checkout code" |
| 34 | + uses: "actions/checkout@v4" |
| 35 | + with: |
| 36 | + # If target_branch is 'main', use 'develop' as the source branch. Otherwise, the source and target branch are the same. |
| 37 | + ref: "${{ github.event.inputs['target_branch'] == 'main' && 'develop' || github.event.inputs['target_branch'] }}" |
| 38 | + fetch-depth: 0 # Fetch all history for git tags |
| 39 | + |
| 40 | + - name: "Setup environment" |
| 41 | + uses: "networktocode/gh-action-setup-poetry-environment@v6" |
| 42 | + with: |
| 43 | + poetry-version: "2.1.3" |
| 44 | + poetry-install-options: "--with dev" |
| 45 | + |
| 46 | + - name: "Validate Branch and Tags" |
| 47 | + run: | |
| 48 | + # 1. Verify branch exists |
| 49 | + if ! git rev-parse --verify origin/${{ github.event.inputs.target_branch }} > /dev/null 2>&1; then |
| 50 | + echo "Error: Branch ${{ github.event.inputs.target_branch }} does not exist." |
| 51 | + exit 1 |
| 52 | + fi |
| 53 | +
|
| 54 | + # 2. Try to get the previous version tag |
| 55 | + # If it fails (no tags), get the hash of the first commit |
| 56 | + if PREV_TAG=$(git describe --tags --abbrev=0 2>/dev/null); then |
| 57 | + echo "PREVIOUS_TAG=$PREV_TAG" >> $GITHUB_ENV |
| 58 | + echo "Found previous tag: $PREV_TAG" |
| 59 | + else |
| 60 | + # Fallback to the first commit in the repository |
| 61 | + FIRST_COMMIT=$(git rev-list --max-parents=0 HEAD) |
| 62 | + echo "PREVIOUS_TAG=$FIRST_COMMIT" >> $GITHUB_ENV |
| 63 | + echo "No tags found. Falling back to initial commit: $FIRST_COMMIT" |
| 64 | + fi |
| 65 | +
|
| 66 | + - name: "Determine New Version" |
| 67 | + id: "versioning" |
| 68 | + run: | |
| 69 | + # Perform the bump based on the user input |
| 70 | + poetry version ${{ github.event.inputs.bump_rule }} |
| 71 | +
|
| 72 | + # Capture the New version string for use in other steps |
| 73 | + NEW_VER=$(poetry version --short) |
| 74 | + echo "NEW_VERSION=$NEW_VER" >> $GITHUB_ENV |
| 75 | + echo "RELEASE_BRANCH=release/$NEW_VER" >> $GITHUB_ENV |
| 76 | +
|
| 77 | + - name: "Set Date Variable" |
| 78 | + run: | |
| 79 | + if [ -z "${{ github.event.inputs.date }}" ]; then |
| 80 | + RELEASE_DATE=$(TZ=America/New_York date +%Y-%m-%d) |
| 81 | + else |
| 82 | + RELEASE_DATE="${{ github.event.inputs.date }}" |
| 83 | + fi |
| 84 | + echo "RELEASE_DATE=$RELEASE_DATE" >> $GITHUB_ENV |
| 85 | +
|
| 86 | + - name: "Create Release Branch" |
| 87 | + env: |
| 88 | + GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}" |
| 89 | + run: | |
| 90 | + git config user.name "${{ github.actor }}" |
| 91 | + git config user.email "${{ github.actor }}@users.noreply.github.com" |
| 92 | +
|
| 93 | + # Ensure release branch doesn't already exist |
| 94 | + if git rev-parse --verify origin/${{ env.RELEASE_BRANCH }} > /dev/null 2>&1; then |
| 95 | + echo "Error: Release branch ${{ env.RELEASE_BRANCH }} already exists." |
| 96 | + exit 1 |
| 97 | + fi |
| 98 | +
|
| 99 | + # Create a new branch for the release |
| 100 | + git checkout -b "${{ env.RELEASE_BRANCH }}" |
| 101 | +
|
| 102 | + - name: "Regenerate poetry.lock" |
| 103 | + run: "poetry lock --regenerate" |
| 104 | + |
| 105 | + - name: "Generate Github Release Notes" |
| 106 | + env: |
| 107 | + GH_TOKEN: "${{ secrets.GITHUB_TOKEN }}" |
| 108 | + run: | |
| 109 | + # 1. Get Towncrier Draft |
| 110 | + TOWNCRIER_NOTES=$(poetry run towncrier build --version "${{ env.NEW_VERSION }}" --date "${{ env.RELEASE_DATE }}" --draft) |
| 111 | +
|
| 112 | + # 2. Call GitHub API to generate raw notes |
| 113 | + RAW_GH_NOTES=$(gh api /repos/${{ github.repository }}/releases/generate-notes \ |
| 114 | + -f tag_name="v${{ env.NEW_VERSION }}" \ |
| 115 | + -f target_commitish="${{ github.event.inputs['target_branch'] == 'main' && 'develop' || github.event.inputs['target_branch'] }}" \ |
| 116 | + -f previous_tag_name="${{ env.PREVIOUS_TAG }}" --jq '.body') |
| 117 | +
|
| 118 | + # 3. Parse usernames (Regex match) |
| 119 | + # We use grep to find "@user" patterns, sort, and uniq them |
| 120 | + USERNAMES=$(echo "$RAW_GH_NOTES" | grep -oP 'by @\K[a-zA-Z0-9-]+' | sort -u | grep -vE 'dependabot|nautobot-bot|github-actions' || true) |
| 121 | +
|
| 122 | + # 4. Format the Contributors section |
| 123 | + CONTRIBUTORS_SECTION="## Contributors" |
| 124 | + for user in $USERNAMES; do |
| 125 | + CONTRIBUTORS_SECTION="$CONTRIBUTORS_SECTION"$'\n'"* @$user" |
| 126 | + done |
| 127 | +
|
| 128 | + # 5. Extract the "Full Changelog" or "New Contributors" part |
| 129 | + # Using awk to grab everything from '## New Contributors' or '**Full Changelog**' to the end |
| 130 | + GH_FOOTER=$(echo "$RAW_GH_NOTES" | awk '/## New Contributors/ || /\*\*Full Changelog\*\*/ {found=1} found {print}') |
| 131 | + if [ -z "$GH_FOOTER" ]; then |
| 132 | + GH_FOOTER=$(echo "$RAW_GH_NOTES" | sed -n '/**Full Changelog**/,$p') |
| 133 | + fi |
| 134 | +
|
| 135 | + # 6. Combine everything |
| 136 | + FINAL_NOTES="$TOWNCRIER_NOTES"$'\n\n'"$CONTRIBUTORS_SECTION"$'\n\n'"$GH_FOOTER" |
| 137 | +
|
| 138 | + # 7. Save to a temporary file to avoid shell argument length limits |
| 139 | + echo "$FINAL_NOTES" > ../consolidated_notes.md |
| 140 | +
|
| 141 | + - name: "Generate App Release Notes and update mkdocs.yml" |
| 142 | + run: "poetry run inv generate-release-notes --version '${{ env.NEW_VERSION }}' --date '${{ env.RELEASE_DATE }}'" |
| 143 | + |
| 144 | + - name: "Commit Changes and Push" |
| 145 | + run: | |
| 146 | + # Add all changes (pyproject.toml, poetry.lock, etc.) |
| 147 | + git add . |
| 148 | + git commit -m "prepare release v${{ env.NEW_VERSION }}" |
| 149 | + git push origin "${{ env.RELEASE_BRANCH }}" |
| 150 | +
|
| 151 | + - name: "Create Pull Request" |
| 152 | + env: |
| 153 | + GH_TOKEN: "${{ secrets.GITHUB_TOKEN }}" |
| 154 | + run: | |
| 155 | + gh pr create \ |
| 156 | + --title "Release v${{ env.NEW_VERSION }}" \ |
| 157 | + --body-file "../consolidated_notes.md" \ |
| 158 | + --base "${{ github.event.inputs.target_branch }}" \ |
| 159 | + --head "${{ env.RELEASE_BRANCH }}" |
| 160 | +
|
| 161 | + - name: "Create Draft Release" |
| 162 | + env: |
| 163 | + GH_TOKEN: "${{ secrets.GITHUB_TOKEN }}" |
| 164 | + run: | |
| 165 | + if [[ "${{ github.event.inputs.bump_rule }}" == "prerelease" ]]; then |
| 166 | + RELEASE_FLAGS="--prerelease" |
| 167 | + elif [[ "${{ github.event.inputs.target_branch }}" == "main" ]]; then |
| 168 | + RELEASE_FLAGS="--latest" |
| 169 | + else |
| 170 | + RELEASE_FLAGS="--latest=false" |
| 171 | + fi |
| 172 | +
|
| 173 | + gh release create "v${{ env.NEW_VERSION }}" \ |
| 174 | + --draft \ |
| 175 | + $RELEASE_FLAGS \ |
| 176 | + --title "v${{ env.NEW_VERSION }} - ${{ env.RELEASE_DATE }}" \ |
| 177 | + --notes-file "../consolidated_notes.md" \ |
| 178 | + --target "${{ github.event.inputs.target_branch }}" |
0 commit comments