add build script

This commit is contained in:
MaysWind
2021-06-07 01:08:31 +08:00
parent f47a9ce492
commit e6bba376a7
6 changed files with 176 additions and 25 deletions
+2 -8
View File
@@ -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
+7 -4
View File
@@ -55,14 +55,13 @@ ezBookkeeping will listen at port 8080 as default. Then you can visit http://<YO
### Build from source
Make sure you have [Golang](https://golang.org/), [GCC](http://gcc.gnu.org/), [Node.js](https://nodejs.org/) and [NPM](https://www.npmjs.com/) installed. Then download the source code, and follow these steps.
Make sure you have [Golang](https://golang.org/), [GCC](http://gcc.gnu.org/), [Node.js](https://nodejs.org/) and [NPM](https://www.npmjs.com/) installed. Then download the source code, and follow these steps:
# Build backend binary file
$ GOOS=linux GOARCH=amd64 CGO_ENABLED=1 go build -a -v -i -trimpath -o ezbookkeeping ezbookkeeping.go
$ ./build.sh backend
# Build frontend static files
$ npm install
$ npm run build
$ ./build.sh frontend
# Copy files to target path
$ cp ezbookkeeping <target>/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 `<target>` 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
-11
View File
@@ -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} .
+151
View File
@@ -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 "$@"
+1 -1
View File
@@ -6,7 +6,7 @@
<f7-card-content class="no-safe-areas" :padding="false">
<f7-list>
<f7-list-item :title="$t('Version')" :after="version"></f7-list-item>
<f7-list-item :title="$t('Build Time')" :after="buildTime | moment($t('format.datetime.long'))"></f7-list-item>
<f7-list-item :title="$t('Build Time')" :after="buildTime | moment($t('format.datetime.long'))" v-if="buildTime"></f7-list-item>
<f7-list-item external :title="$t('Official Website')" link="https://github.com/mayswind/ezbookkeeping" target="_blank"></f7-list-item>
<f7-list-item :title="$t('License')" link="#" popup-open=".license-popup"></f7-list-item>
</f7-list>
+15 -1
View File
@@ -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);