#!/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, with the # systemd unit and logrotate configuration (see README.md, "Running as a # service") if [[ $DO_RELEASE -eq 1 ]]; then install -DT $BUILD_DIR/job-search-$VERSION $RELEASE_DIR/job-search install -DT -m 644 deploy/job-search.service $RELEASE_DIR/job-search.service install -DT -m 644 deploy/job-search.logrotate $RELEASE_DIR/job-search.logrotate ARCH=`dpkg --print-architecture` # Owned by root (0:0), not the user who built it, with no names tar -czf $RELEASE_DIR/job-search-$VERSION-$ARCH.tar.gz \ --owner=0 --group=0 --numeric-owner \ --transform "s,^$RELEASE_DIR/,job-search-$VERSION-$ARCH/," \ $RELEASE_DIR/job-search $RELEASE_DIR/job-search.service \ $RELEASE_DIR/job-search.logrotate echo "Installed $RELEASE_DIR/job-search" fi