From c4cbfc995542341eda10498ec5dbb045de13d41a Mon Sep 17 00:00:00 2001 From: Eugene Date: Sat, 9 Dec 2023 16:30:09 +0100 Subject: [PATCH 1/2] Part 1. Automate the build process with Bash --- .../build-client.sh | 67 +++++++++++++++++++ .../quality-check.sh | 33 +++++++++ 2 files changed, 100 insertions(+) create mode 100644 lab_1/baranov_yevgen_home_work_task-1/build-client.sh create mode 100644 lab_1/baranov_yevgen_home_work_task-1/quality-check.sh diff --git a/lab_1/baranov_yevgen_home_work_task-1/build-client.sh b/lab_1/baranov_yevgen_home_work_task-1/build-client.sh new file mode 100644 index 0000000..c80eeb8 --- /dev/null +++ b/lab_1/baranov_yevgen_home_work_task-1/build-client.sh @@ -0,0 +1,67 @@ +#!/bin/bash + +RED='\033[0;31m' +YELLOW='\033[1;33m' +GREEN='\033[0;32m' +NC='\033[0m' # No Color + +CURRENT_TIME=$(date +"%Y-%m-%d %H:%M:%S") + +if ! command -v node &> /dev/null; then + echo -e "${RED}Error:${NC} Node.js is not installed. Please install Node.js to proceed." + exit 1 +fi + +if ! command -v ng &> /dev/null; then + echo -e "${RED}Error:${NC} Angular CLI is not installed. Please install Angular CLI to proceed." + exit 1 +fi + +APP_DIR="." +if [ -n "$1" ]; then + APP_DIR="$1" +fi + +echo -e "${GREEN}Using directory:${NC} $APP_DIR" + +if [ -f "$APP_DIR/client-app.zip" ]; then + echo -e "${YELLOW}Warning:${NC} Removing existing client-app.zip..." + rm "$APP_DIR/client-app.zip" +fi + +echo -e "${GREEN}Cleaning previous build artifacts...${NC}" +ng clean + +echo -e "${GREEN}Installing npm dependencies...${NC}" +cd "$APP_DIR" || exit 1 +if [ -f "package-lock.json" ]; then + echo -e "${GREEN}package-lock.json found.${NC} Installing npm dependencies using 'npm ci'..." + npm ci --quiet +else + echo -e "${YELLOW}package-lock.json not found.${NC} Installing npm dependencies using 'npm install'..." + npm install --quiet +fi + +echo -e "${GREEN}Dependency installation complete.${NC}" + +if [ -z "$ENV_CONFIGURATION" ]; then + echo -e "${YELLOW}Warning:${NC} ENV_CONFIGURATION is not set. Building the Angular application without a specific configuration flag." + ng build + echo -e "${GREEN}Angular build complete without a specific configuration flag.${NC}" +else + echo -e "${GREEN}Building Angular application with configuration:${NC} $ENV_CONFIGURATION..." + ng build --configuration="$ENV_CONFIGURATION" + echo -e "${GREEN}Angular build complete.${NC}" +fi + +echo -e "${GREEN}Compressing the build output into client-app.zip...${NC}" +cd "$APP_DIR" || exit 1 +if [ -d "dist" ]; then + cd "dist" || exit 1 + zip -r "../client-app.zip" ./* + echo -e "${GREEN}Compression complete:${NC} client-app.zip created in the root folder." +else + echo -e "${RED}Error:${NC} 'dist' directory not found. Couldn't create client-app.zip." +fi + +echo -e "${GREEN}Build script executed successfully.${NC}" \ No newline at end of file diff --git a/lab_1/baranov_yevgen_home_work_task-1/quality-check.sh b/lab_1/baranov_yevgen_home_work_task-1/quality-check.sh new file mode 100644 index 0000000..4eed5bb --- /dev/null +++ b/lab_1/baranov_yevgen_home_work_task-1/quality-check.sh @@ -0,0 +1,33 @@ +#!/bin/bash + +if [ -n "$1" ]; then + directory="$1" +else + directory="$(pwd)" +fi + +cd "$directory" + +echo "Running linting..." +npx eslint . --ignore-path .gitignore --ext .ts + +echo "Running unit tests..." + +test_output=$(npm run test) + +if echo "$test_output" | grep -q "TOTAL:"; then + echo "String 'TOTAL:' found. Continuing..." +else + echo "String 'TOTAL:' not found. Exiting." + exit 1 +fi + +echo "Running npm audit..." +audit_result=$(npm audit) + +if [[ $audit_result == *"found"* ]]; then + echo "Running npm audit fix..." + npm audit fix +fi + +echo "Quality check complete." From a7321a6925f3642495789c60e0ac823d50142e59 Mon Sep 17 00:00:00 2001 From: Eugene Date: Mon, 11 Dec 2023 15:59:38 +0100 Subject: [PATCH 2/2] Part 2. Automate tasks with JSON files --- .../update-pipeline-definition.sh | 164 ++++++++++++++++++ 1 file changed, 164 insertions(+) create mode 100644 lab_1/baranov_yevgen_home_work_task-2/update-pipeline-definition.sh diff --git a/lab_1/baranov_yevgen_home_work_task-2/update-pipeline-definition.sh b/lab_1/baranov_yevgen_home_work_task-2/update-pipeline-definition.sh new file mode 100644 index 0000000..607805b --- /dev/null +++ b/lab_1/baranov_yevgen_home_work_task-2/update-pipeline-definition.sh @@ -0,0 +1,164 @@ +#!/bin/bash + +# Check for the presence of JQ +jq_path=$(command -v jq) + +if [ -z "$jq_path" ]; then + echo "JQ not found. To install JQ:" + echo "On Ubuntu/Debian: sudo apt-get install jq" + echo "On CentOS/Fedora: sudo yum install jq" + echo "On macOS (via Homebrew): brew install jq" + echo "On Windows (via Chocolatey): choco install jq" + exit 1 +fi + +# Function to display usage help for the script +show_help() { + echo "Usage: $0 [additional_options]" + echo "Additional options:" + echo " -b, --branch Set the Branch property value" + echo " -o, --owner Set the Owner property value" + echo " -r, --repo Set the Repo property value" + echo " -p, --poll-for-source-changes Set the PollForSourceChanges property value (true/false)" + echo " -c, --configuration Set the BUILD_CONFIGURATION property value" + echo " --help Display this help message" + echo + echo "Usage Examples:" + echo " $0 ./pipeline.json --branch feat/cicd-lab --poll-for-source-changes true" +} + +# If --help argument is passed, show help and exit the script +if [ "$1" == "--help" ]; then + show_help + exit 0 +fi + +# Check if the argument with the path to JSON file is passed +if [ $# -lt 1 ]; then + echo "Executing only actions 1.1 and 1.2: removing the metadata property and incrementing the version property" + echo "Usage: $0 [additional_options]" + echo "Use '$0 --help' for instructions on script usage." + exit 1 +fi + +input_file="$1" + +# Check if the file with the specified path exists +if [ ! -f "$input_file" ]; then + echo "File $input_file not found." + exit 1 +fi + +# Request for the pipeline’s definitions file path +read -p "Please enter the pipeline’s definitions file path (default: pipeline.json): " input_file +input_file=${input_file:-"pipeline.json"} + +# Request for the BUILD_CONFIGURATION name +read -p "Which BUILD_CONFIGURATION name are you going to use (default: \"\"):" configuration + +# Request for the GitHub repository owner/account +read -p "Enter a GitHub owner/account: " owner + +# Request for the GitHub repository name +read -p "Enter a GitHub repository name: " repo + +# Request for the GitHub branch name (with a default value) +read -p "Enter a GitHub branch name (default: develop): " branch +branch=${branch:-"develop"} + +# Prompt for checking whether to poll for changes (yes/no) +read -p "Do you want the pipeline to poll for changes (yes/no) (default: no)?: " poll_for_source_changes +poll_for_source_changes=${poll_for_source_changes:-"no"} + +# Prompt for saving changes (yes/no) +read -p "Do you want to save changes (yes/no) (default: yes)?: " save_changes +save_changes=${save_changes:-"yes"} + +# Check for JQ existence again +jq_path=$(command -v jq) + +if [ -z "$jq_path" ]; then + echo "JQ not found. To install JQ:" + echo "On Ubuntu/Debian: sudo apt-get install jq" + echo "On CentOS/Fedora: sudo yum install jq" + echo "On macOS (via Homebrew): brew install jq" + echo "On Windows (via Chocolatey): choco install jq" + exit 1 +fi + +# Check if the argument with the path to JSON file is passed +if [ $# -lt 1 ]; then + echo "Executing only actions 1.1 and 1.2: removing the metadata property and incrementing the version property" + echo "Usage: $0 [additional_options]" + exit 1 +fi + +input_file="$1" + +# Check if the file with the specified path exists +if [ ! -f "$input_file" ]; then + echo "File $input_file not found." + exit 1 +fi + +# Check for additional parameters +if [ $# -eq 1 ]; then + jq_filter=' + del(.metadata) | + walk(if type == "object" and has("version") then .version += 1 else . end) + ' + + jq "$jq_filter" "$input_file" > output.json + + echo "The metadata property was removed, the value of the version property was incremented by 1, and saved to the file output.json" + exit 0 +fi + +# Default parameter values +branch="main" +owner="" +repo="" +poll_for_source_changes="false" +configuration="" + +# Read other parameters if passed to the script +while [[ "$#" -gt 0 ]]; do + case $1 in + -b|--branch) branch="$2"; shift ;; + -o|--owner) owner="$2"; shift ;; + -r|--repo) repo="$2"; shift ;; + -p|--poll-for-source-changes) poll_for_source_changes="$2"; shift ;; + -c|--configuration) configuration="$2"; shift ;; + *) echo "Unknown parameter: $1"; exit 1 ;; + esac + shift +done + +jq_filter=' + del(.metadata) | + walk(if type == "object" and has("version") then .version += 1 else . end) | + .pipeline.stages[] | + select(.name == "Source") | + .actions[].configuration |= + if has("Branch") then + .Branch = $branch + else + . + { "Branch": $branch } + end | + if has("Owner") then + .Owner = $owner + else + . + { "Owner": $owner } + end | + if has("Repo") then + .Repo = $repo + else + . + { "Repo": $repo } + end | + .PollForSourceChanges = ($poll_for_source_changes | test("true"; "i")) | + .EnvironmentVariables = [{ "name": "BUILD_CONFIGURATION", "value": $configuration, "type": "PLAINTEXT" }] +' + +jq --arg branch "$branch" --arg owner "$owner" --arg repo "$repo" --arg poll_for_source_changes "$poll_for_source_changes" --arg configuration "$configuration" "$jq_filter" "$input_file" > output.json + +echo "The metadata property was removed, the value of the version property was incremented by 1, properties Branch, Owner, Repo, PollForSourceChanges, and EnvironmentVariables were modified, and saved to the file output.json"