|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Builds a local-testing image of the device-agent from the current working copy. |
| 3 | +# |
| 4 | +# - clears any stale flowfuse-device-agent-*.tgz |
| 5 | +# - runs `npm pack` to package the local code |
| 6 | +# - tags the image based on the current git branch: |
| 7 | +# main -> flowfuse/device-agent:local-build |
| 8 | +# <other-branch> -> flowfuse/device-agent:<branch>-local-build |
| 9 | +# |
| 10 | +# Usage (from anywhere): |
| 11 | +# ./docker/build-local.sh |
| 12 | +# IMAGE_NAME=myorg/device-agent ./docker/build-local.sh # override repo name |
| 13 | +# TAG=custom-tag ./docker/build-local.sh # override the whole tag |
| 14 | + |
| 15 | +# Fail fast: -e exits on any command error, -u errors on unset variables, |
| 16 | +# -o pipefail makes a pipeline fail if any stage (not just the last) fails. |
| 17 | +set -euo pipefail |
| 18 | + |
| 19 | +# Generate a default image name and tag if not supplied by the caller. |
| 20 | +IMAGE_NAME="${IMAGE_NAME:-flowfuse/device-agent}" |
| 21 | +TAG="${TAG:-}" |
| 22 | + |
| 23 | +# Resolve the device-agent repo root (parent of this script's /docker dir) and work from there. |
| 24 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 25 | +REPO_ROOT="$(dirname "$SCRIPT_DIR")" |
| 26 | +cd "$REPO_ROOT" |
| 27 | + |
| 28 | +# 1. Clear any old tarballs so only the fresh pack remains. |
| 29 | +for f in flowfuse-device-agent-*.tgz; do |
| 30 | + if [ -e "$f" ]; then |
| 31 | + echo "Removing old pack: $f" |
| 32 | + rm -f "$f" |
| 33 | + fi |
| 34 | +done |
| 35 | + |
| 36 | +# 2. Package the local working copy. |
| 37 | +echo "Running npm pack..." |
| 38 | +npm pack |
| 39 | + |
| 40 | +# 3. Work out the tag from the branch name (unless one was supplied). |
| 41 | +if [ -z "$TAG" ]; then |
| 42 | + branch="$(git rev-parse --abbrev-ref HEAD)" |
| 43 | + if [ "$branch" = "main" ]; then |
| 44 | + TAG="local-build" |
| 45 | + else |
| 46 | + # Sanitise: lowercase, and replace anything not allowed in a docker tag with '-' |
| 47 | + safe_branch="$(echo "$branch" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9_.-]/-/g')" |
| 48 | + TAG="${safe_branch}-local-build" |
| 49 | + fi |
| 50 | +fi |
| 51 | + |
| 52 | +FULL_IMAGE="${IMAGE_NAME}:${TAG}" |
| 53 | + |
| 54 | +# 4. Build the image (context = repo root, so the .tgz is reachable by COPY). |
| 55 | +echo "Building image: $FULL_IMAGE" |
| 56 | +docker build -f docker/Dockerfile.local -t "$FULL_IMAGE" . |
| 57 | + |
| 58 | +echo "" |
| 59 | +echo "Built $FULL_IMAGE" |
| 60 | +echo "" |
| 61 | +echo "Enter the following command to run the device-agent:" |
| 62 | +echo " docker run --rm -it \\" |
| 63 | +echo " -v /opt/flowfuse-device-docker-local/device.yml:/opt/flowfuse-device/device.yml \\" |
| 64 | +echo " -p 1888:1880 \\" |
| 65 | +echo " $FULL_IMAGE" |
| 66 | +echo "" |
| 67 | +echo "NOTE:" |
| 68 | +echo "Entering the above command will run the device-agent on port 1888" |
| 69 | +echo "using your local device.yml (edit as needed before hitting enter)" |
0 commit comments