Last active
May 6, 2023 12:50
-
-
Save Enigo/c75009ad2bbbfd7d35a5cd753fbd631f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!groovy | |
import groovy.transform.Field | |
@Field | |
def s3BucketFolder | |
pipeline { | |
agent { label 'agent' } | |
parameters { | |
string(name: 'AMI_ID', trim: true) | |
string(name: 'REGION', trim: true) | |
string(name: 'SUBNET', trim: true) | |
} | |
stages { | |
stage('Launch instance from ami') { | |
steps { | |
script { | |
withAWS(role: "${JENKINS_ROLE}", roleAccount: "${AWS_ACCOUNT}", region: "${REGION}") { | |
env.INSTANCE_ID = sh(returnStdout: true, script: "aws ec2 run-instances --image-id ${AMI_ID.trim()} " + | |
"--tag-specifications 'ResourceType=instance,Tags=[{Key=Env,Value=test},{Key=Type,Value=serverspec}]' " + | |
"--instance-profile instance-serverspec " + | |
"--instance-type t3.small " + | |
"--subnet ${SUBNET} " + | |
"--query 'Instances[].InstanceId' " + | |
"--output text").trim() | |
sh "aws ec2 wait instance-status-ok --instance-ids ${INSTANCE_ID}" | |
} | |
} | |
} | |
} | |
stage('Run serverspec tests') { | |
steps { | |
script { | |
withAWS(role: "${JENKINS_ROLE}", roleAccount: "${AWS_ACCOUNT}", region: "${REGION}") { | |
s3BucketFolder = UUID.randomUUID().toString() | |
echo "Installing serverspec on ${INSTANCE_ID}" | |
def commandId = sh(returnStdout: true, script: "aws ssm send-command " + | |
"--instance-ids '${INSTANCE_ID}' " + | |
"--document-name 'AWS-RunShellScript' " + | |
"--comment 'Serverspec' " + | |
"--parameters 'commands=[\"apt install ruby-rspec-core -y && gem install serverspec\"]' " + | |
"| jq -r '.Command.CommandId'").trim() | |
echo "Waiting for command ${commandId} to finish" | |
def commandNotSuccessful = sh(returnStatus: true, script: "aws ssm wait command-executed --command-id ${commandId} --instance-id ${INSTANCE_ID}") != 0 | |
if (commandNotSuccessful) { | |
echo "Command ${commandId} failed!" | |
sh "aws ssm get-command-invocation --command-id '${commandId}' --instance-id '${INSTANCE_ID}'" | |
error("Couldn't install serverspec!") | |
} | |
echo "Serverspec was successfully installed! Uploading rspec files to 'serverspec/${s3BucketFolder}'" | |
dir("serverspec/spec") { | |
sh """ | |
specs=`ls ./` | |
for file in \$specs | |
do | |
aws s3api put-object --bucket serverspec --key $s3BucketFolder/\$file --body \$file | |
done | |
""" | |
} | |
echo "Rspec files uploaded successfully! Downloading files to ${INSTANCE_ID}" | |
commandId = sh(returnStdout: true, script: "aws ssm send-command " + | |
"--instance-ids '${INSTANCE_ID}' " + | |
"--document-name 'AWS-RunShellScript' " + | |
"--comment 'Serverspec' " + | |
"--parameters 'commands=[\"aws s3 sync s3://serverspec/${s3BucketFolder} /tmp/\"]' " + | |
"| jq -r '.Command.CommandId'").trim() | |
commandNotSuccessful = sh(returnStatus: true, script: "aws ssm wait command-executed --command-id ${commandId} --instance-id ${INSTANCE_ID}") != 0 | |
if (commandNotSuccessful) { | |
echo "Command ${commandId} failed!" | |
sh "aws ssm get-command-invocation --command-id '${commandId}' --instance-id '${INSTANCE_ID}'" | |
error("Couldn't download serverspec files!") | |
} | |
echo "Files downloaded successfully! Running the tests now" | |
commandId = sh(returnStdout: true, script: "aws ssm send-command " + | |
"--instance-ids '${INSTANCE_ID}' " + | |
"--document-name 'AWS-RunShellScript' " + | |
"--comment 'Serverspec' " + | |
"--parameters 'commands=[\"rspec --format documentation --pattern /tmp/*.rb \"]' " + | |
"| jq -r '.Command.CommandId'").trim() | |
commandNotSuccessful = sh(returnStatus: true, script: "aws ssm wait command-executed --command-id ${commandId} --instance-id ${INSTANCE_ID}") != 0 | |
echo "Checking tests output" | |
def output = sh(returnStdout: true, script: "aws ssm get-command-invocation " + | |
"--command-id '${commandId}' --instance-id '${INSTANCE_ID}' | jq -r '.StandardOutputContent'").trim() | |
echo "$output" | |
if (commandNotSuccessful) { | |
error("There are some test failures! Check the output above") | |
} | |
} | |
} | |
} | |
} | |
} | |
post { | |
always { | |
script { | |
withAWS(role: "${JENKINS_ROLE}", roleAccount: "${AWS_ACCOUNT}", region: "${REGION}") { | |
sh "aws s3 rm s3://serverspec/$s3BucketFolder --recursive" | |
sh "aws ec2 terminate-instances --instance-ids ${INSTANCE_ID}" | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment