|
| 1 | +name: Create Release with Changelog |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + inputs: |
| 6 | + dry_run: |
| 7 | + description: 'Dry run (do not create tag/release)' |
| 8 | + required: false |
| 9 | + default: false |
| 10 | + type: boolean |
| 11 | + |
| 12 | +jobs: |
| 13 | + release: |
| 14 | + runs-on: ubuntu-latest |
| 15 | + defaults: |
| 16 | + run: |
| 17 | + working-directory: changelog_cli |
| 18 | + |
| 19 | + permissions: |
| 20 | + contents: write |
| 21 | + |
| 22 | + steps: |
| 23 | + - name: Checkout code |
| 24 | + uses: actions/checkout@v4 |
| 25 | + with: |
| 26 | + fetch-depth: 0 # Fetch all history for proper changelog generation |
| 27 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 28 | + |
| 29 | + - name: Setup Dart |
| 30 | + uses: dart-lang/setup-dart@v1 |
| 31 | + |
| 32 | + - name: Install dependencies |
| 33 | + run: dart pub get |
| 34 | + |
| 35 | + - name: Activate changelog_cli |
| 36 | + run: dart pub global activate --source=path . |
| 37 | + |
| 38 | + - name: Read version from version.dart |
| 39 | + id: version_dart |
| 40 | + run: | |
| 41 | + VERSION=$(grep "const packageVersion" lib/src/version.dart | sed "s/const packageVersion = '\(.*\)';/\1/") |
| 42 | + echo "version=$VERSION" >> $GITHUB_OUTPUT |
| 43 | + echo "Version from version.dart: $VERSION" |
| 44 | +
|
| 45 | + - name: Read version from pubspec.yaml |
| 46 | + id: version_pubspec |
| 47 | + run: | |
| 48 | + VERSION=$(grep "^version:" pubspec.yaml | sed "s/version: \(.*\)/\1/") |
| 49 | + echo "version=$VERSION" >> $GITHUB_OUTPUT |
| 50 | + echo "Version from pubspec.yaml: $VERSION" |
| 51 | +
|
| 52 | + - name: Verify versions match |
| 53 | + run: | |
| 54 | + if [ "${{ steps.version_dart.outputs.version }}" != "${{ steps.version_pubspec.outputs.version }}" ]; then |
| 55 | + echo "ERROR: Version mismatch!" |
| 56 | + echo "version.dart: ${{ steps.version_dart.outputs.version }}" |
| 57 | + echo "pubspec.yaml: ${{ steps.version_pubspec.outputs.version }}" |
| 58 | + exit 1 |
| 59 | + fi |
| 60 | + echo "✅ Versions match: ${{ steps.version_dart.outputs.version }}" |
| 61 | +
|
| 62 | + - name: Get latest tag |
| 63 | + id: latest_tag |
| 64 | + run: | |
| 65 | + # Get the latest tag for changelog_cli |
| 66 | + LATEST_TAG=$(git tag -l "changelog_cli-v*" | sort -V | tail -n 1) |
| 67 | + if [ -z "$LATEST_TAG" ]; then |
| 68 | + echo "No previous tags found, using initial commit" |
| 69 | + LATEST_TAG=$(git rev-list --max-parents=0 HEAD) |
| 70 | + fi |
| 71 | + echo "latest_tag=$LATEST_TAG" >> $GITHUB_OUTPUT |
| 72 | + echo "Latest tag: $LATEST_TAG" |
| 73 | +
|
| 74 | + - name: Check if tag already exists |
| 75 | + id: tag_check |
| 76 | + run: | |
| 77 | + TAG_NAME="changelog_cli-v${{ steps.version_dart.outputs.version }}" |
| 78 | + if git tag -l | grep -q "^$TAG_NAME$"; then |
| 79 | + echo "exists=true" >> $GITHUB_OUTPUT |
| 80 | + echo "❌ Tag $TAG_NAME already exists" |
| 81 | + else |
| 82 | + echo "exists=false" >> $GITHUB_OUTPUT |
| 83 | + echo "✅ Tag $TAG_NAME does not exist" |
| 84 | + fi |
| 85 | + echo "tag_name=$TAG_NAME" >> $GITHUB_OUTPUT |
| 86 | +
|
| 87 | + - name: Fail if tag exists |
| 88 | + if: steps.tag_check.outputs.exists == 'true' |
| 89 | + run: | |
| 90 | + echo "ERROR: Tag ${{ steps.tag_check.outputs.tag_name }} already exists!" |
| 91 | + echo "Please update the version in version.dart and pubspec.yaml" |
| 92 | + exit 1 |
| 93 | +
|
| 94 | + - name: Generate changelog since last tag |
| 95 | + id: changelog |
| 96 | + run: | |
| 97 | + echo "Generating changelog from ${{ steps.latest_tag.outputs.latest_tag }} to HEAD" |
| 98 | + |
| 99 | + # Generate changelog using changelog_cli |
| 100 | + CHANGELOG_CONTENT=$(changelog_cli generate \ |
| 101 | + --start "${{ steps.latest_tag.outputs.latest_tag }}" \ |
| 102 | + --end "HEAD" \ |
| 103 | + --version "${{ steps.version_dart.outputs.version }}" \ |
| 104 | + --printer markdown \ |
| 105 | + --path . \ |
| 106 | + --auto-tag-glob-pattern "changelog_cli*") |
| 107 | + |
| 108 | + # Save changelog content to file for later use |
| 109 | + echo "$CHANGELOG_CONTENT" > /tmp/new_changelog.md |
| 110 | + |
| 111 | + # Also save to output (escape for GitHub Actions) |
| 112 | + { |
| 113 | + echo 'content<<EOF' |
| 114 | + echo "$CHANGELOG_CONTENT" |
| 115 | + echo 'EOF' |
| 116 | + } >> $GITHUB_OUTPUT |
| 117 | +
|
| 118 | + - name: Update CHANGELOG.md |
| 119 | + run: | |
| 120 | + # Read the new changelog content |
| 121 | + NEW_CONTENT=$(cat /tmp/new_changelog.md) |
| 122 | + |
| 123 | + # Create temporary file with new content + existing changelog |
| 124 | + { |
| 125 | + echo "$NEW_CONTENT" |
| 126 | + echo "" |
| 127 | + |
| 128 | + # Add existing content if CHANGELOG.md exists and has content after first line |
| 129 | + if [ -f "CHANGELOG.md" ] && [ "$(wc -l < CHANGELOG.md)" -gt 0 ]; then |
| 130 | + tail -n +1 CHANGELOG.md |
| 131 | + fi |
| 132 | + } > /tmp/updated_changelog.md |
| 133 | + |
| 134 | + # Replace the original file |
| 135 | + mv /tmp/updated_changelog.md CHANGELOG.md |
| 136 | + |
| 137 | + echo "✅ Updated CHANGELOG.md with new version ${{ steps.version_dart.outputs.version }}" |
| 138 | +
|
| 139 | + - name: Setup Git |
| 140 | + run: | |
| 141 | + git config --local user.email "[email protected]" |
| 142 | + git config --local user.name "GitHub Action" |
| 143 | +
|
| 144 | + - name: Commit updated CHANGELOG.md |
| 145 | + run: | |
| 146 | + git add CHANGELOG.md |
| 147 | + if git diff --staged --quiet; then |
| 148 | + echo "No changes to commit" |
| 149 | + else |
| 150 | + git commit -m "chore: update CHANGELOG.md for v${{ steps.version_dart.outputs.version }}" |
| 151 | + echo "✅ Committed CHANGELOG.md changes" |
| 152 | + fi |
| 153 | +
|
| 154 | + - name: Push changes |
| 155 | + if: ${{ !inputs.dry_run }} |
| 156 | + run: | |
| 157 | + git push origin main |
| 158 | +
|
| 159 | + - name: Create and push tag |
| 160 | + if: ${{ !inputs.dry_run }} |
| 161 | + run: | |
| 162 | + TAG_NAME="${{ steps.tag_check.outputs.tag_name }}" |
| 163 | + git tag -a "$TAG_NAME" -m "Release $TAG_NAME" |
| 164 | + git push origin "$TAG_NAME" |
| 165 | + echo "✅ Created and pushed tag: $TAG_NAME" |
| 166 | +
|
| 167 | + - name: Create GitHub Release |
| 168 | + if: ${{ !inputs.dry_run }} |
| 169 | + env: |
| 170 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 171 | + run: | |
| 172 | + TAG_NAME="${{ steps.tag_check.outputs.tag_name }}" |
| 173 | + |
| 174 | + # Create release with changelog content |
| 175 | + gh release create "$TAG_NAME" \ |
| 176 | + --title "changelog_cli ${{ steps.version_dart.outputs.version }}" \ |
| 177 | + --notes-file /tmp/new_changelog.md \ |
| 178 | + --draft |
| 179 | + |
| 180 | + echo "✅ Created GitHub release: $TAG_NAME" |
| 181 | + echo "The release is created as draft and will be updated with artifacts by the build workflow" |
| 182 | +
|
| 183 | + - name: Dry run summary |
| 184 | + if: ${{ inputs.dry_run }} |
| 185 | + run: | |
| 186 | + echo "🔍 DRY RUN SUMMARY:" |
| 187 | + echo "- Version: ${{ steps.version_dart.outputs.version }}" |
| 188 | + echo "- Tag that would be created: ${{ steps.tag_check.outputs.tag_name }}" |
| 189 | + echo "- Latest tag found: ${{ steps.latest_tag.outputs.latest_tag }}" |
| 190 | + echo "- CHANGELOG.md would be updated with new content" |
| 191 | + echo "- No actual tag or release would be created" |
| 192 | + |
| 193 | + echo "" |
| 194 | + echo "📝 Generated changelog content:" |
| 195 | + cat /tmp/new_changelog.md |
0 commit comments