github.com/grafana/synthetic-monitoring-agent@v0.64.2/scripts/release (about)

     1  #!/bin/sh
     2  
     3  set -e
     4  
     5  must_find_cmd() {
     6  	cmd=$1
     7  
     8  	if command -v "${cmd}" > /dev/null 2>&1 ; then
     9  		echo "${cmd}"
    10  		return
    11  	elif [ -x "$PWD/scripts/go/bin/${cmd}" ] ; then
    12  		echo "$PWD/scripts/go/bin/${cmd}"
    13  		return
    14  	else
    15  		echo "E: '${cmd}' is needed to run this script. Abort." >&2
    16  		exit 1
    17  	fi
    18  }
    19  
    20  git=$(must_find_cmd git)
    21  git_chglog=$(must_find_cmd git-chglog)
    22  
    23  next_version=$1
    24  
    25  cd "$("${git}" rev-parse --show-toplevel)" || exit 2
    26  
    27  if test ! -e CHANGELOG.md ; then
    28  	echo "E: Expecting a CHANGELOG.md file in $PWD, none found. Abort."
    29  	exit 3
    30  fi
    31  
    32  current_version() {
    33  	"${git}" describe --tags HEAD | cut -d- -f1 | tr -d v
    34  }
    35  
    36  next_version_patch() {
    37  	parts=$(current_version)
    38  	major=$(echo "${parts}" | cut -d. -f1)
    39  	minor=$(echo "${parts}" | cut -d. -f2)
    40  	patch=$(echo "${parts}" | cut -d. -f3)
    41  	echo "${major}.${minor}.$((patch+1))"
    42  }
    43  
    44  next_version_minor() {
    45  	parts=$(current_version)
    46  	major=$(echo "${parts}" | cut -d. -f1)
    47  	minor=$(echo "${parts}" | cut -d. -f2)
    48  	echo "${major}.$((minor+1)).0"
    49  }
    50  
    51  next_version_major() {
    52  	parts=$(current_version)
    53  	major=$(echo "${parts}" | cut -d. -f1)
    54  	echo "$((major+1)).0.0"
    55  }
    56  
    57  if test -z "${next_version}" ; then
    58  	cv=$(current_version)
    59  	next_patch=$(next_version_patch)
    60  	next_minor=$(next_version_minor)
    61  	next_major=$(next_version_major)
    62  	cat <<-EOT
    63  	I: Current version: v${cv}
    64  	I: Next fix: v${next_patch}
    65  	I: Next feature: v${next_minor}
    66  	I: Next breaking change: v${next_major}
    67  
    68  	I: Changes since current version:
    69  
    70  	EOT
    71  
    72  	"${git}" --no-pager log --pretty=tformat:'    %C(auto)%h %d %s' "v${cv}...HEAD"
    73  
    74  	cat <<-EOT
    75  
    76  	E: Next version argument required. Abort.
    77  	EOT
    78  	exit 4
    79  fi
    80  
    81  commit_msg=$(mktemp)
    82  
    83  cleanup() {
    84  	rm -f "${commit_msg}"
    85  }
    86  
    87  trap cleanup EXIT
    88  
    89  last_subject=$(git log --format=%s --max-count=1 | grep -E "^Release ${next_version}( \(#[0-9]+\))?\$" || true)
    90  
    91  if test -z "${last_subject}" ; then
    92  	# Need to create release notes.
    93  	cur_version=$("${git}" describe --tags | cut -d- -f1)
    94  
    95  	"${git_chglog}" --next-tag "${next_version}" > CHANGELOG.md
    96  
    97  	"${git}" add CHANGELOG.md
    98  
    99  	"${git}" switch --create "release-${next_version}"
   100  
   101  	cat > "${commit_msg}" <<-EOT
   102  	Release ${next_version}
   103  
   104  	$("${git}" log --oneline --reverse "${cur_version}".. | cut -d' ' -f2- | sed -e 's,^,* ,')
   105  	EOT
   106  
   107  	"${git}" commit --signoff --file="${commit_msg}"
   108  
   109  	cat <<-EOT
   110  
   111  	A commit has been created to update the CHANGELOG.md file and prepare a
   112  	release for version ${next_version}.
   113  
   114  	Please open a PR and once that is merged, run this script again with the
   115  	same version number as argument.
   116  	EOT
   117  else
   118  	# Need to create tag.
   119  	"${git}" show --pretty=format:"%B" --no-patch > "${commit_msg}"
   120  
   121  	"${git}" tag --annotate --file="${commit_msg}" "${next_version}"
   122  
   123  	cat <<-EOT
   124  	An annotated tag has been created for version ${next_version}.
   125  
   126  	You must push this tag to the remote repository in order to trigger the
   127  	release process.
   128  	EOT
   129  fi