Skip to content

Instantly share code, notes, and snippets.

@shqear93
Last active October 5, 2020 13:47
Show Gist options
  • Save shqear93/6dac07368dcb17ae7dd090150346e5c9 to your computer and use it in GitHub Desktop.
Save shqear93/6dac07368dcb17ae7dd090150346e5c9 to your computer and use it in GitHub Desktop.
Jenkins trigger for amplify with logs streaming
pipeline {
agent any
options { skipDefaultCheckout() }
environment {
AWS_DEFAULT_REGION = 'eu-west-1'
}
stages {
stage('Deploy') {
agent {
docker {
image 'mikesir87/aws-cli:2.0.22'
args '-u 111:115'
}
}
steps {
withCredentials([[$class : 'AmazonWebServicesCredentialsBinding',
credentialsId: 'khaled-waraqa']]) {
script {
APP_ID = sh(
label: 'Get Waraqa Frontend App ID',
returnStdout: true,
script: 'aws amplify list-apps --output text --query "apps[?name == \'aqlamy-angular\'].appId | [0]"'
)
// create amplify branch if not exists
BRANCH_STATUS = sh(
label: "Checking branch ${BRANCH_NAME}...",
returnStatus: true,
script: "aws amplify get-branch --branch-name ${BRANCH_NAME} --app-id ${APP_ID}"
)
if (BRANCH_STATUS == 255) {
echo "${BRANCH_NAME} branch doesn't exists, creating new branch"
sh "aws amplify create-branch --no-enable-auto-build --branch-name ${BRANCH_NAME} --app-id ${APP_ID}"
} else {
// Raise exception if any other reason!
if (BRANCH_STATUS != 0) {
currentBuild.result = 'FAILURE'
error("Error! AWS CLI ERROR #${BRANCH_STATUS}")
}
}
JOB_ID = sh(
label: 'Requesting new deployment job..',
returnStdout: true,
script: "aws amplify start-job --branch-name ${BRANCH_NAME} --output text --job-type RELEASE --query 'jobSummary.jobId' --app-id ${APP_ID}"
).toInteger()
sh(
label: 'Waiting the new deployment job..',
script: "while [ \"\$(aws amplify get-job --branch-name ${BRANCH_NAME} --output text --job-id ${JOB_ID} --query 'job.summary.status' --app-id ${APP_ID})\" = \"PENDING\" ]; do sleep 2; done"
)
// get amplify steps data from AWS
JSON_DATA = sh(
label: 'Get job information',
returnStdout: true,
script: """
aws amplify get-job --output json --branch-name ${BRANCH_NAME} --job-id ${JOB_ID} --app-id ${APP_ID}
"""
)
readJSON(text: JSON_DATA).job.steps.eachWithIndex { step, index ->
if (step.stepName == 'VERIFY') {
status = sh(
label: step.stepName.toLowerCase().capitalize(),
returnStatus: true,
script: """
set +x
while true
do
STATUS=\$(aws amplify get-job --query \'job.steps[$index].status\' --output text --branch-name ${BRANCH_NAME} --job-id ${JOB_ID} --app-id ${APP_ID})
if [ "\${STATUS}" != "RUNNING" ] && [ "\${STATUS}" != "PROVISIONING" ]
then
if [ "\${STATUS}" = "SUCCEED" ]; then exit 0; fi
if [ "\${STATUS}" = "CANCELLING" ]; then exit 1; fi
if [ "\${STATUS}" = "CANCELLED" ]; then exit 1; fi
if [ "\${STATUS}" = "FAILED" ]; then exit 2; fi
else
sleep 2
fi
done
"""
)
switch (status) {
case 1:
currentBuild.result = 'ABORTED'
error("${step.stepName} has been Aborted!")
break;
case 2:
currentBuild.result = 'FAILURE'
error("${step.stepName} has been failed!")
}
} else {
UP_URL = step.logUrl.replace('&', '\\&')
LOG_FILE_NAME = "${step.stepName}_${BUILD_ID}.log"
status = sh(
label: step.stepName.toLowerCase().capitalize(),
returnStatus: true,
script: """
set +x
touch ${LOG_FILE_NAME}
while true; do sleep 2; wget -ca ${LOG_FILE_NAME} -o /dev/null -O ${LOG_FILE_NAME} ${UP_URL}; done &
set -x
tail -f ${LOG_FILE_NAME} &
set +x
while true
do
STATUS=\$(aws amplify get-job --query \'job.steps[$index].status\' --output text --branch-name ${BRANCH_NAME} --job-id ${JOB_ID} --app-id ${APP_ID})
if [ "\${STATUS}" != "RUNNING" ] && [ "\${STATUS}" != "PROVISIONING" ]
then
if [ "\${STATUS}" = "SUCCEED" ]; then exit 0; fi
if [ "\${STATUS}" = "CANCELLING" ]; then exit 1; fi
if [ "\${STATUS}" = "CANCELLED" ]; then exit 1; fi
if [ "\${STATUS}" = "FAILED" ]; then exit 2; fi
else
sleep 2
fi
done
"""
)
switch (status) {
case 1:
currentBuild.result = 'ABORTED'
error("${step.stepName} has been Aborted!")
break;
case 2:
currentBuild.result = 'FAILURE'
error("${step.stepName} has been failed!")
}
}
}
}
}
}
}
}
}
@shqear93
Copy link
Author

This pipeline will trigger amplify job and stream it's logs, you don't need to go to AWS console to check the deployment process

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment