Skip to content

Ci script

CI Bash script

The following can be used as a template for a ci.bash file. This can then be run as in a CI Pipeline.

The script below is setup for a Symfony project with PHPQA installed but it should be customisable to different projects.

Important things to note:

  • All commands that you want to run should be in the main runFulPipeline function
  • Any new functionality should be added to new functions and then called.
  • Any variables should be defined at the top of the file and then referenced in the script rather than hardcoded further down
#!/usr/bin/env bash
readonly DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )";
cd ${DIR};
set -eE # We need the big `E` for this to apply to functions https://stackoverflow.com/a/35800451
set -u
set -o pipefail
standardIFS="$IFS"
IFS=$'\n\t'
echo "
===========================================
$(hostname) $0 $@
===========================================
"

# Database Connection details
ciDatabaseName='ci_database';
ciDatabaseUser='ci_user';
ciDatabasePass='password123';

# Directories to be created that aren't tracked in git
requiredDirectories=(
    "${DIR}/var"
    "${DIR}/var/qa"
);

# Env file to copy across for the CI Environment
ciEnvFile="${DIR}/.env.ci";
liveEnvFile="${DIR}/.env";

# Where the qa command is
qaCommand="${DIR}/bin/qa"

# Where the results should be logged
qaLogFile="${DIR}/qa.log"

# This will echo a message and tail the log. Importantly it will redirect those commands output to FD4 which we can then
# redirect to stdout when running the main command. This then allows us to redirect all of the standard output to a log
# file and wrap everything in a function making it much easier to write and capture the commands
function tailAndExitOnError() {
  echo "\
Error in the QA process!
========================
Some details below - full log is here: 
--------------------------------------
${qaLogFile}
" >&4
  tail ${qaLogFile} -n 100 >&4
  exit 1
}


trap "tailAndExitOnError " ERR

function setupDatabase() {
    databaseName=$1;
    databaseUser=$2;
    databasePass=$3;
    mysql -e "DROP DATABASE IF EXISTS ${databaseName}"
    mysql -e "CREATE DATABASE ${databaseName}"
    mysql -e "GRANT ALL ON ${databaseName}.* TO '${databaseUser}'@'localhost' IDENTIFIED BY '${databasePass}'"
}

function setupEnvFiles() {
    if [[ -f ${liveEnvFile} ]]
    then
      rm -f ${liveEnvFile}
    fi

    cp ${ciEnvFile} ${liveEnvFile}
}

function runQa() {
    ${qaCommand}
}

function runFullPipeline() {
    printf "QA Process Started at %s\n\n" "$(date)"

    echo "
Creating Database and User
==========================
    "
    setupDatabase ${ciDatabaseName} ${ciDatabaseUser} ${ciDatabasePass}

    echo "
Creating Required Directories
=============================
    "
    for directory in "${requiredDirectories[@]}"
    do
    mkdir -p ${directory}
    done

    echo "
Setting up the .env file
========================
    "
    setupEnvFiles

    echo "
Running Composer Install
========================
    "
    composer install

    echo "
Running QA Pipeline
===================
    "
    runQa

    printf "QA Process Finished at %s\n\n" "$(date)"
}

# The order of and numbers in the re-directions in command are important - don't change them
runFullPipeline 4>&1 &>${qaLogFile}

echo "
----------------
$(hostname) $0 QA Passed Successfully
----------------
"