`npm run sync` in web/ copies the build output (web/dist, from npm run build) to internal/frontend/dist, which the binary embeds. Go can't embed from outside the package's directory (web/ is also outside the Go module), hence the copy. Only dist/.gitkeep is committed, so a fresh clone still compiles, and a binary built without syncing first serves no frontend. job-search serve serves server.frontend.dir if it is set, or else the embedded build, or warns that there is no frontend. scripts/build.sh builds and syncs the frontend before building, running npm ci first for a release or when web/node_modules is missing.
74 lines
2.3 KiB
Bash
Executable File
74 lines
2.3 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
# Build script for job-search release
|
|
|
|
set -euo pipefail
|
|
|
|
BUILD_DIR="build"
|
|
RELEASE_DIR="release"
|
|
DO_RELEASE=0
|
|
|
|
if [[ $* == *--release* ]]; then
|
|
DO_RELEASE=1
|
|
fi
|
|
|
|
BUILD_TIME=$(date -u '+%Y-%m-%d_%H:%M:%S')
|
|
GIT_COMMIT=
|
|
VERSION=$(git describe --tags --abbrev=0 2>/dev/null || echo 'unknown')
|
|
VERSION_TAG=$(git describe --tags 2>/dev/null || echo 'unknown')
|
|
|
|
if [ "$VERSION" != "$VERSION_TAG" ]; then
|
|
echo "Dirty commits detected, marking as development build"
|
|
GIT_COMMIT=$(git rev-parse HEAD 2>/dev/null || echo 'unknown')
|
|
GIT_COMMIT_SHORT=$(git rev-parse --short HEAD 2>/dev/null || echo 'unknown')
|
|
VERSION="${VERSION}.${GIT_COMMIT_SHORT}"
|
|
|
|
DO_RELEASE=0
|
|
if [[ $* == *--release* && $* == *--force* ]]; then
|
|
echo "Forcing release build anyway"
|
|
DO_RELEASE=1
|
|
fi
|
|
fi
|
|
|
|
LD_FLAGS=${LD_FLAGS:-""}
|
|
LD_FLAGS="$LD_FLAGS -X git.avsdev.uk/Go/job-search/internal/version.Version=${VERSION}"
|
|
if [[ $DO_RELEASE -ne 1 ]]; then
|
|
LD_FLAGS="$LD_FLAGS -X git.avsdev.uk/Go/job-search/internal/version.BuildTime=${BUILD_TIME}"
|
|
fi
|
|
if [ "$GIT_COMMIT" != "" ]; then
|
|
LD_FLAGS="$LD_FLAGS -X git.avsdev.uk/Go/job-search/internal/version.GitCommit=${GIT_COMMIT}"
|
|
fi
|
|
|
|
# Build the frontend and copy it to internal/frontend/dist, where the binary
|
|
# embeds it. npm ci installs exactly the lock file's versions: always for a
|
|
# release, otherwise only when nothing is installed yet.
|
|
if [[ $DO_RELEASE -eq 1 || ! -d web/node_modules ]]; then
|
|
(cd web && npm ci)
|
|
fi
|
|
(cd web && npm run build && npm run sync)
|
|
|
|
# Process any generators
|
|
go generate ./...
|
|
|
|
# Build binary with version info
|
|
# sqlite_fts5 compiles in SQLite's FTS5 module, needed by the store's title
|
|
# search (migration 6 fails without it)
|
|
go build \
|
|
-tags sqlite_fts5 \
|
|
-ldflags="$LD_FLAGS" \
|
|
-trimpath \
|
|
-o $BUILD_DIR/job-search-$VERSION \
|
|
./cmd/jobsearch
|
|
|
|
echo "Built $BUILD_DIR/job-search-$VERSION"
|
|
|
|
# If proper release, copy to output directory for packaging
|
|
if [[ $DO_RELEASE -eq 1 ]]; then
|
|
install -DT $BUILD_DIR/job-search-$VERSION $RELEASE_DIR/job-search
|
|
ARCH=`dpkg --print-architecture`
|
|
tar -czf $RELEASE_DIR/job-search-$VERSION-$ARCH.tar.gz \
|
|
--transform "s,^$RELEASE_DIR/,job-search-$VERSION-$ARCH/," \
|
|
$RELEASE_DIR/job-search
|
|
echo "Installed $RELEASE_DIR/job-search"
|
|
fi
|