blob: 36abbd7afd0e23be1c6544f77c7158a4194ed332 [file] [log] [blame]
/**
* JS testing pipeline
* CREDENTIALS_ID - gerrit credentials id
* COMPOSE_PATH - path to compose file in repository
* NODE_IMAGE - NodeJS with NPM Docker image name
* COMMANDS - a list of command(s) to run
**/
gerrit = new com.mirantis.mk.Gerrit()
common = new com.mirantis.mk.Common()
def executeCmd(user, containerName, cmd) {
stage(cmd) {
assert containerName != null
common.infoMsg("Starting command: ${cmd}")
wrap([$class: 'AnsiColorBuildWrapper']) {
sh("docker exec --user=${user} ${containerName} ${cmd}")
}
common.successMsg("Successfully completed: ${cmd}")
}
}
def gerritRef
try {
gerritRef = GERRIT_REFSPEC
} catch (MissingPropertyException e) {
gerritRef = null
}
def defaultGitRef, defaultGitUrl
try {
defaultGitRef = DEFAULT_GIT_REF
defaultGitUrl = DEFAULT_GIT_URL
} catch (MissingPropertyException e) {
defaultGitRef = null
defaultGitUrl = null
}
def checkouted = false
node("vm") {
def containerName
def uniqId
try {
stage('Checkout source code') {
if (gerritRef) {
// job is triggered by Gerrit
checkouted = gerrit.gerritPatchsetCheckout ([
credentialsId : CREDENTIALS_ID,
withWipeOut : true,
])
} else if(defaultGitRef && defaultGitUrl) {
checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID)
}
if(!checkouted){
throw new Exception("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null")
}
}
stage('Start container') {
def workspace = common.getWorkspace()
def timeStamp = new Date().format("HHmmss", TimeZone.getTimeZone('UTC'))
if (gerritRef) {
uniqId = gerritRef.tokenize('/').takeRight(2).join('') + timeStamp
} else {
uniqId = defaultGitRef.tokenize('/').takeRight(2).join('') + timeStamp
}
sh("docker-compose -f ${COMPOSE_PATH} -p ${uniqId} up -d")
containerName = "${uniqId}_devopsportal_1"
common.successMsg("Container with id ${containerName} started.")
}
def jenkinsUID = common.getJenkinsUid()
def jenkinsGID = common.getJenkinsGid()
def jenkinsUser = "${jenkinsUID}:${jenkinsGID}"
executeCmd(jenkinsUser, containerName, "npm install")
def cmds = COMMANDS.tokenize('\n')
for (int i = 0; i < cmds.size(); i++) {
timeout(5) {
executeCmd(jenkinsUser, containerName, cmds[i])
}
}
} catch (err) {
currentBuild.result = 'FAILURE'
common.errorMsg("Build failed due to error: ${err}")
throw err
} finally {
common.sendNotification(currentBuild.result, "" ,["slack"])
stage('Attach artifacts') {
if (containerName != null) {
archiveArtifacts(
artifacts: "test_output/screenshots/*.png",
)
}
}
stage('Cleanup') {
if (containerName != null) {
dockerCleanupCommands = ['stop', 'rm -f']
for (int i = 0; i < dockerCleanupCommands.size(); i++) {
sh("docker-compose -f ${COMPOSE_PATH} -p ${uniqId} ${dockerCleanupCommands[i]} || true")
}
sh("docker network rm ${uniqId}_default || true")
common.infoMsg("Container with id ${containerName} was removed.")
}
}
}
}