| 1 | #!/bin/sh |
| 2 | |
| 3 | set -e |
| 4 | |
| 5 | REPO="${1}" |
| 6 | EVENT_NAME="${2}" |
| 7 | EVENT_TYPE="${3}" |
| 8 | EVENT_VERSION="${4}" |
| 9 | RELEASE_TEST="${5}" |
| 10 | |
| 11 | ############################################################## |
| 12 | # Version validation functions |
| 13 | |
| 14 | check_version_format() { |
| 15 | if ! echo "${EVENT_VERSION}" | grep -qE '^v[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+$'; then |
| 16 | echo "::error::The supplied version (${EVENT_VERSION}) is not a valid version string." |
| 17 | return 1 |
| 18 | fi |
| 19 | } |
| 20 | |
| 21 | patch_is_zero() { |
| 22 | if ! echo "${EVENT_VERSION}" | grep -qE '^v[[:digit:]]+\.[[:digit:]]+\.0$'; then |
| 23 | echo "::error::The patch number for a ${EVENT_TYPE} build must be 0." |
| 24 | return 1 |
| 25 | fi |
| 26 | } |
| 27 | |
| 28 | minor_is_zero() { |
| 29 | if ! echo "${EVENT_VERSION}" | grep -qE '^v[[:digit:]]+\.0'; then |
| 30 | echo "::error::The minor version number for a ${EVENT_TYPE} build must be 0." |
| 31 | return 1 |
| 32 | fi |
| 33 | } |
| 34 | |
| 35 | major_matches() { |
| 36 | current_major="$(cut -f 1 -d '-' packaging/version | cut -f 1 -d '.' | cut -f 2 -d 'v')" |
| 37 | target_major="$(echo "${EVENT_VERSION}" | cut -f 1 -d '.' | cut -f 2 -d 'v')" |
| 38 | |
| 39 | if [ "${target_major}" != "${current_major}" ]; then |
| 40 | echo "::error::Major version mismatch, expected ${current_major} but got ${target_major}." |
| 41 | return 1 |
| 42 | fi |
| 43 | } |
| 44 | |
| 45 | minor_matches() { |
| 46 | current_minor="$(cut -f 1 -d '-' packaging/version | cut -f 2 -d '.')" |
| 47 | target_minor="$(echo "${EVENT_VERSION}" | cut -f 2 -d '.')" |
| 48 | |
| 49 | if [ "${target_minor}" != "${current_minor}" ]; then |
| 50 | echo "::error::Minor version mismatch, expected ${current_minor} but got ${target_minor}." |
| 51 | return 1 |
| 52 | fi |
| 53 | } |
| 54 | |
| 55 | check_for_existing_tag() { |
| 56 | if git tag | grep -qE "^${EVENT_VERSION}$"; then |
| 57 | echo "::error::A tag for version ${EVENT_VERSION} already exists." |
| 58 | return 1 |
| 59 | fi |
| 60 | } |
| 61 | |
| 62 | check_newer_major_version() { |
| 63 | current="$(cut -f 1 -d '-' packaging/version | cut -f 1 -d '.' | cut -f 2 -d 'v')" |
| 64 | target="$(echo "${EVENT_VERSION}" | cut -f 1 -d '.' | cut -f 2 -d 'v')" |
| 65 | |
| 66 | if [ "${target}" -le "${current}" ]; then |
| 67 | echo "::error::Version ${EVENT_VERSION} is not newer than the current version." |
| 68 | return 1 |
| 69 | fi |
| 70 | } |
| 71 | |
| 72 | check_newer_minor_version() { |
| 73 | current="$(cut -f 1 -d '-' packaging/version | cut -f 2 -d '.')" |
| 74 | target="$(echo "${EVENT_VERSION}" | cut -f 2 -d '.')" |
| 75 | |
| 76 | if [ "${target}" -le "${current}" ]; then |
| 77 | echo "::error::Version ${EVENT_VERSION} is not newer than the current version." |
| 78 | return 1 |
| 79 | fi |
| 80 | } |
| 81 | |
| 82 | check_newer_patch_version() { |
| 83 | current="$(cut -f 1 -d '-' packaging/version | cut -f 3 -d '.')" |
| 84 | target="$(echo "${EVENT_VERSION}" | cut -f 3 -d '.')" |
| 85 | |
| 86 | if [ "${target}" -le "${current}" ]; then |
| 87 | echo "::error::Version ${EVENT_VERSION} is not newer than the current version." |
| 88 | return 1 |
| 89 | fi |
| 90 | } |
| 91 | |
| 92 | ############################################################## |
| 93 | # Core logic |
| 94 | |
| 95 | git config user.name "netdatabot" |
| 96 | git config user.email "bot@netdata.cloud" |
| 97 | |
| 98 | if [ "${REPO}" != "netdata/netdata" ] && [ -z "${RELEASE_TEST}" ]; then |
| 99 | echo "::notice::Not running in the netdata/netdata repository, not queueing a release build." |
| 100 | echo "run=false" >> "${GITHUB_OUTPUT}" |
| 101 | elif [ "${EVENT_NAME}" = 'schedule' ] || [ "${EVENT_TYPE}" = 'nightly' ]; then |
| 102 | echo "::notice::Preparing a nightly release build." |
| 103 | LAST_TAG=$(git describe --abbrev=0 --tags) |
| 104 | COMMITS_SINCE_RELEASE=$(git rev-list "${LAST_TAG}"..HEAD --count) |
| 105 | NEW_VERSION="${LAST_TAG}-$((COMMITS_SINCE_RELEASE + 1))-nightly" |
| 106 | LAST_VERSION_COMMIT="$(git rev-list -1 HEAD packaging/version)" |
| 107 | HEAD_COMMIT="$(git rev-parse HEAD)" |
| 108 | if [ "${EVENT_NAME}" = 'schedule' ] && [ "${LAST_VERSION_COMMIT}" = "${HEAD_COMMIT}" ] && grep -qE '.*-nightly$' packaging/version; then |
| 109 | echo "::notice::No commits since last nightly build, not publishing a new nightly build." |
| 110 | echo "run=false" >> "${GITHUB_OUTPUT}" |
| 111 | else |
| 112 | echo "${NEW_VERSION}" > packaging/version || exit 1 |
| 113 | # shellcheck disable=SC2129 |
| 114 | echo "run=true" >> "${GITHUB_OUTPUT}" |
| 115 | echo "message=Update changelog and version for nightly build: ${NEW_VERSION}." >> "${GITHUB_OUTPUT}" |
| 116 | echo "ref=master" >> "${GITHUB_OUTPUT}" |
| 117 | echo "type=nightly" >> "${GITHUB_OUTPUT}" |
| 118 | echo "branch=master" >> "${GITHUB_OUTPUT}" |
| 119 | echo "version=nightly" >> "${GITHUB_OUTPUT}" |
| 120 | fi |
| 121 | elif [ "${EVENT_TYPE}" = 'patch' ] && [ "${EVENT_VERSION}" != "nightly" ]; then |
| 122 | echo "::notice::Preparing a patch release build." |
| 123 | check_version_format || exit 1 |
| 124 | check_for_existing_tag || exit 1 |
| 125 | branch_name="$(echo "${EVENT_VERSION}" | cut -f 1-2 -d '.')" |
| 126 | if ! git checkout "${branch_name}"; then |
| 127 | echo "::error::Could not find a branch for the ${branch_name}.x release series." |
| 128 | exit 1 |
| 129 | fi |
| 130 | minor_matches || exit 1 |
| 131 | major_matches || exit 1 |
| 132 | check_newer_patch_version || exit 1 |
| 133 | echo "${EVENT_VERSION}" > packaging/version || exit 1 |
| 134 | # shellcheck disable=SC2129 |
| 135 | echo "run=true" >> "${GITHUB_OUTPUT}" |
| 136 | echo "message=Patch release ${EVENT_VERSION}." >> "${GITHUB_OUTPUT}" |
| 137 | echo "ref=${EVENT_VERSION}" >> "${GITHUB_OUTPUT}" |
| 138 | echo "type=release" >> "${GITHUB_OUTPUT}" |
| 139 | echo "branch=${branch_name}" >> "${GITHUB_OUTPUT}" |
| 140 | echo "version=$(tr -d 'v' < packaging/version)" >> "${GITHUB_OUTPUT}" |
| 141 | elif [ "${EVENT_TYPE}" = 'minor' ] && [ "${EVENT_VERSION}" != "nightly" ]; then |
| 142 | echo "::notice::Preparing a minor release build." |
| 143 | check_version_format || exit 1 |
| 144 | patch_is_zero || exit 1 |
| 145 | major_matches || exit 1 |
| 146 | check_newer_minor_version || exit 1 |
| 147 | check_for_existing_tag || exit 1 |
| 148 | branch_name="$(echo "${EVENT_VERSION}" | cut -f 1-2 -d '.')" |
| 149 | if [ -n "$(git branch --list "${branch_name}")" ]; then |
| 150 | echo "::error::A branch named ${branch_name} already exists in the repository." |
| 151 | exit 1 |
| 152 | fi |
| 153 | echo "${EVENT_VERSION}" > packaging/version || exit 1 |
| 154 | # shellcheck disable=SC2129 |
| 155 | echo "run=true" >> "${GITHUB_OUTPUT}" |
| 156 | echo "message=Minor release ${EVENT_VERSION}." >> "${GITHUB_OUTPUT}" |
| 157 | echo "ref=${EVENT_VERSION}" >> "${GITHUB_OUTPUT}" |
| 158 | echo "type=release" >> "${GITHUB_OUTPUT}" |
| 159 | echo "branch=master" >> "${GITHUB_OUTPUT}" |
| 160 | echo "new-branch=${branch_name}" >> "${GITHUB_OUTPUT}" |
| 161 | echo "version=$(tr -d 'v' < packaging/version)" >> "${GITHUB_OUTPUT}" |
| 162 | elif [ "${EVENT_TYPE}" = 'major' ] && [ "${EVENT_VERSION}" != "nightly" ]; then |
| 163 | echo "::notice::Preparing a major release build." |
| 164 | check_version_format || exit 1 |
| 165 | minor_is_zero || exit 1 |
| 166 | patch_is_zero || exit 1 |
| 167 | check_newer_major_version || exit 1 |
| 168 | check_for_existing_tag || exit 1 |
| 169 | branch_name="$(echo "${EVENT_VERSION}" | cut -f 1-2 -d '.')" |
| 170 | if [ -n "$(git branch --list "${branch_name}")" ]; then |
| 171 | echo "::error::A branch named ${branch_name} already exists in the repository." |
| 172 | exit 1 |
| 173 | fi |
| 174 | echo "${EVENT_VERSION}" > packaging/version || exit 1 |
| 175 | # shellcheck disable=SC2129 |
| 176 | echo "run=true" >> "${GITHUB_OUTPUT}" |
| 177 | echo "message=Major release ${EVENT_VERSION}" >> "${GITHUB_OUTPUT}" |
| 178 | echo "ref=${EVENT_VERSION}" >> "${GITHUB_OUTPUT}" |
| 179 | echo "type=release" >> "${GITHUB_OUTPUT}" |
| 180 | echo "branch=master" >> "${GITHUB_OUTPUT}" |
| 181 | echo "new-branch=${branch_name}" >> "${GITHUB_OUTPUT}" |
| 182 | echo "version=$(tr -d 'v' < packaging/version)" >> "${GITHUB_OUTPUT}" |
| 183 | else |
| 184 | echo '::error::Unrecognized release type or invalid version.' |
| 185 | exit 1 |
| 186 | fi |