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
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/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
----------------
"