about summary refs log tree commit diff
path: root/build-release.sh
blob: 0109cace73f2d8b8a3bfbd34f7a4ec05da903b11 (plain) (blame)
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
#!/bin/bash
set -ueo pipefail

readonly GIT_HASH="$(git rev-parse --short HEAD)"
readonly LDFLAGS="-X main.gitHash=${GIT_HASH} -w -s"
readonly VERSION="1.1.0-${GIT_HASH}"

function binary-name() {
    local os="${1}"
    local target="${2}"
    if [ "${os}" = "windows" ]; then
        echo -n "${target}/kontemplate.exe"
    else
        echo -n "${target}/kontemplate"
    fi
}

function build-for() {
    local os="${1}"
    local arch="${2}"
    local target="release/${os}/${arch}"
    local bin=$(binary-name "${os}" "${target}")

    echo "Building kontemplate for ${os}-${arch} in ${target}"

    mkdir -p "${target}"

    env GOOS="${os}" GOARCH="${arch}" go build \
        -ldflags "${LDFLAGS}" \
        -o "${bin}" \
        -tags netgo
}

function sign-for() {
    local os="${1}"
    local arch="${2}"
    local target="release/${os}/${arch}"
    local bin="${target}/kontemplate"
    local hash="$(sha256sum ${bin})"
    local tar="release/kontemplate-${VERSION}-${os}-${arch}.tar.gz"

    echo "Signing kontemplate binary for ${os}-${arch} with SHA256 ${hash}"
    gpg --sign "${bin}"

    echo "Packing release into ${tar}"
    tar czvf "${tar}" -C "${target}" kontemplate kontemplate.gpg
}

case "${1}" in
    "build")
        # Build releases for various operating systems:
        build-for "linux" "amd64"
        build-for "darwin" "amd64"
        build-for "windows" "amd64"
        build-for "freebsd" "amd64"
        exit 0
        ;;
    "sign")
        # Sign releases:
        sign-for "linux" "amd64"
        sign-for "darwin" "amd64"
        sign-for "windows" "amd64"
        sign-for "freebsd" "amd64"
        exit 0
        ;;
esac