|
| 1 | +#!/bin/bash |
| 2 | +# Copyright (C) 2017 SUSE LLC. |
| 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 | +set -e |
| 17 | + |
| 18 | +## ---> |
| 19 | +# Project-specific options and functions. In *theory* you shouldn't need to |
| 20 | +# touch anything else in this script in order to use this elsewhere. |
| 21 | +project="runc" |
| 22 | +root="$(readlink -f "$(dirname "${BASH_SOURCE}")/..")" |
| 23 | + |
| 24 | +# This function takes an output path as an argument, where the built |
| 25 | +# (preferably static) binary should be placed. |
| 26 | +function build_project() { |
| 27 | + builddir="$(dirname "$1")" |
| 28 | + |
| 29 | + # Build with all tags enabled. |
| 30 | + make -C "$root" COMMIT_NO= BUILDTAGS="seccomp selinux apparmor" static |
| 31 | + mv "$root/$project" "$1" |
| 32 | +} |
| 33 | + |
| 34 | +# End of the easy-to-configure portion. |
| 35 | +## <--- |
| 36 | + |
| 37 | +# Print usage information. |
| 38 | +function usage() { |
| 39 | + echo "usage: release.sh [-S <gpg-key-id>] [-c <commit-ish>] [-r <release-dir>] [-v <version>]" >&2 |
| 40 | + exit 1 |
| 41 | +} |
| 42 | + |
| 43 | +# Log something to stderr. |
| 44 | +function log() { |
| 45 | + echo "[*] $*" >&2 |
| 46 | +} |
| 47 | + |
| 48 | +# Log something to stderr and then exit with 0. |
| 49 | +function bail() { |
| 50 | + log "$@" |
| 51 | + exit 0 |
| 52 | +} |
| 53 | + |
| 54 | +# Conduct a sanity-check to make sure that GPG provided with the given |
| 55 | +# arguments can sign something. Inability to sign things is not a fatal error. |
| 56 | +function gpg_cansign() { |
| 57 | + gpg "$@" --clear-sign </dev/null >/dev/null |
| 58 | +} |
| 59 | + |
| 60 | +# When creating releases we need to build static binaries, an archive of the |
| 61 | +# current commit, and generate detached signatures for both. |
| 62 | +keyid="" |
| 63 | +commit="HEAD" |
| 64 | +version="" |
| 65 | +releasedir="" |
| 66 | +hashcmd="" |
| 67 | +while getopts "S:c:r:v:h:" opt; do |
| 68 | + case "$opt" in |
| 69 | + S) |
| 70 | + keyid="$OPTARG" |
| 71 | + ;; |
| 72 | + c) |
| 73 | + commit="$OPTARG" |
| 74 | + ;; |
| 75 | + r) |
| 76 | + releasedir="$OPTARG" |
| 77 | + ;; |
| 78 | + v) |
| 79 | + version="$OPTARG" |
| 80 | + ;; |
| 81 | + h) |
| 82 | + hashcmd="$OPTARG" |
| 83 | + ;; |
| 84 | + \:) |
| 85 | + echo "Missing argument: -$OPTARG" >&2 |
| 86 | + usage |
| 87 | + ;; |
| 88 | + \?) |
| 89 | + echo "Invalid option: -$OPTARG" >&2 |
| 90 | + usage |
| 91 | + ;; |
| 92 | + esac |
| 93 | +done |
| 94 | + |
| 95 | +version="${version:-$(<"$root/VERSION")}" |
| 96 | +releasedir="${releasedir:-release/$version}" |
| 97 | +hashcmd="${hashcmd:-sha256sum}" |
| 98 | +goarch="$(go env GOARCH || echo "amd64")" |
| 99 | + |
| 100 | +log "creating $project release in '$releasedir'" |
| 101 | +log " version: $version" |
| 102 | +log " commit: $commit" |
| 103 | +log " key: ${keyid:-DEFAULT}" |
| 104 | +log " hash: $hashcmd" |
| 105 | + |
| 106 | +# Make explicit what we're doing. |
| 107 | +set -x |
| 108 | + |
| 109 | +# Make the release directory. |
| 110 | +rm -rf "$releasedir" && mkdir -p "$releasedir" |
| 111 | + |
| 112 | +# Build project. |
| 113 | +build_project "$releasedir/$project.$goarch" |
| 114 | + |
| 115 | +# Generate new archive. |
| 116 | +git archive --format=tar --prefix="$project-$version/" "$commit" | xz > "$releasedir/$project.tar.xz" |
| 117 | + |
| 118 | +# Generate sha256 checksums for both. |
| 119 | +( cd "$releasedir" ; "$hashcmd" "$project".{"$goarch",tar.xz} > "$project.$hashcmd" ; ) |
| 120 | + |
| 121 | +# Set up the gpgflags. |
| 122 | +[[ "$keyid" ]] && export gpgflags="--default-key $keyid" |
| 123 | +gpg_cansign $gpgflags || bail "Could not find suitable GPG key, skipping signing step." |
| 124 | + |
| 125 | +# Sign everything. |
| 126 | +gpg $gpgflags --detach-sign --armor "$releasedir/$project.$goarch" |
| 127 | +gpg $gpgflags --detach-sign --armor "$releasedir/$project.tar.xz" |
| 128 | +gpg $gpgflags --clear-sign --armor \ |
| 129 | + --output "$releasedir/$project.$hashcmd"{.tmp,} && \ |
| 130 | + mv "$releasedir/$project.$hashcmd"{.tmp,} |
0 commit comments