gitlab-build-images/scripts/install-golang

54 lines
1.8 KiB
Bash
Executable file

#!/bin/bash
set -xeou pipefail
INSTALL_GOLANG_VERSION=${1}
GOLANG_DOWNLOAD_SHA256=${2}
INSTALL_GOLANG_FIPS_TAG=${3}
GOLANG_DOWNLOAD_URL="https://golang.org/dl/go${INSTALL_GOLANG_VERSION}.linux-${TARGETARCH:-amd64}.tar.gz"
function build_debian() {
/scripts/download-file golang.tar.gz "$GOLANG_DOWNLOAD_URL" $GOLANG_DOWNLOAD_SHA256
tar -C /usr/local -xzf golang.tar.gz
rm golang.tar.gz
}
function build_ubi() {
mkdir -p /tmp/golang
curl -fsSL "$GOLANG_DOWNLOAD_URL" -o golang.tar.gz
echo "${GOLANG_DOWNLOAD_SHA256} golang.tar.gz" | sha256sum -c -
tar -C /tmp/golang -xzf golang.tar.gz
# For UBI, we will be installing golang-fips
GO_TAG="go${INSTALL_GOLANG_FIPS_TAG}-openssl-fips"
git clone https://github.com/golang-fips/go.git --branch "${GO_TAG}" --single-branch --depth 1 /tmp/golang-fips
cd /tmp/golang-fips
# The initialize script ends with a commit, so we need to set the user info. And needs to be global due to submodules in use.
git config --global user.email "builder@example.com"
git config --global user.name "Builder"
PATH=$PATH:/tmp/golang/go/bin ./scripts/full-initialize-repo.sh
cd /tmp/golang-fips/go/src
PATH=$PATH:/tmp/golang/go/bin GOROOT_FINAL=/usr/local/go CGO_ENABLED=1 ./make.bash
mv /tmp/golang-fips/go /usr/local/go
rm -rf /usr/local/go/pkg/*/cmd /usr/local/go/pkg/bootstrap \
/usr/local/go/pkg/obj /usr/local/go/pkg/tool/*/api \
/usr/local/go/pkg/tool/*/go_bootstrap /usr/local/go/src/cmd/dist/dist \
/usr/local/go/.git* /tmp/golang /tmp/golang-fips
ln -sf /usr/local/go/bin/go /usr/local/go/bin/gofmt /usr/local/go/bin/godoc /usr/local/bin/
}
BUILD_OS=${BUILD_OS:-debian}
if [[ $BUILD_OS =~ debian ]]; then
build_debian "$@"
elif [[ $BUILD_OS =~ ubi ]]; then
build_ubi "$@"
fi