Installing Spinnaker Using Jenkins

Source: https://github.com/spinnaker/spinnaker.github.io/blob/master/assets/images/spinnaker-logo-transparent-color.png

Problem

Solution

Implementation

docker.image('gcr.io/spinnaker-marketplace/halyard:1.20.2').inside() {
stage('Start Halyard') {
sh label: 'Start Halyard Daemon', script: "./scripts/start-halyard-daemon"
}
...
#!/bin/bash
set -Eeuxo pipefail

# Print the current version of hal
echo "hal version: $(hal -v)"

# Start up halyard in the background with logs sent to /tmp/halyard.log
"/opt/halyard/bin/halyard" > /tmp/halyard.log 2>&1 &

# Wait until halyard has fully started
( tail -f -n +1 /tmp/halyard.log & ) | grep -q -E "Tomcat started|Started Main"
echo "halyard daemon started"
docker.image('gcr.io/spinnaker-marketplace/halyard:1.20.2').inside('-u root') {
stage('Start Halyard') {
sh label: 'Start Halyard Daemon', script: 'su spinnaker -c "./scripts/start-halyard-daemon"'
}
...
docker.image('bash:5.0.7').inside('-u root') {
stage('Prepare scripts') {
dir('scripts') {
sh 'chmod 755 *'
sh 'adduser spinnaker -D'
sh 'chown spinnaker:spinnaker *'
}
}
}
#!/bin/bash
set -Eeuxo pipefail

GITHUB_ACCOUNT_NAME="$1"
GITHUB_PAT_FILE="$2"

if [[ -z "$GITHUB_ACCOUNT_NAME" || -z "$GITHUB_PAT_FILE" ]]; then
echo 'one or more variables are undefined:'
echo ' GITHUB_ACCOUNT_NAME($1) - name of github account as it will appear in spinnaker'
echo ' GITHUB_PAT_FILE($2) - path to a file containing a personal access token that will be used by spinnaker to authenticate to GitHub'
exit 1
fi

# make sure artifacts are enabled
hal config features edit --artifacts true

# https://www.spinnaker.io/setup/artifacts/github/
hal config artifact github enable
hal config artifact github account add "$GITHUB_ACCOUNT_NAME" \
--token-file "$GITHUB_PAT_FILE"
#!/bin/bash
set -Eeuxo pipefail

S3_ACCOUNT_NAME="$1"

if [[ -z "$S3_ACCOUNT_NAME" || -z "$AWS_ACCESS_KEY_ID" || -z "$AWS_SECRET_ACCESS_KEY" || -z "$AWS_REGION" ]]; then
echo 'one or more variables are undefined:'
echo ' S3_ACCOUNT_NAME($1) - name of s3 account as it will appear in spinnaker'
echo ' AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_REGION - AWS environment variables'
exit 1
fi

# make sure artifacts are enabled
hal config features edit --artifacts true

# https://www.spinnaker.io/setup/artifacts/github/
hal config artifact s3 enable
echo "$AWS_SECRET_ACCESS_KEY" | hal config artifact s3 account add "$S3_ACCOUNT_NAME" \
--aws-access-key-id "$AWS_ACCESS_KEY_ID" \
--region "$AWS_REGION" \
--aws-secret-access-key
stage('Configure Artifacts') {
// Get the personal access token for accessing github
withCredentials([file(credentialsId: 'SPINNAKER_GITHUB_ARTIFACTS', variable: 'GITHUB_PAT_TOKEN_FILE')]) {
// Execute the script to configure github artifacts
sh label: 'Github', script: "su spinnaker -c './scripts/configure-artifacts-github my-github-user \"${GITHUB_PAT_TOKEN_FILE}\"'"
}
// Get the aws access keys for accessing s3 artifacts
withCredentials([usernamePassword(credentialsId: 'SPINNAKER_AWS_ACCESS', passwordVariable: 'AWS_SECRET_ACCESS_KEY', usernameVariable: 'AWS_ACCESS_KEY_ID')]) {
// Execute the script to configure s3 artifacts
sh label: 'S3 Bucket ${SPINNAKER_S3_BUCKET_ARTIFACTS}', script: "su spinnaker -c './scripts/configure-artifacts-s3 my-spinnaker-s3-demo'"
}
}
#!/bin/bash
set -Eeuxo pipefail

VERSION="$1"

if [[ -z "$VERSION" || -z "$CRED_DIR" ]]; then
echo 'one or more variables are undefined:'
echo ' VERSION($1) - version of spinnaker to be installed'
exit 1
fi

# List the available versions of spinnaker that can be deployed
hal version list

# Set the desired version to be deployed
hal config version edit --version "$VERSION"

# Execute the spinnaker deployment
hal deploy apply
stage('Hal Deploy') {
sh label: 'Deploy Spinnaker', script: "su spinnaker -c './scripts/deploy \"${SPINNAKER_DEPLOY_VERSION}\"'"
}
docker.image('gcr.io/spinnaker-marketplace/halyard:1.20.2').inside('-u root') {// all configuration and deployment steps done here    
...
stage('Hal Config Backup') {
sh label: 'generate backup file', script: "su spinnaker -c 'hal backup create'"
sh 'cp /home/spinnaker/halbackup-*.tar .'
sh 'chmod 777 halbackup-*.tar'
// allow jenkins default user to own the file
sh 'chown 1001:1001 halbackup-*.tar'
stash includes: 'halbackup*.tar', name: 'halbackupstash'
}
}
docker.build("awscli", "-f Dockerfile.awscli .").inside {
stage('Push Spinnaker Backup to S3') {
unstash 'halbackupstash'
sh 'ls -la halbackup-*.tar'
// Get the aws access keys for accessing s3 artifacts
withCredentials([usernamePassword(credentialsId: 'SPINNAKER_AWS_ACCESS', passwordVariable: 'AWS_SECRET_ACCESS_KEY', usernameVariable: 'AWS_ACCESS_KEY_ID')]) {
sh 'aws s3 cp halbackup*.tar s3://$SPINNAKER_S3_BUCKET_BACKUP --region $AWS_REGION'
}
}
}

Conclusion

node {
try {
checkout scm
// SPINNAKER_DEPLOY_VERSION sets the version of spinnaker that will be deployed
env.SPINNAKER_DEPLOY_VERSION = "1.14.4"
docker.image('bash:5.0.7').inside('-u root') {
stage('Prepare scripts') {
dir('scripts') {
sh 'chmod 755 *'
sh 'adduser spinnaker -D'
sh 'chown spinnaker:spinnaker *'
}
}
}
docker.image('gcr.io/spinnaker-marketplace/halyard:1.20.2').inside('-u root') {
stage('Start Halyard') {
sh label: 'Start Halyard Daemon', script: 'su spinnaker -c "./scripts/start-halyard-daemon"'
}
stage('Configure Artifacts') {
// Get the personal access token for accessing github
withCredentials([file(credentialsId: 'SPINNAKER_GITHUB_ARTIFACTS', variable: 'GITHUB_PAT_TOKEN_FILE')]) {
// Execute the script to configure github artifacts
sh label: 'Github', script: "su spinnaker -c './scripts/configure-artifacts-github my-github-user \"${GITHUB_PAT_TOKEN_FILE}\"'"
}
// Get the aws access keys for accessing s3 artifacts
withCredentials([usernamePassword(credentialsId: 'SPINNAKER_AWS_ACCESS', passwordVariable: 'AWS_SECRET_ACCESS_KEY', usernameVariable: 'AWS_ACCESS_KEY_ID')]) {
// Execute the script to configure s3 artifacts
sh label: 'S3 Bucket ${SPINNAKER_S3_BUCKET_ARTIFACTS}', script: "su spinnaker -c './scripts/configure-artifacts-s3 my-spinnaker-s3-demo'"
}
}
stage('Hal Deploy') {
sh label: 'Deploy Spinnaker', script: "su spinnaker -c './scripts/deploy \"${SPINNAKER_DEPLOY_VERSION}\"'"
}
stage('Hal Config Backup') {
sh label: 'generate backup file', script: "su spinnaker -c 'hal backup create'"
sh 'cp /home/spinnaker/halbackup-*.tar .'
sh 'chmod 777 halbackup-*.tar'
// allow jenkins default user to own the file
sh 'chown 1001:1001 halbackup-*.tar'
stash includes: 'halbackup*.tar', name: 'halbackupstash'
}
}
docker.build("awscli", "-f Dockerfile.awscli .").inside {
stage('Push Spinnaker Backup to S3') {
unstash 'halbackupstash'
sh 'ls -la halbackup-*.tar'
// Get the aws access keys for accessing s3 artifacts
withCredentials([usernamePassword(credentialsId: 'SPINNAKER_AWS_ACCESS', passwordVariable: 'AWS_SECRET_ACCESS_KEY', usernameVariable: 'AWS_ACCESS_KEY_ID')]) {
sh 'aws s3 cp halbackup*.tar s3://$SPINNAKER_S3_BUCKET_BACKUP --region $AWS_REGION'
}
}
}
}
catch (e) {
echo "========Pipeline Failed========"
throw e
}
finally {
stage('Cleanup') {
echo "========Cleaning up the mess we made========"
cleanWs()
}
}
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store