1
+ name : Create Tag and Release
2
+
3
+ on :
4
+ workflow_dispatch :
5
+ inputs :
6
+ version_type :
7
+ description : " The type of version you want to release"
8
+ type : choice
9
+ options :
10
+ - patch
11
+ - minor
12
+ - major
13
+ default : ' minor'
14
+ required : false
15
+
16
+ jobs :
17
+ check-permission :
18
+ runs-on : ubuntu-latest
19
+ outputs :
20
+ is-admin : ${{ steps.check-admin.outputs.is_admin }}
21
+ steps :
22
+ - name : Check if user is admin
23
+ id : check-admin
24
+ run : |
25
+ IS_ADMIN=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
26
+ "https://api.github.com/repos/${{ github.repository }}/collaborators/${{ github.actor }}/permission" | \
27
+ jq -r '.permission == "admin"')
28
+ echo "is_admin=$IS_ADMIN" >> $GITHUB_OUTPUT
29
+ echo "User ${{ github.actor }} is admin: $IS_ADMIN"
30
+
31
+ create-release :
32
+ needs : check-permission
33
+ if : needs.check-permission.outputs.is-admin == 'true'
34
+ runs-on : ubuntu-latest
35
+ steps :
36
+ - name : Checkout code
37
+ uses : actions/checkout@v3
38
+ with :
39
+ fetch-depth : 0
40
+
41
+ - name : Get latest tag
42
+ id : get_latest_tag
43
+ run : |
44
+ # Ensure we fetch all tags from all remotes
45
+ git fetch --tags --force
46
+
47
+ # List all tags, sort by version number, and get the latest
48
+ LATEST_TAG=$(git tag -l "v*" | sort -V | tail -n 1)
49
+ if [ -z "$LATEST_TAG" ]; then
50
+ LATEST_TAG="v0.0.0"
51
+ fi
52
+
53
+ echo "latest_tag=$LATEST_TAG" >> $GITHUB_OUTPUT
54
+ echo "Latest tag: $LATEST_TAG"
55
+
56
+ - name : Calculate new version
57
+ id : calculate_version
58
+ run : |
59
+ LATEST_TAG="${{ steps.get_latest_tag.outputs.latest_tag }}"
60
+ # Remove 'v' prefix if present
61
+ VERSION=${LATEST_TAG#v}
62
+
63
+ # Split version into components
64
+ IFS='.' read -r -a VERSION_PARTS <<< "$VERSION"
65
+ MAJOR=${VERSION_PARTS[0]}
66
+ MINOR=${VERSION_PARTS[1]}
67
+ PATCH=${VERSION_PARTS[2]}
68
+
69
+ # Increment based on version type
70
+ case "${{ github.event.inputs.version_type }}" in
71
+ major)
72
+ MAJOR=$((MAJOR + 1))
73
+ MINOR=0
74
+ PATCH=0
75
+ ;;
76
+ minor)
77
+ MINOR=$((MINOR + 1))
78
+ PATCH=0
79
+ ;;
80
+ patch)
81
+ PATCH=$((PATCH + 1))
82
+ ;;
83
+ esac
84
+
85
+ NEW_VERSION="v$MAJOR.$MINOR.$PATCH"
86
+ echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT
87
+ echo "New version: $NEW_VERSION"
88
+
89
+ - name : Create tag
90
+ run : |
91
+ git config --local user.email "github-actions[bot]@users.noreply.github.com"
92
+ git config --local user.name "github-actions[bot]"
93
+
94
+ # Fetch all tags to ensure we have the latest information
95
+ git fetch --tags
96
+
97
+ # Check if tag already exists
98
+ if git rev-parse --verify --quiet "refs/tags/${{ steps.calculate_version.outputs.new_version }}" >/dev/null; then
99
+ echo "Tag ${{ steps.calculate_version.outputs.new_version }} already exists, skipping tag creation"
100
+ else
101
+ echo "Creating tag ${{ steps.calculate_version.outputs.new_version }}"
102
+ git tag ${{ steps.calculate_version.outputs.new_version }}
103
+ git push origin ${{ steps.calculate_version.outputs.new_version }}
104
+ fi
105
+
106
+ - name : Create release
107
+ run : |
108
+ # Check if release already exists
109
+ if gh release view ${{ steps.calculate_version.outputs.new_version }} &>/dev/null; then
110
+ echo "Release ${{ steps.calculate_version.outputs.new_version }} already exists, skipping release creation"
111
+ else
112
+ echo "Creating release ${{ steps.calculate_version.outputs.new_version }}"
113
+ gh release create ${{ steps.calculate_version.outputs.new_version }} \
114
+ --generate-notes \
115
+ --title "Release ${{ steps.calculate_version.outputs.new_version }}" \
116
+ --draft
117
+ fi
118
+ env :
119
+ GH_TOKEN : ${{ github.token }}
120
+
121
+ unauthorized :
122
+ needs : check-permission
123
+ if : needs.check-permission.outputs.is-admin != 'true'
124
+ runs-on : ubuntu-latest
125
+ steps :
126
+ - name : Unauthorized message
127
+ run : |
128
+ echo "Error: Only repository administrators can create releases"
129
+ exit 1
0 commit comments