Skip to content

Commit

Permalink
Add ci_build_local.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
at055612 committed Sep 25, 2024
1 parent 34a3b28 commit ee73f47
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 14 deletions.
46 changes: 32 additions & 14 deletions ci_build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ setup_echo_colours() {
fi
}

error_exit() {
local -r msg="$1"
echo -e "${msg}"
exit 1
}

prepare_for_schema_release() {
echo -e "${GREEN}Preparing for a schema release${NC}"
prepare_release_artefacts
Expand All @@ -39,11 +45,11 @@ validate_source_schema() {
xmllint \
--noout \
--schema http://www.w3.org/2001/XMLSchema.xsd \
"${BUILD_DIR}/${SOURCE_SCHEMA_FILE}"
"${BUILD_DIR}/${SOURCE_SCHEMA_FILE_NAME}"
echo "::endgroup::"

echo -e "::group::Validating versions in schema file"
./validateSchemaVersions.py "${SCHEMA_VERSION}"
"${BUILD_DIR}/validateSchemaVersions.py" "${SCHEMA_VERSION}"
echo "::endgroup::"
}

Expand All @@ -52,17 +58,22 @@ build_schema_variants() {
# Download the jar

pushd "${LIB_DIR}"
echo -e "${GREEN}Downloading ${BLUE}${TRANSFORMER_JAR_URL}${NC}"
wget --quiet "${TRANSFORMER_JAR_URL}"
# If running locally, we don't want to keep downloading the same thing
if [[ ! -f "${TRANSFORMER_JAR_FILENAME}" ]]; then
echo -e "${GREEN}Downloading ${BLUE}${TRANSFORMER_JAR_URL}${NC}"
wget --quiet "${TRANSFORMER_JAR_URL}"
fi
popd

local PIPELINES_DIR="${BUILD_DIR}/pipelines"
local GENERATED_DIR="${PIPELINES_DIR}/generated"

# Run the transformations
java \
-jar "${LIB_DIR}/${TRANSFORMER_JAR_FILENAME}" \
./pipelines \
"./${SOURCE_SCHEMA_FILE}"
"${PIPELINES_DIR}/" \
"${BUILD_DIR}/${SOURCE_SCHEMA_FILE_NAME}"

GENERATED_DIR="./pipelines/generated"

echo -e "${GREEN}Dumping contents of ${BLUE}${GENERATED_DIR}${NC}"
ls -l "${GENERATED_DIR}"
Expand Down Expand Up @@ -91,7 +102,7 @@ dump_build_info() {
echo -e "BUILD_IS_SCHEMA_RELEASE: [${GREEN}${BUILD_IS_SCHEMA_RELEASE}${NC}]"
echo -e "RELEASE_ARTEFACTS_DIR: [${GREEN}${RELEASE_ARTEFACTS_DIR}${NC}]"
echo -e "SCHEMA_VERSION: [${GREEN}${SCHEMA_VERSION}${NC}]"
echo -e "SOURCE_SCHEMA_FILE: [${GREEN}${SOURCE_SCHEMA_FILE}${NC}]"
echo -e "SOURCE_SCHEMA_FILE_NAME: [${GREEN}${SOURCE_SCHEMA_FILE_NAME}${NC}]"
echo -e "TRANSFORMER_JAR_FILENAME [${GREEN}${TRANSFORMER_JAR_FILENAME}${NC}]"

echo -e "\nJava version:"
Expand All @@ -109,6 +120,10 @@ create_dir() {
main() {
setup_echo_colours

if [[ -z "$BUILD_DIR" ]]; then
error_exit "${RED}ERROR${NC} BUILD_DIR is not set"
fi

if [ -n "$BUILD_TAG" ]; then
#Tagged commit so use that as our stroom version, e.g. v3.0.0
SCHEMA_VERSION="${BUILD_TAG}"
Expand All @@ -119,7 +134,7 @@ main() {
local RELEASE_ARTEFACTS_DIR_NAME="release_artefacts"
local RELEASE_ARTEFACTS_DIR="${BUILD_DIR}/${RELEASE_ARTEFACTS_DIR_NAME}"
local LIB_DIR="${BUILD_DIR}/lib"
local SOURCE_SCHEMA_FILE="detection.xsd"
local SOURCE_SCHEMA_FILE_NAME="detection.xsd"
local TRANSFORMER_JAR_VERSION="v4.1.0"
local TRANSFORMER_JAR_URL_BASE="https://github.com/gchq/event-logging-schema/releases/download"
local TRANSFORMER_JAR_FILENAME="event-logging-transformer-${TRANSFORMER_JAR_VERSION}-all.jar"
Expand All @@ -133,19 +148,22 @@ main() {
echo "::endgroup::"

validate_source_schema
# Run the gradle build to generate the different forms of the schema
build_schema_variants

build_schema_variants

if [[ "${BUILD_IS_SCHEMA_RELEASE}" = "true" ]]; then
echo -e "${GREEN}This is a release build${NC}"
echo -e "${GREEN}Dumping contents of ${BLUE}${RELEASE_ARTEFACTS_DIR}${NC}"
ls -l "${RELEASE_ARTEFACTS_DIR}"
else
echo -e "${GREEN}Not a release so skipping release preparation${NC}"
# Clear out any artefacts to make sure nothing
# can get uploaded to github
rm -rf "${RELEASE_ARTEFACTS_DIR:?}/*"
# Clear out any artefacts to make sure nothing can get uploaded to github
# Only do the delete if we are in a github action (i.e. GITHUB_REF is set)
# to reduce the risk of doing 'rm -rf /*' on your local machine, which
# would be BAD.
if [[ -n "${RELEASE_ARTEFACTS_DIR}" && -n "${GITHUB_REF}" ]]; then
rm -rf "${RELEASE_ARTEFACTS_DIR:?}/*"
fi
fi
}

Expand Down
44 changes: 44 additions & 0 deletions ci_build_local.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env bash

# Runs ci_build.sh on your local machine by first setting
# up some env vars to imitate what GH actions would do.

set -eo pipefail

error_exit() {
local -r msg="$1"
echo -e "${msg}"
exit 1
}

check_for_installed_binary() {
local -r binary_name=$1
command -v "${binary_name}" 1>/dev/null \
|| error_exit "${GREEN}${binary_name}${RED} is not installed${NC}"
}

main() {
check_for_installed_binary "xmllint"
check_for_installed_binary "wget"
check_for_installed_binary "java"

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

# Set the env vars that ci_build.sh needs
export BUILD_IS_SCHEMA_RELEASE=false
export BUILD_DIR="${SCRIPT_DIR}"

# Clear out old stuff
rm -f "${BUILD_DIR}/pipelines/generated/*"

echo "Using BUILD_DIR: ${BUILD_DIR}"

pushd "${BUILD_DIR}" > /dev/null

# Run the CI build locally
"${BUILD_DIR}/ci_build.sh"

popd > /dev/null
}

main "$@"

0 comments on commit ee73f47

Please sign in to comment.