|
| 1 | +#! /usr/bin/env bash |
| 2 | +# Copyright 2021 The Microshift authors |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# |
| 16 | +# release.sh |
| 17 | +# This helper script generates and publishes Microshift releases for a given git ref. This is done by checking out the |
| 18 | +# git ref and cross-compiling to architecture specific image digests. Images are composed from the multistage container |
| 19 | +# file stored in ./images/build/Dockerfile, with the release images being layered on top of |
| 20 | +# registry.access.redhat.com/ubi8/ubi-minimal:8.4. Images are wrapped with a container manifest and pushed to |
| 21 | +# quay.io/microshift/microshift. A github release and a tag are created and identified with the version generated |
| 22 | +# by the Makefile. Cross-compiled binaries are copied from the container images and published in the git release. |
| 23 | + |
| 24 | +set -euo pipefail |
| 25 | +shopt -s expand_aliases |
| 26 | + |
| 27 | +# debugging options |
| 28 | +#trap 'echo "# $BASH_COMMAND"' DEBUG |
| 29 | +#set -x |
| 30 | + |
| 31 | +######## |
| 32 | +# INIT # |
| 33 | +######## |
| 34 | +ROOT="$(readlink -f "$(dirname "${BASH_SOURCE[0]}")/../")" |
| 35 | + |
| 36 | +# Check for a container manager cli (podman || docker), and alias it to "podman", since |
| 37 | +# they implement the same cli interface. |
| 38 | +__ctr_mgr_alias=$({ which podman &>/dev/null && echo "podman"; } || { which docker &>/dev/null && echo "docker"; } || echo "") |
| 39 | +alias podman="${__ctr_mgr_alias:?"a container manager (podman || docker) is required as part of the release automation; none found"}" |
| 40 | + |
| 41 | +######### |
| 42 | +# FUNCS # |
| 43 | +######### |
| 44 | + |
| 45 | +help() { |
| 46 | + printf 'Microshift: release.sh |
| 47 | +This script provides some simple automation for cutting new releases of Microshift. |
| 48 | +
|
| 49 | +Use: |
| 50 | + ./release.sh --token $(cat /token/path) |
| 51 | + Note: do not use "=" with flag values |
| 52 | +Inputs: |
| 53 | + --token (Required) The github application auth token, use to create a github release. |
| 54 | + --debug, -d Print generated script values for debugging. |
| 55 | + --help, -h Print this help text. |
| 56 | +Outputs: |
| 57 | +- A version, formatted as 4.7.0-0.microshift-YYYY-MM-DD-HHMMSS, is applied as a git tag and pushed to the repo |
| 58 | +- Multi-architecture container manifest, tagged as `quay.io/microshift/microshift:$VERSION` and `:latest` |
| 59 | +- Cross-compiled binaries |
| 60 | +- A sha256 checksum file, containing the checksums for all binary artifacts |
| 61 | +- A github release, containing the binary artifacts and checksum file. |
| 62 | +
|
| 63 | +DEBUG |
| 64 | +To test releases against a downstream/fork repository, override GIT_OWNER to forked git org/owner and QUAY_OWNER to your |
| 65 | +quay.io owner or org. |
| 66 | +
|
| 67 | + e.g. GIT_OWNER=my_repo QUAY_OWNER=my_quay_repo ./release.sh --token $(cat /token/path |
| 68 | +' |
| 69 | +} |
| 70 | + |
| 71 | +generate_api_release_request() { |
| 72 | + local is_prerelease="${1:=true}" # (copejon) assume for now that all releases are prerelease, unless otherwise specified |
| 73 | + printf '{"tag_name": "%s","name": "%s","prerelease": %s}' "$VERSION" "$VERSION" "$is_prerelease" |
| 74 | +} |
| 75 | + |
| 76 | +git_create_release() { |
| 77 | + local data="$1" |
| 78 | + local response |
| 79 | + response="$( |
| 80 | + curl -X POST \ |
| 81 | + -H "Accept: application/vnd.github.v3+json" \ |
| 82 | + -H "Authorization: token $TOKEN" \ |
| 83 | + "https://api.github.com/repos/$GIT_OWNER/microshift/releases" \ |
| 84 | + -d "${data[@]}" |
| 85 | + )" |
| 86 | + local raw_upload_url |
| 87 | + raw_upload_url="$(echo "$response" | grep "upload_url")" |
| 88 | + local upload_url |
| 89 | + upload_url=$(echo "$raw_upload_url" | sed -n 's,.*\(https://uploads.github.com/repos/'$GIT_OWNER'/microshift/releases/[0-9a-zA-Z]*/assets\).*,\1,p') |
| 90 | + # curl will return 0 even on 4xx http errors, so verify that the actually got an up_load url |
| 91 | + [ -z "$upload_url" ] && return 1 |
| 92 | + echo "$upload_url" |
| 93 | +} |
| 94 | + |
| 95 | +git_post() { |
| 96 | + local bin_file="$1" |
| 97 | + local upload_url="$2" |
| 98 | + local mime_type |
| 99 | + mime_type="$(file -b --mime-type "$bin_file")" |
| 100 | + curl --fail-early \ |
| 101 | + -X POST \ |
| 102 | + -H "Accept: application/vnd.github.v3" \ |
| 103 | + -H "Authorization: token $TOKEN" \ |
| 104 | + -H "Content-Type: $mime_type" \ |
| 105 | + --data-binary @"$bin_file" \ |
| 106 | + "$upload_url"?name="$(basename $bin_file)" |
| 107 | +} |
| 108 | + |
| 109 | +git_post_artifacts() { |
| 110 | + local asset_dir="$1" |
| 111 | + local upload_url="$2" |
| 112 | + local files |
| 113 | + files="$(ls "$asset_dir")" |
| 114 | + for f in $files; do |
| 115 | + git_post "$asset_dir/$f" "$upload_url" |
| 116 | + done |
| 117 | +} |
| 118 | + |
| 119 | +prep_stage_area() { |
| 120 | + local asset_dir |
| 121 | + asset_dir=$(mktemp -d -p "$STAGING_DIR/") |
| 122 | + echo "$asset_dir" |
| 123 | +} |
| 124 | + |
| 125 | +extract_release_image_binary() { |
| 126 | + local tag="$1" |
| 127 | + local dest="$2" |
| 128 | + local out_bin="$dest"/microshift-"${tag#*"$VERSION-"}" |
| 129 | + podman cp "$(podman create "$tag")":/usr/bin/microshift "$out_bin" >&2 |
| 130 | + echo "$out_bin" |
| 131 | +} |
| 132 | + |
| 133 | +stage_release_image_binaries() { |
| 134 | + local dest |
| 135 | + dest="$(prep_stage_area)" |
| 136 | + for t in "${RELEASE_IMAGE_TAGS[@]}"; do |
| 137 | + local out_bin |
| 138 | + out_bin=$(extract_release_image_binary "$t" "$dest") || return 1 |
| 139 | + ( |
| 140 | + cd "$dest" |
| 141 | + sha256sum "$(basename "$out_bin")" >>"$dest"/release.sha256 |
| 142 | + ) || return 1 |
| 143 | + done |
| 144 | + echo "$dest" |
| 145 | +} |
| 146 | + |
| 147 | +build_container_images_artifacts() { |
| 148 | + ( |
| 149 | + cd "$ROOT" |
| 150 | + make build-containerized-cross-build SOURCE_GIT_TAG="$VERSION" IMAGE_REPO="$IMAGE_REPO" |
| 151 | + ) || return 1 |
| 152 | +} |
| 153 | + |
| 154 | +push_container_image_artifacts() { |
| 155 | + for t in "${RELEASE_IMAGE_TAGS[@]}"; do |
| 156 | + podman push "$t" |
| 157 | + done |
| 158 | +} |
| 159 | + |
| 160 | +podman_create_manifest(){ |
| 161 | + podman manifest create "$IMAGE_REPO:$VERSION" >&2 |
| 162 | + for ref in "${RELEASE_IMAGE_TAGS[@]}"; do |
| 163 | + podman manifest add "$IMAGE_REPO:$VERSION" "docker://$ref" |
| 164 | + done |
| 165 | + podman manifest push "$IMAGE_REPO:$VERSION" "$IMAGE_REPO:$VERSION" |
| 166 | + podman manifest push "$IMAGE_REPO:$VERSION" "$IMAGE_REPO:latest" |
| 167 | +} |
| 168 | + |
| 169 | +docker_create_manifest(){ |
| 170 | + local amend_images_options |
| 171 | + for image in "${RELEASE_IMAGE_TAGS[@]}"; do |
| 172 | + amend_images_options+="--amend $image" |
| 173 | + done |
| 174 | + # use docker cli directly for clarity, as this is a docker-only func |
| 175 | + docker manifest create "$IMAGE_REPO:$VERSION" "${RELEASE_IMAGE_TAGS[@]}" >&2 |
| 176 | + docker tag "$IMAGE_REPO:$VERSION" "$IMAGE_REPO:latest" |
| 177 | + docker manifest push "$IMAGE_REPO:$VERSION" |
| 178 | + docker manifest push "$IMAGE_REPO:latest" |
| 179 | +} |
| 180 | + |
| 181 | +push_container_manifest() { |
| 182 | + local cli="$(alias podman)" |
| 183 | + if [[ "${cli#*=}" =~ docker ]]; then |
| 184 | + docker_create_manifest |
| 185 | + else |
| 186 | + podman_create_manifest |
| 187 | + fi |
| 188 | + |
| 189 | +} |
| 190 | +debug() { |
| 191 | + local version="$1" |
| 192 | + local api_request="$2" |
| 193 | + printf "Git Target: %s\n" "$TARGET" |
| 194 | + printf "Image Artifact: %s\n" "$IMAGE_REPO:$VERSION" |
| 195 | + printf "generate_version: %s\n" "$version" |
| 196 | + printf "compose_release_request: %s\n" "$api_request" |
| 197 | +} |
| 198 | + |
| 199 | +######## |
| 200 | +# MAIN # |
| 201 | +######## |
| 202 | +while [ $# -gt 0 ]; do |
| 203 | + case "$1" in |
| 204 | + "--token") |
| 205 | + TOKEN="${2:-}" |
| 206 | + [[ "$TOKEN" =~ ^-.* ]] || [[ -z "$TOKEN" ]] && { |
| 207 | + printf "flag $1 git release API calls require robot token" |
| 208 | + exit 1 |
| 209 | + } |
| 210 | + shift 2 |
| 211 | + ;; |
| 212 | + "--version") |
| 213 | + VERSION="${2:-}" |
| 214 | + [[ "$VERSION" =~ ^-.* ]] || [[ -z "$VERSION" ]] && { |
| 215 | + printf "flag $1 expects a version input value" |
| 216 | + exit 1 |
| 217 | + } |
| 218 | + shift 2 |
| 219 | + ;; |
| 220 | + "-h" | "--help") |
| 221 | + help && exit |
| 222 | + ;; |
| 223 | + *) |
| 224 | + echo "unknown input: $1" && help && exit 1 |
| 225 | + ;; |
| 226 | + esac |
| 227 | +done |
| 228 | + |
| 229 | +printf "Using container manager: %s\n" "$(podman --version)" |
| 230 | + |
| 231 | +#### Debugging Vars. For generating a full release to a fork, set to your own git/quay owner. |
| 232 | +GIT_OWNER=${GIT_OWNER:="redhat-et"} |
| 233 | +QUAY_OWNER=${QUAY_OWNER:="microshift"} |
| 234 | +#### |
| 235 | + |
| 236 | +# Generate data early for debugging |
| 237 | +API_DATA="$(generate_api_release_request "true")" # leave body empty for now |
| 238 | + |
| 239 | +IMAGE_REPO="quay.io/$QUAY_OWNER/microshift" |
| 240 | +RELEASE_IMAGE_TAGS=("$IMAGE_REPO:$VERSION-linux-amd64" "$IMAGE_REPO:$VERSION-linux-arm64" ) |
| 241 | + |
| 242 | +STAGING_DIR="$ROOT/_output/staging" |
| 243 | +mkdir -p "$STAGING_DIR" |
| 244 | + |
| 245 | +build_container_images_artifacts || exit 1 |
| 246 | +STAGE_DIR=$(stage_release_image_binaries) || exit 1 |
| 247 | +push_container_image_artifacts || exit 1 |
| 248 | +push_container_manifest || exit 1 |
| 249 | +UPLOAD_URL="$(git_create_release "$API_DATA" "$TOKEN")" || exit 1 |
| 250 | +git_post_artifacts "$STAGE_DIR" "$UPLOAD_URL" "$TOKEN" || exit 1 |
0 commit comments