|
| 1 | +name: Notification Manager package release |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + tags: |
| 6 | + - 'react-cookie-manager@[0-9]+.[0-9]+.[0-9]+' |
| 7 | + - 'sveltekit-cookie-manager@[0-9]+.[0-9]+.[0-9]+' |
| 8 | + |
| 9 | +jobs: |
| 10 | + publish-npm-package: |
| 11 | + outputs: |
| 12 | + library_dir: ${{ steps.detect_tag.outputs.library_dir }} |
| 13 | + tag: ${{ github.ref_name }} |
| 14 | + runs-on: ubuntu-latest |
| 15 | + steps: |
| 16 | + - uses: actions/checkout@v3 |
| 17 | + - uses: actions/setup-node@v3 |
| 18 | + with: |
| 19 | + node-version: 16 |
| 20 | + registry-url: https://registry.npmjs.org/ |
| 21 | + - name: Detect tag and set variable |
| 22 | + id: detect_tag |
| 23 | + run: | |
| 24 | + # Set the library name based on the tag |
| 25 | + if [[ ${{ github.ref }} =~ ^refs/tags/(.*)-cookie-manager.* ]]; then |
| 26 | + library_name=${BASH_REMATCH[1]} |
| 27 | + echo "library_dir=$library_name" >> "$GITHUB_OUTPUT" |
| 28 | + else |
| 29 | + echo "Invalid tag" |
| 30 | + exit 1 |
| 31 | + fi |
| 32 | +
|
| 33 | + - name: Publish package |
| 34 | + # The package directory is the library name e.g "apps/sveltekit | apps/react" |
| 35 | + run: | |
| 36 | + cd cookie-manager/${{ steps.detect_tag.outputs.library_dir }} && \ |
| 37 | + pnpm i && \ |
| 38 | + pnpm package && \ |
| 39 | + cp ../../LICENSE ../../NOTICE . && \ |
| 40 | + npm publish --access=public |
| 41 | + env: |
| 42 | + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} |
| 43 | + |
| 44 | + github_draft_release: |
| 45 | + needs: publish-npm-package |
| 46 | + runs-on: ubuntu-latest |
| 47 | + steps: |
| 48 | + - name: Checkout |
| 49 | + uses: actions/checkout@v3 |
| 50 | + - name: Prepare repository tags |
| 51 | + run: | |
| 52 | + # Fetch all tags |
| 53 | + if git rev-parse --is-shallow-repository | grep -q 'true'; then |
| 54 | + git fetch --prune --unshallow --tags -f |
| 55 | + else |
| 56 | + git fetch --prune --tags -f |
| 57 | + fi |
| 58 | + - name: Build changelog |
| 59 | + id: build_changelog |
| 60 | + run: | |
| 61 | + echo "Building changelog..." |
| 62 | +
|
| 63 | + # Create a changelog file with a header: |
| 64 | + echo "# What's Changed" > CHANGELOG.md |
| 65 | +
|
| 66 | + # Get the current tag from the workflow |
| 67 | + current_tag="${{ needs.publish-npm-package.outputs.tag }}" |
| 68 | +
|
| 69 | + # Extract the tag type from the current tag |
| 70 | + current_tag_type=$(echo "$current_tag" | awk -F'@' '{print $1}') |
| 71 | +
|
| 72 | + library_tags=$(git tag -l "$current_tag_type@*" --sort=-v:refname) |
| 73 | +
|
| 74 | + tag_array=() |
| 75 | + while IFS= read -r line; do |
| 76 | + tag_array+=("$line") |
| 77 | + done <<< "$library_tags" |
| 78 | +
|
| 79 | + current_tag_index=-1 |
| 80 | + for i in "${!tag_array[@]}"; do |
| 81 | + if [ "${tag_array[$i]}" == "$current_tag" ]; then |
| 82 | + current_tag_index=$i |
| 83 | + break |
| 84 | + fi |
| 85 | + done |
| 86 | +
|
| 87 | + if [ "$current_tag_index" -eq -1 ]; then |
| 88 | + echo "Current tag $current_tag not found in the available tags." |
| 89 | + exit 1 |
| 90 | + fi |
| 91 | +
|
| 92 | + if [ "$current_tag_index" -lt $((${#tag_array[@]} - 1)) ]; then |
| 93 | + prev_tag="${tag_array[$((current_tag_index + 1))]}" |
| 94 | + else |
| 95 | + prev_tag=$(git rev-list --max-parents=0 HEAD) |
| 96 | + fi |
| 97 | +
|
| 98 | + last_commit=$(git rev-list -n 1 "$prev_tag") |
| 99 | +
|
| 100 | + # Fill the changelog file with the commits since the last tag |
| 101 | + # Set max tries to equal the maximum number of commits as protection |
| 102 | + max_tries=$(git rev-list --count HEAD) |
| 103 | + i=0 |
| 104 | + while [ "$(git rev-parse HEAD~$i)" != "$last_commit" ] && [ $i -lt $((max_tries-1)) ]; do |
| 105 | + commit_message=$(git show -s --format=%s HEAD~$i) |
| 106 | + echo "- $commit_message" >> CHANGELOG.md |
| 107 | + i=$((i+1)) |
| 108 | + done |
| 109 | +
|
| 110 | + # Set the complete changelog URL |
| 111 | + echo >> CHANGELOG.md |
| 112 | + compare="${prev_tag}...${current_tag}" |
| 113 | +
|
| 114 | + echo "Appending full changelog URL to CHANGELOG.md..." |
| 115 | + echo "**Full Changelog**: https://github.com/${{ github.repository }}/compare/${compare}" >> CHANGELOG.md |
| 116 | +
|
| 117 | + echo "Changelog built successfully." |
| 118 | +
|
| 119 | + - name: Create draft release |
| 120 | + uses: softprops/action-gh-release@v1 |
| 121 | + with: |
| 122 | + name: '@boxfish-studio/${{ github.ref_name }}' |
| 123 | + tag_name: ${{ github.ref_name }} |
| 124 | + body_path: CHANGELOG.md |
| 125 | + draft: true |
0 commit comments