Skip to content

Commit

Permalink
Refactor the build-release.sh and build-docker.sh scripts to make it …
Browse files Browse the repository at this point in the history
…more generic (#23)
  • Loading branch information
mstmdev authored Sep 15, 2023
1 parent 0acde30 commit b3cda2b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 43 deletions.
26 changes: 13 additions & 13 deletions scripts/build-docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,30 @@ docker pull golang:latest
# set GOPROXY environment variable
# GOPROXY=https://goproxy.cn

# set the fsctl docker image name by FSCTL_IMAGE_NAME variable
FSCTL_IMAGE_NAME=nosrc/fsctl
SOFT_NAME=fsctl
# docker image name
SOFT_IMAGE_NAME=nosrc/${SOFT_NAME}
# docker image tag
SOFT_IMAGE_TAG=latest

# set the fsctl docker image tag by FSCTL_IMAGE_TAG variable
FSCTL_IMAGE_TAG=latest

# reset FSCTL_IMAGE_TAG to the value of the first parameter provided by the user
# reset SOFT_IMAGE_TAG to the value of the first parameter provided by the user
if [ -n "$1" ]; then
FSCTL_IMAGE_TAG=$1
SOFT_IMAGE_TAG=$1
fi

# remove the existing old image
docker rmi -f $FSCTL_IMAGE_NAME:$FSCTL_IMAGE_TAG
docker rmi -f $SOFT_IMAGE_NAME:$SOFT_IMAGE_TAG

# build Dockerfile
docker build --build-arg GOPROXY=$GOPROXY -t $FSCTL_IMAGE_NAME:$FSCTL_IMAGE_TAG .
docker build --build-arg GOPROXY=$GOPROXY -t $SOFT_IMAGE_NAME:$SOFT_IMAGE_TAG .

# remove dangling images
docker image prune -f

docker images | grep fsctl
docker images | grep ${SOFT_NAME}

# run a container to print the fsctl version
docker run --rm --name running-fsctl-version $FSCTL_IMAGE_NAME:$FSCTL_IMAGE_TAG fsctl -v
# run a container to print the soft version
docker run --rm --name running-${SOFT_NAME}-version $SOFT_IMAGE_NAME:$SOFT_IMAGE_TAG ${SOFT_NAME} -v

# push the image to the DockerHub
# docker push $FSCTL_IMAGE_NAME:$FSCTL_IMAGE_TAG
# docker push $SOFT_IMAGE_NAME:$SOFT_IMAGE_TAG
61 changes: 31 additions & 30 deletions scripts/build-release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,48 @@ echo -e "$(git rev-parse HEAD)\c" >internal/version/commit
# set GOPROXY environment variable
# export GOPROXY=https://goproxy.cn

function build_release {
# build
export SOFT_RELEASE_GO_VERSION
export SOFT_RELEASE_VERSION
export SOFT_NAME="fsctl"
export SOFT_PREFIX="${SOFT_NAME}_"

function init_version {
go build -v -o . ./...

# release path, for example, fsctl_go1.20.2_amd64_linux_v0.1.0
export FSCTL_RELEASE="fsctl_${FSCTL_RELEASE_GO_VERSION}_${GOARCH}_${GOOS}_${FSCTL_RELEASE_VERSION}"
SOFT_RELEASE_GO_VERSION=$(go version | awk '{print $3}')
SOFT_RELEASE_VERSION=$(./${SOFT_NAME} -v | awk 'NR==1 {print $3}')
}

function build_release {
# release path, for example, fsctl_go1.21.1_arm64_linux_v0.1.0
SOFT_RELEASE="${SOFT_PREFIX}${SOFT_RELEASE_GO_VERSION}_${GOARCH}_${GOOS}_${SOFT_RELEASE_VERSION}"

rm -rf "$FSCTL_RELEASE"
mkdir "$FSCTL_RELEASE"
mv fsctl "$FSCTL_RELEASE/"
rm -rf "$SOFT_RELEASE"
mkdir "$SOFT_RELEASE"

# release archive
tar -zcvf "$FSCTL_RELEASE.tar.gz" "$FSCTL_RELEASE"
# build
go build -v -o . ./...

rm -rf "$FSCTL_RELEASE"
if [ "$GOOS" == "windows" ]; then
mv ${SOFT_NAME}.exe "$SOFT_RELEASE/"
# windows release archive
zip -r "$SOFT_RELEASE.zip" "$SOFT_RELEASE"
else
mv ${SOFT_NAME} "$SOFT_RELEASE/"
# release archive
tar -zcvf "$SOFT_RELEASE.tar.gz" "$SOFT_RELEASE"
fi
rm -rf "$SOFT_RELEASE"
}

init_version

############################## linux-amd64-release ##############################

# set go env
export GOOS=linux
export GOARCH=amd64

# build
go build -v -o . ./...

FSCTL_RELEASE_GO_VERSION=$(go version | awk '{print $3}')
FSCTL_RELEASE_VERSION=$(./fsctl -v | awk 'NR==1 {print $3}')

build_release

############################## linux-amd64-release ##############################
Expand All @@ -59,19 +72,7 @@ build_release
export GOOS=windows
export GOARCH=amd64

# build
go build -v -o . ./...

# release path, for example, fsctl_go1.20.2_amd64_windows_v0.1.0
FSCTL_RELEASE="fsctl_${FSCTL_RELEASE_GO_VERSION}_${GOARCH}_${GOOS}_${FSCTL_RELEASE_VERSION}"

mkdir "$FSCTL_RELEASE"
mv fsctl.exe "$FSCTL_RELEASE/"

# release archive
zip -r "$FSCTL_RELEASE.zip" "$FSCTL_RELEASE"

rm -rf "$FSCTL_RELEASE"
build_release

############################# windows-release #############################

Expand All @@ -96,4 +97,4 @@ build_release
# reset commit file
echo -e "\c" >internal/version/commit

ls -alh | grep fsctl_
ls -alh | grep ${SOFT_PREFIX}

0 comments on commit b3cda2b

Please sign in to comment.