2019-01-11 20:57:47 +08:00
|
|
|
# This script contains commands to be executed by the CI tool.
|
2019-05-13 20:48:54 +08:00
|
|
|
NPROC=$(nproc)
|
2022-06-10 03:57:37 +08:00
|
|
|
GO_VERSION=1.18.3
|
2021-02-23 02:41:59 +08:00
|
|
|
PROTOC_VERSION=3.12.3
|
2020-06-03 21:16:19 +08:00
|
|
|
PROTOC_GEN_VERSION=v1.4.2
|
2019-12-11 01:53:14 +08:00
|
|
|
PROTOC_GOFAST_VERSION=v1.3.1
|
2019-01-11 20:57:47 +08:00
|
|
|
|
2019-09-24 03:43:21 +08:00
|
|
|
function version_gt() { test "$(printf '%s\n' "$@" | sort -V | head -n 1)" != "$1"; }
|
|
|
|
|
|
|
|
update_go() {
|
2020-06-05 23:29:24 +08:00
|
|
|
CURRENT_GO_VERSION=$(go version | sed 's/[^0-9.]*\([0-9.]*\).*/\1/')
|
|
|
|
if version_gt $GO_VERSION $CURRENT_GO_VERSION; then
|
2020-07-13 21:24:55 +08:00
|
|
|
echo "Updating go version from $CURRENT_GO_VERSION to $GO_VERSION ..."
|
2022-06-10 03:57:37 +08:00
|
|
|
# remove other Go version from path
|
|
|
|
sudo rm -rf /usr/bin/go
|
2020-06-05 23:29:24 +08:00
|
|
|
sudo rm -rf /usr/local/go
|
2022-06-10 03:57:37 +08:00
|
|
|
sudo rm -rf /usr/local/bin/go
|
2020-06-05 23:29:24 +08:00
|
|
|
sudo rm -rf /usr/local/golang
|
2022-06-10 03:57:37 +08:00
|
|
|
sudo rm -rf $GOROOT $GOPAT $GOBIN
|
|
|
|
wget https://go.dev/dl/go$GO_VERSION.linux-amd64.tar.gz
|
|
|
|
sudo tar -C /usr/local -xzf go$GO_VERSION.linux-amd64.tar.gz
|
2020-06-05 23:29:24 +08:00
|
|
|
export GOROOT=/usr/local/go
|
2022-06-10 03:57:37 +08:00
|
|
|
export PATH=$PATH:/usr/local/go/bin
|
2020-06-05 23:29:24 +08:00
|
|
|
fi
|
2022-06-10 03:57:37 +08:00
|
|
|
export GOBIN=$HOME/go/bin
|
|
|
|
export PATH=$PATH:$GOBIN
|
2020-06-05 23:29:24 +08:00
|
|
|
go version
|
2019-09-24 03:43:21 +08:00
|
|
|
}
|
|
|
|
|
2019-01-11 20:57:47 +08:00
|
|
|
setup_protoc() {
|
2020-06-05 23:29:24 +08:00
|
|
|
# Execute `go get` for protoc dependencies outside of project dir.
|
|
|
|
echo "Setting up protoc..."
|
|
|
|
PROTOC_ZIP=protoc-$PROTOC_VERSION-linux-x86_64.zip
|
|
|
|
curl -0L https://github.com/google/protobuf/releases/download/v$PROTOC_VERSION/$PROTOC_ZIP -o $PROTOC_ZIP
|
|
|
|
unzip -o $PROTOC_ZIP -d protoc3
|
|
|
|
sudo mv protoc3/bin/* /usr/local/bin/
|
|
|
|
sudo mv protoc3/include/* /usr/local/include/
|
|
|
|
rm -f PROTOC_ZIP
|
2019-05-15 23:41:18 +08:00
|
|
|
|
2022-06-10 03:57:37 +08:00
|
|
|
go install github.com/golang/protobuf/protoc-gen-go@$PROTOC_GEN_VERSION
|
|
|
|
go install github.com/gogo/protobuf/protoc-gen-gofast@$PROTOC_GOFAST_VERSION
|
2019-05-15 23:41:18 +08:00
|
|
|
|
2020-06-05 23:29:24 +08:00
|
|
|
export PATH=$PATH:/usr/local/bin/protoc
|
2019-01-11 20:57:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
setup_mf() {
|
2020-06-05 23:29:24 +08:00
|
|
|
echo "Setting up Mainflux..."
|
|
|
|
for p in $(ls *.pb.go); do
|
|
|
|
mv $p $p.tmp
|
|
|
|
done
|
|
|
|
for p in $(ls pkg/*/*.pb.go); do
|
|
|
|
mv $p $p.tmp
|
|
|
|
done
|
|
|
|
make proto
|
|
|
|
for p in $(ls *.pb.go); do
|
|
|
|
if ! cmp -s $p $p.tmp; then
|
|
|
|
echo "Proto file and generated Go file $p are out of sync!"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
for p in $(ls pkg/*/*.pb.go); do
|
|
|
|
if ! cmp -s $p $p.tmp; then
|
|
|
|
echo "Proto file and generated Go file $p are out of sync!"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
make -j$NPROC
|
2019-01-11 20:57:47 +08:00
|
|
|
}
|
|
|
|
|
2020-04-21 23:05:19 +08:00
|
|
|
setup_lint() {
|
2020-06-05 23:29:24 +08:00
|
|
|
# binary will be $(go env GOBIN)/golangci-lint
|
2022-06-10 03:57:37 +08:00
|
|
|
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOBIN) v1.46.2
|
2020-06-03 21:16:19 +08:00
|
|
|
|
2020-04-21 23:05:19 +08:00
|
|
|
}
|
|
|
|
|
2019-01-11 20:57:47 +08:00
|
|
|
setup() {
|
2020-06-05 23:29:24 +08:00
|
|
|
echo "Setting up..."
|
|
|
|
update_go
|
|
|
|
setup_protoc
|
|
|
|
setup_mf
|
|
|
|
setup_lint
|
2019-01-11 20:57:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
run_test() {
|
2020-06-05 23:29:24 +08:00
|
|
|
echo "Running lint..."
|
2022-06-10 03:57:37 +08:00
|
|
|
golangci-lint run --no-config --disable-all --enable gosimple --enable govet --enable unused --enable deadcode --timeout 3m
|
2020-06-05 23:29:24 +08:00
|
|
|
echo "Running tests..."
|
|
|
|
echo "" > coverage.txt
|
|
|
|
for d in $(go list ./... | grep -v 'vendor\|cmd'); do
|
|
|
|
GOCACHE=off
|
|
|
|
go test -mod=vendor -v -race -tags test -coverprofile=profile.out -covermode=atomic $d
|
|
|
|
if [ -f profile.out ]; then
|
|
|
|
cat profile.out >> coverage.txt
|
|
|
|
rm profile.out
|
|
|
|
fi
|
|
|
|
done
|
2019-01-11 20:57:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
push() {
|
2020-06-05 23:29:24 +08:00
|
|
|
if test -n "$BRANCH_NAME" && test "$BRANCH_NAME" = "master"; then
|
|
|
|
echo "Pushing Docker images..."
|
|
|
|
make -j$NPROC latest
|
|
|
|
fi
|
2019-01-11 20:57:47 +08:00
|
|
|
}
|
|
|
|
|
2019-02-18 01:40:30 +08:00
|
|
|
set -e
|
2019-01-11 20:57:47 +08:00
|
|
|
setup
|
|
|
|
run_test
|
|
|
|
push
|