Skip to content

Instantly share code, notes, and snippets.

@mshafiee
Last active June 8, 2021 13:43
Show Gist options
  • Save mshafiee/5a681bbefda8f26f1f257d62f5e4a699 to your computer and use it in GitHub Desktop.
Save mshafiee/5a681bbefda8f26f1f257d62f5e4a699 to your computer and use it in GitHub Desktop.
Gitlab CI Template For Cross Compile Golang Source Code
image: golang:latest
variables:
REPO_NAME: gitlab.com/***/***
before_script:
- go version
- echo $CI_BUILD_REF
- echo $CI_PROJECT_DIR
stages:
- test
- build
test-project:
stage: test
script:
- mkdir -p $GOPATH/src/$REPO_NAME
- mv $CI_PROJECT_DIR/* $GOPATH/src/$REPO_NAME
- cd $GOPATH/src/$REPO_NAME
- go test $(go list ./... | grep -v /vendor/)
build-project:
stage: build
script:
- OUTPUT="ouput"
- mkdir -p $GOPATH/src/$REPO_NAME
- mv $CI_PROJECT_DIR/* $GOPATH/src/$REPO_NAME/
- cd $GOPATH/src/$REPO_NAME
- bash build-all.sh $OUTPUT $CI_PROJECT_DIR
artifacts:
paths:
- artifacts/
BIN_FILE_NAME_PREFIX=$1
PROJECT_DIR=$2
PLATFORMS=$(go tool dist list)
for PLATFORM in $PLATFORMS; do
GOOS=${PLATFORM%/*}
GOARCH=${PLATFORM#*/}
FILEPATH="$PROJECT_DIR/artifacts/${GOOS}-${GOARCH}"
#echo $FILEPATH
mkdir -p $FILEPATH
BIN_FILE_NAME="$FILEPATH/${BIN_FILE_NAME_PREFIX}"
#echo $BIN_FILE_NAME
if [[ "${GOOS}" == "windows" ]]; then BIN_FILE_NAME="${BIN_FILE_NAME}.exe"; fi
CMD="GOOS=${GOOS} GOARCH=${GOARCH} go build -o ${BIN_FILE_NAME}"
#echo $CMD
echo "${CMD}"
eval $CMD || FAILURES="${FAILURES} ${PLATFORM}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment