Created Mar 13, 2018

Ionic Framework push with Jenkins Declarative

This gist has been created to document one successful way I have found to use Declarative pipeline + nodeJS + Ionic Framework
and preview the work through the Ionic SaaS Platform.
More on this can be found on the Ionic website

Prerequisites

Jenkins/Jenkins:LTS

Amazon EC2 plugin:1.38

NodeJS Plugin:1.2.4

NodeJS 8.9.4

Env Setup

For this task, I am currently using Jenkins LTS docker image, runnning on an EC2 instance.
No executors are being used on the master(BAD PRACTICE)
I am currently using the EC2-Cloud plugin to create slaves from an AMI.

Jenkins Configuration

I am currently using a Multi-branch pipeline. I connect to my Bitbucket repositories and use segmented
Declarative Pipeline based executions.

Variable Declaration

There are a number of variables that I used to parametrize and hide credentials or other attributes in Jenkins

environment{
//This is the unique identifier of your ionic project
IONIC_PROJECT = "abc12345"
//This is the credential object that Jenkins has to login to Ionic
IONIC = credentials("my_Ionic_user")
//This is a secret text file that I use to store the SSH key used to talk to Ionic
KEY = credentials("jenkins_Private_Key")
}

Jenkinfile Code

Below is the raw code for how we execute the upload to Ionic SaaS

//Define this stage as part of my pipeline
stage('IonicView Deploy') {
//set where I am going to run the stage.
agent { label 'jenkins_worker' }
steps {
//Cleaning the workspace helps me know I start fresh
cleanWs()
//checkout the code I want deployed to Ionic
git branch: '$BRANCH_NAME', credentialsId: 'bitbucket_serviceaccount', url: "${GITHUB_URL}"
//Informs me of the repo layout when I download it. This is more error reporting.
sh "ls -ltra"
/**Even though we will use shell, I always try to use this nodejs block to ensure node is installed
In this block I do a number of things. I make sure I have the Ionic framework installed, I reverify the linking between my code and the Ionic project, and then I deal with connecting via SSH to Ionic.
Once that is all done, I push the code to Ionic
**/
nodejs(nodeJSInstallationName: 'nodejs_8.9.4', configId:null) {
sh """
npm install -g ionic@latest
ionic login \${IONIC_USR} \${IONIC_PSW} --confirm --no-interactive
ionic link --pro-id ${IONIC_PROJECT} --confirm --no-interactive
ionic git remote --no-interactive
ionic ssh add /home/ec2-user/.ssh/authorized_keys
ionic ssh use /home/ec2-user/.ssh/my_random_key.pem --confirm
ssh-keyscan git.ionicjs.com >> ~/.ssh/known_hosts
git push ionic HEAD:\${BRANCH_NAME}
"""
}
}
}