-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub-release.sh
More file actions
191 lines (158 loc) · 5.98 KB
/
github-release.sh
File metadata and controls
191 lines (158 loc) · 5.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/bin/bash
###############################################################################
# github-release.sh
#
# A reusable Bash script to automate the release process for SDK repositories
# following the Gitflow branching model.
#
# Features:
# - Reads the version from a VERSION file.
# - Validates the version format (Semantic Versioning).
# - Ensures the working directory is clean.
# - Initiates a Gitflow release branch.
# - Generates and updates the CHANGELOG.md file.
# - Commits changelog changes.
# - Finishes the Gitflow release (merges into master and develop, tags the release).
# - Pushes commits and tags to the remote repository.
# - Creates a GitHub release using the GitHub CLI (`gh`).
#
# Usage:
# ./github-release.sh
#
# Configuration:
# - Customize the variables under the "Configuration" section as needed.
#
# Requirements:
# - Git installed and configured.
# - Gitflow extensions installed.
# - GitHub CLI (`gh`) installed and authenticated.
# - A `VERSION` file present at the repository root.
#
###############################################################################
# Exit immediately if a command exits with a non-zero status
set -e
###############################################################################
# Configuration
###############################################################################
# Repository Details
REPO_OWNER="wink-travel" # GitHub repository owner
REPO_NAME="trip-pay-python-sdk" # GitHub repository name
# Git Branch Names (Gitflow defaults)
MAIN_BRANCH="master" # Main branch name (e.g., master, main)
DEVELOP_BRANCH="develop" # Development branch name
# GitHub Release Details
GH_RELEASE_NOTES_FILE="CHANGELOG.md" # Path to the changelog or release notes file
# Version File
VERSION_FILE="VERSION" # Path to the VERSION file
# Tag Prefix
TAG_PREFIX="v" # Prefix for Git tags (e.g., 'v' for 'v1.0.0')
###############################################################################
# Functions
###############################################################################
# Function to display error messages
error() {
echo "Error: $1" >&2
exit 1
}
# Function to validate Semantic Versioning (e.g., 1.0.0)
validate_version() {
local version_regex="^[0-9]+\.[0-9]+\.[0-9]+(-[A-Za-z0-9.-]+)?(\+[A-Za-z0-9.-]+)?$"
if [[ ! "$1" =~ $version_regex ]]; then
error "Version '$1' does not follow Semantic Versioning (e.g., 1.0.0)."
fi
}
# Function to ensure the working directory is clean
ensure_clean_working_directory() {
if [[ -n $(git status --porcelain) ]]; then
error "Working directory is not clean. Please commit or stash your changes before releasing."
fi
}
# Function to read the version from the VERSION file
read_version() {
if [[ ! -f "$VERSION_FILE" ]]; then
error "VERSION file not found at '$VERSION_FILE'. Please ensure it exists."
fi
VERSION=$(cat "$VERSION_FILE")
echo "Current Version: $VERSION"
}
# Function to initialize Gitflow if not already initialized
initialize_gitflow() {
if ! git flow config > /dev/null 2>&1; then
echo "Initializing Gitflow..."
git flow init -d
echo "Gitflow initialized."
else
echo "Gitflow already initialized."
fi
}
# Function to start a Gitflow release
start_gitflow_release() {
echo "Starting Gitflow release for version $VERSION..."
git flow release start "$VERSION"
echo "Gitflow release 'release/$VERSION' started."
}
# Function to generate and update the CHANGELOG.md file
generate_changelog() {
echo "Generating changelog..."
npx git-changelog-command-line -of "$GH_RELEASE_NOTES_FILE"
echo "Changelog generated/updated successfully."
}
# Function to commit changelog changes on the release branch
commit_changelog() {
echo "Committing changelog changes..."
git add "$GH_RELEASE_NOTES_FILE"
git commit -m "chore: update CHANGELOG for release $VERSION" || echo "No changes to commit."
echo "Committed changelog changes."
}
# Function to finish a Gitflow release with automated merge messages
finish_gitflow_release() {
echo "Finishing Gitflow release..."
# Finish the Gitflow release with a predefined commit message
GIT_MERGE_AUTOEDIT=no git flow release finish -m "chore: release $TAG_PREFIX$VERSION" "$VERSION" || error "Failed to finish Gitflow release."
echo "Gitflow release '$VERSION' finished."
}
# Function to push commits and tags to remote
push_to_remote() {
echo "Pushing commits to remote repository..."
git push origin "$MAIN_BRANCH"
git push origin "$DEVELOP_BRANCH"
echo "Pushing Git tags to remote repository..."
git push origin --tags
echo "Pushed commits and tags to remote."
}
# Function to create a GitHub release using `gh`
create_github_release() {
if [[ ! -f "$GH_RELEASE_NOTES_FILE" ]]; then
error "Release notes file '$GH_RELEASE_NOTES_FILE' not found."
fi
echo "Creating GitHub release for tag '$TAG_PREFIX$VERSION'..."
gh release create "$TAG_PREFIX$VERSION" \
--title "Release $TAG_PREFIX$VERSION" \
--notes-file "$GH_RELEASE_NOTES_FILE" \
--target "$MAIN_BRANCH" || error "Failed to create GitHub release."
echo "GitHub release created successfully."
}
###############################################################################
# Main Script Execution
###############################################################################
echo "=== Gitflow-Based Release Process Initiated ==="
# Read and validate version
read_version
validate_version "$VERSION"
# Ensure the working directory is clean
ensure_clean_working_directory
# Initialize Gitflow if not already initialized
initialize_gitflow
# Start Gitflow release
start_gitflow_release
# Generate and commit the changelog
generate_changelog
commit_changelog
# Finish Gitflow release with automated merge messages
finish_gitflow_release
# Push commits and tags to remote repository
push_to_remote
# Create GitHub release
create_github_release
echo "=== Gitflow-Based Release Process Completed Successfully ==="
echo "Version $VERSION has been released."