From 01206eb586088d511822bc5a52475db524ca8145 Mon Sep 17 00:00:00 2001 From: Timothy Messier Date: Tue, 20 Sep 2022 11:14:02 -0400 Subject: [PATCH] chore(build): Allow for downloading pre-built admin ui assets (#2475) The admin ui assets are built via a github workflow and published as github a artifact. The artifact can be downloaded, avoiding the need to build the assets again locally, and eliminating the need for front-end build tools like yarn and npm. This updates the `build-ui` target to use a new script that will attempt to download the pre-built assets. Note that this script requires that the `gh` cli is installed since an authenticated request is necessary to download the workflow artifacts. If the artifact cannot be downloaded either due to the lack of `gh` or because the artifact cannot be found, it will fallback to using `git clone` (and will require yarn to build the assets). See: https://cli.github.com/ https://docs.github.com/en/rest/actions/artifacts https://github.com/actions/upload-artifact/issues/51 --- Makefile | 9 ++++-- README.md | 4 ++- scripts/build-ui.sh | 67 +++++++++++++++++++++++++++++++++++++++++++++ scripts/uiclone.sh | 31 --------------------- scripts/uigen.sh | 18 ------------ 5 files changed, 76 insertions(+), 53 deletions(-) create mode 100755 scripts/build-ui.sh delete mode 100755 scripts/uiclone.sh delete mode 100755 scripts/uigen.sh diff --git a/Makefile b/Makefile index c62ee20d0..280b6ca92 100644 --- a/Makefile +++ b/Makefile @@ -64,11 +64,10 @@ fmt: buf format -w # Set env for all UI targets. -UI_TARGETS := update-ui-version build-ui build-ui-ifne +UI_TARGETS := update-ui-version build-ui build-ui-ifne clean-ui # Note the extra .tmp path segment in UI_CLONE_DIR is significant and required. $(UI_TARGETS): export UI_CLONE_DIR := internal/ui/.tmp/boundary-ui $(UI_TARGETS): export UI_VERSION_FILE := internal/ui/VERSION -$(UI_TARGETS): export UI_ASSETS_FILE := internal/ui/assets.go $(UI_TARGETS): export UI_DEFAULT_BRANCH := main $(UI_TARGETS): export UI_CURRENT_COMMIT := $(shell head -n1 < "$(UI_VERSION_FILE)" | cut -d' ' -f1) $(UI_TARGETS): export UI_COMMITISH ?= @@ -91,7 +90,11 @@ build-ui: else \ echo "==> Building custom UI version $(UI_COMMITISH)"; \ fi; \ - ./scripts/uiclone.sh && ./scripts/uigen.sh + ./scripts/build-ui.sh + +.PHONY: clean-ui +clean-ui: + rm -rf ${UI_CLONE_DIR} .PHONY: build-ui-ifne build-ui-ifne: diff --git a/README.md b/README.md index 343cf054b..243e0a990 100644 --- a/README.md +++ b/README.md @@ -90,7 +90,9 @@ for permanent installations, are detailed in our [Installation Guide](https://ww If you have the following requirements met locally: - Golang v1.19 or greater - Docker -- [Boundary UI Dependencies](https://github.com/hashicorp/boundary-ui#prerequisites) +- Either the [Boundary UI Dependencies](https://github.com/hashicorp/boundary-ui#prerequisites) + for locally building the ui assets + or [gh cli](https://cli.github.com) for downloading pre-built ui assets. You can get up and running with Boundary quickly. Simply run: diff --git a/scripts/build-ui.sh b/scripts/build-ui.sh new file mode 100755 index 000000000..48883e704 --- /dev/null +++ b/scripts/build-ui.sh @@ -0,0 +1,67 @@ +#!/usr/bin/env bash + +set -e + +if [ -z "$UI_COMMITISH" ]; then + echo "Must set UI_COMMITISH"; exit 1 +fi + +if [ -z "$UI_CLONE_DIR" ]; then + echo "Must set UI_CLONE_DIR"; exit 1 +fi + +if [ -z "$UI_VERSION_FILE" ]; then + echo "Must set UI_CLONE_DIR"; exit 1 +fi + +if which gh &> /dev/null; then + echo "Found gh cli, attempting to download ui assets" + + UI_EDITION=$(make --no-print-directory edition) + artifact_id=$(gh api repos/hashicorp/boundary-ui/actions/artifacts | \ + jq ".artifacts[] | select(.workflow_run.head_sha == \"${UI_COMMITISH}\" and .name == \"admin-ui-${UI_EDITION}\")" | \ + jq -r '.id') + + if [[ ${artifact_id} ]]; then + echo "Downloading artifact: ${artifact_id} for admin-ui-${UI_EDITION} ${UI_COMMITISH}" + tmp_dir=$(mktemp -d) + gh api "repos/hashicorp/boundary-ui/actions/artifacts/${artifact_id}/zip" > "${tmp_dir}/boundary-ui.zip" + trap 'rm -rf ${tmp_dir}' EXIT + + # remove any previous artifact download or git clone + rm -rf "${UI_CLONE_DIR}" + + mkdir -p "${UI_CLONE_DIR}/ui/admin/dist" + unzip "${tmp_dir}/boundary-ui.zip" -d "${UI_CLONE_DIR}/ui/admin/dist" + exit $? + else + echo "could not find artifact: admin-ui-${UI_EDITION} ${UI_COMMITISH}, falling back to git clone" + fi +fi + +if ! which yarn &> /dev/null; then + echo "Yarn must be installed to build ui assets from a git clone.\nPlease ensure Node v14+ and Yarn v1.22.10+ are installed." + exit 1 +fi + +tempdir="$(dirname "${UI_CLONE_DIR}")" + +mkdir -p "${tempdir}" +echo "*" > "${tempdir}/.gitignore" + +if ! [ -d "${UI_CLONE_DIR}/.git" ]; then + # clear out dir, incase it was previously an artifact download + rm -rf "${UI_CLONE_DIR}" + git clone https://github.com/hashicorp/boundary-ui "${UI_CLONE_DIR}" +fi + +pushd "${UI_CLONE_DIR}" +git reset --hard +git fetch origin "${UI_COMMITISH}" +git checkout "${UI_COMMITISH}" +git pull --ff-only origin "${UI_COMMITISH}" +git reset --hard "${UI_COMMITISH}" + +yarn install +yarn build +popd diff --git a/scripts/uiclone.sh b/scripts/uiclone.sh deleted file mode 100755 index 163794044..000000000 --- a/scripts/uiclone.sh +++ /dev/null @@ -1,31 +0,0 @@ -#!/bin/sh - -set -e - -if [ -z "$UI_COMMITISH" ]; then - echo "Must set UI_COMMITISH"; exit 1 -fi - -if [ -z "$UI_CLONE_DIR" ]; then - echo "Must set UI_CLONE_DIR"; exit 1 -fi - -if [ -z "$UI_VERSION_FILE" ]; then - echo "Must set UI_CLONE_DIR"; exit 1 -fi - -tempdir="$(dirname "${UI_CLONE_DIR}")" - -mkdir -p "${tempdir}" -echo "*" > "${tempdir}/.gitignore" - -if ! [ -d "${UI_CLONE_DIR}/.git" ]; then - git clone https://github.com/hashicorp/boundary-ui "${UI_CLONE_DIR}" -fi - -cd "${UI_CLONE_DIR}" -git reset --hard -git fetch origin "${UI_COMMITISH}" -git checkout "${UI_COMMITISH}" -git pull --ff-only origin "${UI_COMMITISH}" -git reset --hard "${UI_COMMITISH}" diff --git a/scripts/uigen.sh b/scripts/uigen.sh deleted file mode 100755 index 5486c9eed..000000000 --- a/scripts/uigen.sh +++ /dev/null @@ -1,18 +0,0 @@ -#!/bin/sh - -set -e - -if [ -z "$UI_CLONE_DIR" ]; then - echo "Must set UI_CLONE_DIR"; exit 1 -fi - -if [ -z "$UI_ASSETS_FILE" ]; then - echo "Must set UI_ASSETS_FILE"; exit 1 -fi - -( - cd "$UI_CLONE_DIR" - if ! (yarn install && yarn build); then - echo "Please ensure Node v14+ and Yarn v1.22.10+ are installed." - fi -)