Skip to content

Commit

Permalink
Add cross-build script
Browse files Browse the repository at this point in the history
  • Loading branch information
farshidtz committed Apr 24, 2022
1 parent 717f094 commit 8fca01e
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ jobs:
- name: Cross Compile go
if: success()
run: |
curl -s https://raw.githubusercontent.com/linksmart/ci-scripts/master/go/go-build.sh | bash
./build.sh
mkdir -p output/bin output/conf
cp bin/* output/bin
cp sample_conf/* wot/wot_td_schema.json output/conf
Expand Down
54 changes: 54 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/bin/bash

# EXAMPLES
# Build for default platforms:
# NAME=app ./go-build.sh
# Build for specific platforms:
# NAME=app PLATFORMS="linux/arm64 linux/arm" ./go-build.sh
# Pass version and build number (ldflags):
# NAME=app VERSION=v1.0.0 BUILDNUM=1 ./go-build.sh

output_dir=bin

export GO111MODULE=on
export CGO_ENABLED=0

if [[ -z "$NAME" ]]; then
echo "usage: NAME=app sh go-build.sh"
exit 1
fi

if [[ -z "$PLATFORMS" ]]; then
PLATFORMS="windows/amd64 darwin/amd64 linux/amd64 linux/arm64 linux/arm"
echo "Using default platforms: $PLATFORMS"
fi

if [[ -n "$VERSION" ]]; then
echo "Version: $VERSION"
fi

if [[ -n "$BUILDNUM" ]]; then
echo "Build Num: $BUILDNUM"
fi

for platform in $PLATFORMS
do
platform_split=(${platform//\// })
export GOOS=${platform_split[0]}
export GOARCH=${platform_split[1]}
output_name=$output_dir'/'$NAME'-'$GOOS'-'$GOARCH
if [ $GOOS = "windows" ]; then
output_name+='.exe'
fi
echo "Building $output_name"

go build -ldflags "-X main.Version=$VERSION -X main.BuildNumber=$BUILDNUM" -o $output_name
if [ $? -ne 0 ]; then
echo "An error has occurred! Aborting the script execution..."
exit 1
fi
done

# Adapted from:
# https://www.digitalocean.com/community/tutorials/how-to-build-go-executables-for-multiple-platforms-on-ubuntu-16-04
# https://github.com/linksmart/ci-scripts/blob/master/go/go-build.sh
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,6 @@ golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20190924154521-2837fb4f24fe/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200519105757-fe76b779f299 h1:DYfZAGf2WMFjMxbgTjaC+2HC7NkNAQs+6Q8b9WEB/F4=
golang.org/x/sys v0.0.0-20200519105757-fe76b779f299/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220422013727-9388b58f7150 h1:xHms4gcpe1YE7A3yIllJXP16CMAGuqwO2lX1mTyyRRc=
golang.org/x/sys v0.0.0-20220422013727-9388b58f7150/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
Expand Down

0 comments on commit 8fca01e

Please sign in to comment.