|
| 1 | +name: Release and Publish |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + inputs: |
| 6 | + version_type: |
| 7 | + description: 'Version bump type (auto = determined by conventional commits)' |
| 8 | + required: true |
| 9 | + default: 'auto' |
| 10 | + type: choice |
| 11 | + options: |
| 12 | + - auto |
| 13 | + - patch |
| 14 | + - minor |
| 15 | + - major |
| 16 | + dry_run: |
| 17 | + description: 'Dry run (preview changes without publishing)' |
| 18 | + required: false |
| 19 | + default: false |
| 20 | + type: boolean |
| 21 | + |
| 22 | +jobs: |
| 23 | + release: |
| 24 | + name: Release and Publish |
| 25 | + runs-on: ubuntu-latest |
| 26 | + permissions: |
| 27 | + contents: write |
| 28 | + id-token: write |
| 29 | + |
| 30 | + steps: |
| 31 | + - name: Checkout repository |
| 32 | + uses: actions/checkout@v4 |
| 33 | + with: |
| 34 | + fetch-depth: 0 |
| 35 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 36 | + |
| 37 | + - name: Setup Node.js |
| 38 | + uses: actions/setup-node@v4 |
| 39 | + with: |
| 40 | + node-version: '20' |
| 41 | + registry-url: 'https://registry.npmjs.org' |
| 42 | + cache: 'npm' |
| 43 | + |
| 44 | + - name: Configure Git |
| 45 | + run: | |
| 46 | + git config --global user.name "github-actions[bot]" |
| 47 | + git config --global user.email "github-actions[bot]@users.noreply.github.com" |
| 48 | +
|
| 49 | + - name: Install dependencies |
| 50 | + run: npm ci |
| 51 | + |
| 52 | + - name: Build |
| 53 | + run: npm run build |
| 54 | + |
| 55 | + - name: Run tests |
| 56 | + run: npm run test |
| 57 | + env: |
| 58 | + CI: true |
| 59 | + |
| 60 | + - name: Check for changes |
| 61 | + id: check |
| 62 | + run: | |
| 63 | + CHANGED=$(npx lerna changed --json 2>/dev/null || echo "[]") |
| 64 | + if [ "$CHANGED" == "[]" ]; then |
| 65 | + echo "changed=false" >> $GITHUB_OUTPUT |
| 66 | + echo "No packages have changed since last release" |
| 67 | + else |
| 68 | + echo "changed=true" >> $GITHUB_OUTPUT |
| 69 | + echo "Changed packages:" |
| 70 | + echo "$CHANGED" | jq -r '.[].name' |
| 71 | + fi |
| 72 | +
|
| 73 | + - name: Dry Run - Preview Changes |
| 74 | + if: inputs.dry_run == true |
| 75 | + run: | |
| 76 | + echo "=== DRY RUN MODE ===" |
| 77 | + echo "" |
| 78 | + echo "Version type: ${{ inputs.version_type }}" |
| 79 | + if [ "${{ inputs.version_type }}" == "auto" ]; then |
| 80 | + echo " (auto = determined by conventional commits)" |
| 81 | + echo " - fix: commits → patch bump" |
| 82 | + echo " - feat: commits → minor bump" |
| 83 | + echo " - BREAKING CHANGE → major bump" |
| 84 | + fi |
| 85 | + echo "" |
| 86 | + echo "=== Changed packages ===" |
| 87 | + npx lerna changed -l || echo "No packages changed" |
| 88 | + echo "" |
| 89 | + echo "=== Preview version bumps ===" |
| 90 | + VERSION_ARG="" |
| 91 | + if [ "${{ inputs.version_type }}" != "auto" ]; then |
| 92 | + VERSION_ARG="${{ inputs.version_type }}" |
| 93 | + fi |
| 94 | + npx lerna version $VERSION_ARG --conventional-commits --no-git-tag-version --no-push --yes 2>/dev/null || true |
| 95 | + echo "" |
| 96 | + echo "=== Files that would change ===" |
| 97 | + git diff --name-only |
| 98 | + git checkout -- . |
| 99 | +
|
| 100 | + - name: Version packages |
| 101 | + if: inputs.dry_run == false && steps.check.outputs.changed == 'true' |
| 102 | + id: version |
| 103 | + run: | |
| 104 | + # Determine version argument (empty for auto = let conventional commits decide) |
| 105 | + VERSION_ARG="" |
| 106 | + if [ "${{ inputs.version_type }}" != "auto" ]; then |
| 107 | + VERSION_ARG="${{ inputs.version_type }}" |
| 108 | + fi |
| 109 | +
|
| 110 | + # Version packages with conventional commits |
| 111 | + # When VERSION_ARG is empty (auto), lerna uses conventional commits to determine version: |
| 112 | + # fix: → patch, feat: → minor, BREAKING CHANGE → major |
| 113 | + npx lerna version $VERSION_ARG \ |
| 114 | + --yes \ |
| 115 | + --conventional-commits \ |
| 116 | + --changelog-preset angular \ |
| 117 | + --message "chore(release): publish" |
| 118 | +
|
| 119 | + # Get the new version for release notes |
| 120 | + NEW_VERSION=$(node -p "require('./lerna.json').version || 'independent'") |
| 121 | + echo "version=$NEW_VERSION" >> $GITHUB_OUTPUT |
| 122 | + env: |
| 123 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 124 | + |
| 125 | + - name: Generate Release Notes |
| 126 | + if: inputs.dry_run == false && steps.check.outputs.changed == 'true' |
| 127 | + id: release_notes |
| 128 | + run: | |
| 129 | + # Get the latest tag |
| 130 | + LATEST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "") |
| 131 | +
|
| 132 | + # Generate changelog from commits |
| 133 | + if [ -n "$LATEST_TAG" ]; then |
| 134 | + PREV_TAG=$(git describe --tags --abbrev=0 ${LATEST_TAG}^ 2>/dev/null || echo "") |
| 135 | + if [ -n "$PREV_TAG" ]; then |
| 136 | + COMMITS=$(git log ${PREV_TAG}..${LATEST_TAG} --pretty=format:"- %s (%h)" --no-merges) |
| 137 | + else |
| 138 | + COMMITS=$(git log ${LATEST_TAG} --pretty=format:"- %s (%h)" --no-merges -20) |
| 139 | + fi |
| 140 | + else |
| 141 | + COMMITS=$(git log --pretty=format:"- %s (%h)" --no-merges -20) |
| 142 | + fi |
| 143 | +
|
| 144 | + # Create release notes file |
| 145 | + cat > release_notes.md << 'NOTES_EOF' |
| 146 | + ## What's Changed |
| 147 | +
|
| 148 | + NOTES_EOF |
| 149 | +
|
| 150 | + # Add package versions |
| 151 | + echo "### Published Packages" >> release_notes.md |
| 152 | + echo "" >> release_notes.md |
| 153 | + npx lerna ls -l --json | jq -r '.[] | "- \(.name)@\(.version)"' >> release_notes.md |
| 154 | + echo "" >> release_notes.md |
| 155 | +
|
| 156 | + # Add commits |
| 157 | + echo "### Commits" >> release_notes.md |
| 158 | + echo "" >> release_notes.md |
| 159 | + if [ -n "$COMMITS" ]; then |
| 160 | + echo "$COMMITS" >> release_notes.md |
| 161 | + else |
| 162 | + echo "- Initial release" >> release_notes.md |
| 163 | + fi |
| 164 | + echo "" >> release_notes.md |
| 165 | +
|
| 166 | + # Add install instructions |
| 167 | + echo "### Installation" >> release_notes.md |
| 168 | + echo "" >> release_notes.md |
| 169 | + echo '```bash' >> release_notes.md |
| 170 | + echo 'npm install @hokify/node-ts-cache' >> release_notes.md |
| 171 | + echo '```' >> release_notes.md |
| 172 | +
|
| 173 | + cat release_notes.md |
| 174 | +
|
| 175 | + - name: Publish to npm |
| 176 | + if: inputs.dry_run == false && steps.check.outputs.changed == 'true' |
| 177 | + run: | |
| 178 | + npx lerna publish from-git --yes |
| 179 | + env: |
| 180 | + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} |
| 181 | + |
| 182 | + - name: Create GitHub Release |
| 183 | + if: inputs.dry_run == false && steps.check.outputs.changed == 'true' |
| 184 | + run: | |
| 185 | + # Get the latest tag created by lerna |
| 186 | + LATEST_TAG=$(git describe --tags --abbrev=0) |
| 187 | +
|
| 188 | + # Create the GitHub release |
| 189 | + gh release create "$LATEST_TAG" \ |
| 190 | + --title "Release $LATEST_TAG" \ |
| 191 | + --notes-file release_notes.md |
| 192 | + env: |
| 193 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 194 | + |
| 195 | + - name: Summary |
| 196 | + if: inputs.dry_run == false && steps.check.outputs.changed == 'true' |
| 197 | + run: | |
| 198 | + echo "## Release Summary" >> $GITHUB_STEP_SUMMARY |
| 199 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 200 | + echo "Successfully released the following packages:" >> $GITHUB_STEP_SUMMARY |
| 201 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 202 | + npx lerna ls -l --json | jq -r '.[] | "- **\(.name)** @ \(.version)"' >> $GITHUB_STEP_SUMMARY |
| 203 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 204 | + LATEST_TAG=$(git describe --tags --abbrev=0) |
| 205 | + echo "GitHub Release: https://github.com/${{ github.repository }}/releases/tag/$LATEST_TAG" >> $GITHUB_STEP_SUMMARY |
| 206 | +
|
| 207 | + - name: No changes to release |
| 208 | + if: steps.check.outputs.changed == 'false' |
| 209 | + run: | |
| 210 | + echo "## No Changes" >> $GITHUB_STEP_SUMMARY |
| 211 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 212 | + echo "No packages have changed since the last release. Nothing to publish." >> $GITHUB_STEP_SUMMARY |
0 commit comments