From e6bba376a71b3088eed2db39011a93c27c51c658 Mon Sep 17 00:00:00 2001 From: MaysWind Date: Mon, 7 Jun 2021 01:08:31 +0800 Subject: [PATCH] add build script --- Dockerfile | 10 +-- README.md | 11 ++- build-docker.sh | 11 --- build.sh | 151 +++++++++++++++++++++++++++++++++++++ src/views/mobile/About.vue | 2 +- vue.config.js | 16 +++- 6 files changed, 176 insertions(+), 25 deletions(-) delete mode 100755 build-docker.sh create mode 100644 build.sh diff --git a/Dockerfile b/Dockerfile index 005803aa..306e389e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,13 +4,7 @@ WORKDIR /go/src/github.com/mayswind/ezbookkeeping COPY . . RUN docker/backend-build-pre-setup.sh RUN apk add git gcc g++ libc-dev -RUN VERSION=`grep '"version": ' package.json | awk -F ':' '{print $2}' | tr -d ' ' | tr -d ',' | tr -d '"'` \ - && COMMIT_HASH=$(git rev-parse --short HEAD) \ - && BUILD_UNIXTIME="$(date '+%s')" \ - && VERSION_FLAGS="-X github.com/mayswind/ezbookkeeping/pkg/version.Version=${VERSION} -X github.com/mayswind/ezbookkeeping/pkg/version.CommitHash=${COMMIT_HASH} -X github.com/mayswind/ezbookkeeping/pkg/version.BuildUnixTime=${BUILD_UNIXTIME}" \ - && CGO_ENABLED=1 \ - && go build -a -v -trimpath -ldflags "-w -s -linkmode external -extldflags '-static' ${VERSION_FLAGS}" -o ezbookkeeping ezbookkeeping.go -RUN chmod +x ezbookkeeping +RUN build.sh backend # Build frontend files FROM node:14.17.0-alpine3.13 AS fe-builder @@ -18,7 +12,7 @@ WORKDIR /go/src/github.com/mayswind/ezbookkeeping COPY . . RUN docker/frontend-build-pre-setup.sh RUN apk add git -RUN npm install && npm run build +RUN build.sh frontend # Package docker image FROM alpine:3.13.5 diff --git a/README.md b/README.md index 736e81c7..70daa213 100644 --- a/README.md +++ b/README.md @@ -55,14 +55,13 @@ ezBookkeeping will listen at port 8080 as default. Then you can visit http:///ezbookkeeping @@ -71,6 +70,10 @@ Make sure you have [Golang](https://golang.org/), [GCC](http://gcc.gnu.org/), [N All the files will be placed in `` directory. +You can also build docker image, make sure you have [docker](https://www.docker.com/) installed, then follow these steps: + + $ ./build.sh docker + For more information about how to install ezBookkeeping, please visit our documentation. ## License diff --git a/build-docker.sh b/build-docker.sh deleted file mode 100755 index 80d539ea..00000000 --- a/build-docker.sh +++ /dev/null @@ -1,11 +0,0 @@ -#!/usr/bin/env bash - -mode=$1; -version=`grep '"version": ' package.json | awk -F ':' '{print $2}' | tr -d ' ' | tr -d ',' | tr -d '"'`; - -if [ "$mode" == "--snapshot" ]; then - version="SNAPSHOT-"`date "+%Y%m%d"`; -fi - -echo "Building docker image..."; -docker build -t ezbookkeeping:${version} . diff --git a/build.sh b/build.sh new file mode 100644 index 00000000..56373e2c --- /dev/null +++ b/build.sh @@ -0,0 +1,151 @@ +#!/usr/bin/env bash + +TYPE="" +RELEASE=${RELEASE_BUILD:-"0"}; +VERSION="" +COMMIT_HASH="" +BUILD_UNIXTIME="" + +echo_red() { + printf "\033[31m$1\033[0m\n" +} + +check_dependency() { + for cmd in $1 + do + which "$cmd" > /dev/null + + if [ $? != 0 ]; then + echo_red "Error: \"$cmd\" is required." + exit 127 + fi + done +} + +show_help() { + cat <<-EOF +ezBookkeeping build script + +Usage: + build.sh type [options] + +Types: + backend Build backend binary file + frontend Build frontend files + docker Build docker image + +Options: + -r, --release Build release (The script will use environment variable "RELEASE_BUILD" to detect whether this is release building by default) + -h, --help Show help +EOF +} + +parse_args() { + if [ "$1" == "backend" ] || [ "$1" == "frontend" ] || [ "$1" == "docker" ]; then + TYPE="$1" + shift 1 + fi + + while [ ${#} -gt 0 ]; do + case "${1}" in + --release | -r) + RELEASE="1" + ;; + --help | -h) + show_help + exit 0 + ;; + *) + echo_red "Invalid argument: $1" + show_help + exit 2 + ;; + esac + + shift 1 + done +} + +check_type_dependencies() { + if [ "$TYPE" == "" ]; then + echo_red "No specified type" + show_help + exit 2 + fi + + check_dependency "git" + + if [ "$TYPE" == "backend" ]; then + check_dependency "go" + elif [ "$TYPE" == "frontend" ]; then + check_dependency "node npm" + elif [ "$TYPE" == "docker" ]; then + check_dependency "docker" + fi +} + +set_build_parameters() { + VERSION="`grep '"version": ' package.json | awk -F ':' '{print $2}' | tr -d ' ' | tr -d ',' | tr -d '"'`" + COMMIT_HASH="$(git rev-parse --short HEAD)" + BUILD_UNIXTIME="$(date '+%s')" +} + +build_backend() { + local extra_arguments="-X github.com/mayswind/ezbookkeeping/pkg/version.Version=$VERSION" + extra_arguments="$extra_arguments -X github.com/mayswind/ezbookkeeping/pkg/version.CommitHash=$COMMIT_HASH" + + if [ "$RELEASE" == "0" ]; then + extra_arguments="$extra_arguments -X github.com/mayswind/ezbookkeeping/pkg/version.BuildUnixTime=$BUILD_UNIXTIME" + fi + + echo "Building backend binary file..." + + CGO_ENABLED=1 go build -a -v -trimpath -ldflags "-w -s -linkmode external -extldflags '-static' $extra_arguments" -o ezbookkeeping ezbookkeeping.go + chmod +x ezbookkeeping +} + +build_frontend() { + local build_arguments="--"; + + if [ "$RELEASE" == "0" ]; then + build_arguments="$build_arguments --buildUnixTime=$BUILD_UNIXTIME" + fi + + echo "Building frontend files..." + + npm install + npm run build $build_arguments +} + +build_docker() { + local docker_tag="$VERSION" + + if [ "$RELEASE" == "0" ]; then + docker_tag="SNAPSHOT-$(date '+%Y%m%d')"; + fi + + echo "Building docker image \"ezbookkeeping:$docker_tag\"..." + + docker build . -t ezbookkeeping:$docker_tag +} + +main() { + if [ -z "$1" ]; then + show_help + exit 0 + fi + + parse_args "$@" + check_type_dependencies "$TYPE" + set_build_parameters + + if [ "$TYPE" == "backend" ]; then + build_backend + elif [ "$TYPE" == "frontend" ]; then + build_frontend + elif [ "$TYPE" == "docker" ]; then + build_docker + fi +} + +main "$@" diff --git a/src/views/mobile/About.vue b/src/views/mobile/About.vue index 37b7f800..fafb2857 100644 --- a/src/views/mobile/About.vue +++ b/src/views/mobile/About.vue @@ -6,7 +6,7 @@ - + diff --git a/vue.config.js b/vue.config.js index e9c3e404..c1090cc0 100644 --- a/vue.config.js +++ b/vue.config.js @@ -66,10 +66,24 @@ module.exports = { }); config.plugin('define').tap(definitions => { + let buildUnixTime = ''; + + for (let i = 0; i < process.argv.length; i++) { + if (process.argv[i].indexOf('--') !== 0) { + continue; + } + + const pairs = process.argv[i].split('='); + + if (pairs[0] === '--buildUnixTime') { + buildUnixTime = pairs[1]; + } + } + const gitRevisionPlugin = new GitRevisionPlugin(); definitions[0]['process.env']['VERSION'] = JSON.stringify(pkgFile.version); definitions[0]['process.env']['COMMIT_HASH'] = JSON.stringify(gitRevisionPlugin.commithash()); - definitions[0]['process.env']['BUILD_UNIXTIME'] = JSON.stringify(parseInt((new Date().getTime() / 1000).toString())); + definitions[0]['process.env']['BUILD_UNIXTIME'] = buildUnixTime; definitions[0]['process.env']['LICENSE'] = JSON.stringify(licenseFile.trim()); definitions[0]['process.env']['THIRD_PARTY_LICENSES'] = JSON.stringify(thirdPartyLicenseFile);