#!/usr/bin/env bash set -e function headIsDetached() { [[ $(git rev-parse --abbrev-ref --symbolic-full-name HEAD) == "HEAD" ]]; } function dockerImageExists() { docker image inspect "${IMAGE}:${COMMIT}" 2>&1 1>/dev/null; } function binaryExists() { ls -l build/{{ item.targets | first }}_${COMMIT} 2>&1 1>/dev/null } function fetchChanges() { # We cannot use "git pull" in here, because history may be changed upstream git fetch git reset --hard "origin/${BRANCH}" } function buildBinaries() { # Lower CPU and I/O priority so it doesn't affect the running beacon node NICE="nice -n 19 ionice -c2 -n7" ${NICE} make -j1 update ${NICE} make -j1 {{ item.targets | join(" ") }} \ LOG_LEVEL="TRACE" NIMFLAGS="-d:testnet_servers_image -d:noSignalHandler" # Rename binaries to match commit the were built from. {% for target in item.targets %} mv "build/{{ target }}" "build/{{ target }}_${COMMIT}" {% endfor %} # Delete copies that are older than 7 days find build -mtime +7 -exec rm '{}' \+ } # Adds binary into a simple Alpine image function buildDockerImage() { docker build -t "${IMAGE}:${COMMIT}" \ --build-arg=COMMIT=${COMMIT} \ --label "commit=${COMMIT}" . } function pushImageTag() { docker tag "${IMAGE}:${COMMIT}" "${IMAGE}:${TAG}" docker push "${IMAGE}:${TAG}" } #------------------------------------------------------------------------------- BRANCH="{{ item.version }}" IMAGE="{{ beacon_node_builds_image_name }}" TAG="{{ item.name }}" if [[ "${USER}" != "{{ beacon_node_builds_user }}" ]]; then echo "Incorrect user: ${USER}" >&2 echo "Expected: {{ beacon_node_builds_user }}" >&2 exit 1 fi # Build the Beacon node binaries pushd repo >/dev/null # Detached HEAD means we're probably on a tag if headIsDetached; then echo " >>> Deatached HEAD, nothing to fetch." else echo " >>> Fetching changes..." fetchChanges fi COMMIT=$(git rev-parse --short=8 HEAD) if binaryExists && [[ "$1" != "--force" ]]; then echo " >>> Binary already built" else echo " >>> Building binaries..." buildBinaries fi popd >/dev/null if dockerImageExists && [[ "$1" != "--force" ]]; then echo " >>> Image already built: ${IMAGE}:${COMMIT}" else echo " >>> Building Docker image..." buildDockerImage fi echo " >>> Pushing Docker image..." pushImageTag echo " >>> SUCCESS - Pushed: ${IMAGE}:${TAG}"