Scripting Jenkins Through Spinnaker
The Script stage in Spinnaker allows you to run an arbitrary shell, Python or Groovy scripts on a Jenkins instance. It can be incorporated into Spinnaker pipelines as a first-class stage, which allows for powerful integrations and uses (such as launching a test suite from a pipeline, instead of doing it manually). Using the Script stage requires additional setup, however, that is not completely explained in the Spinnaker documentation.
In this post you will learn how to:
- Apply parameters to the Script Stage
- Configure the Script stage to use the most recent docker image
Parameters
Scripting suites often require parameter ingestion to execute the program in a way that the user wants it to be done. When doing functional testing on an application, you would normally want to input parameters that direct the script to a file path, an IP address, or any other variable definitions like a container tag. This article will show you how to tell Spinnaker to execute a script in Jenkins where the script will be run with parameters defined in Spinnaker.
Scripting Deployments
In a production instance of Spinnaker, you would likely utilize a staging prospect before your code is rolled into production. As noted above, it allows you to launch arbitrary actions (such as unit or functional testing) before deploying an application, or promoting an application. This stage takes input from users, which will be the parameters that are carried over into Jenkins to run the script.
Image of the Script Stage in Spinnaker. COMMAND, IMAGE, and CREDENTIALS are all parameters that will be passed into the Jenkins instance.
Script Stage
Spinnaker's script stage works via Jenkins. This means, that in order to use it, you must first set up a Jenkins instance, and then configure Jenkins to work with Spinnaker. When the script stage runs, it triggers a Jenkins job that will execute a testing script on an application. The result of this test will go back to Spinnaker; Spinnaker, in turn, will either proceed or halt deployment based on the outcome of the script.
With a stable Jenkins job integrated into Spinnaker, Jenkins can be used to trigger Spinnaker pipelines in Spinnaker which causes the continuous deployment process to progress. The script stage can be utilized here, by triggering the pipelines to deploy applications if the tests pass, and halting the pipelines from deploying if the tests fail. With a working environment, this process is automated. This saves time by initiating an all-automated process and adds confidence in that broken code will not be deployed to production.
After pulling a Docker image, Spinnaker initiates a test suite with parameters that get passed to Jenkins. If the tests pass in Jenkins, Jenkins will green-light the "Script Stage" to proceed with deploying to production. If the tests fail in Jenkins, further processes are prevented.
In this post, we will assume you have already set up and configured Jenkins with Spinnaker. If you haven't integrated Jenkins with Spinnaker, you can do so by following these guides:
Jenkins / Spinnaker Script Stage Walkthrough
Below will show an example of configuring a Jenkins Pipeline Job that will execute a script that has parameters passed through from Spinnaker.
Note: This example assumes you have pre-configured integration with Jenkins and Spinnaker. If you have not done this process, follow the links provided above.
The process will go as follows:
- Configure a Jenkins Pipeline Job to have parameters in the project. Define the parameters with suitable names.
- Write a Pipeline Script in the Jenkins job. This script will execute inside a docker container and run the test suites.
- Create a Spinnaker Script Stage that passes parameters into the Jenkins job.
Parameters
Within Jenkins and Spinnaker, parameters are a list of arguments that can be provided to the script when it is triggered, allowing for commands to be customized.
Parametrizing in Jenkins is fairly straight forward. In your Jenkins job, there should be a checkbox labeled This project is parameterized. To enable parameters, check the box.
Note: The parameterized option in Jenkins is not available for all Jenkins jobs. The preferred method is using a Jenkins Pipeline job.
Spinnaker includes a set of default parameters that are passed to Jenkins: Command and Image ID. In the example below, we will define these parameters, and then watch how they are passed to Jenkins as part of the pipeline.
Pipeline Script
At the very bottom of a Jenkins' Pipeline Job, there is a Pipeline script option for you to write up your testing procedure.
This input uses the Groovy Sandbox, which is the same as writing up a Jenkinsfile.
Below we will show a coding example of running your application test script from a Docker image.
This image shows the enabling process of parameters within a Jenkins Pipeline Job. The name of each "string parameter" will be implemented into the script that runs the testing procedures.
At the bottom of every Jenkins Pipeline Job lays this script box. This is where you input the commands that will run the test executables inside your container.
Test Script
This script uses the Docker Jenkins plugin to be able to run Docker commands in a Jenkins sandbox environment.
- docker.withRegistry() - connects to a Docker registries URL to pull images
- docker.image() - identifies an image from the defined Docker registry
- ${IMAGE_ID} represents our image ID parameter we set earlier. Spinnaker will define this value in the Script stage
- This image is defined as app
- app.pull() - pulls the defined Docker image
- app.inside() - Executes inside the Docker image which opens up a shell to run any script.
- We run sh '${COMMAND}' to represent our command line within the Docker image that will run our test script inside the application.
node { docker.withRegistry('http://registry.example.com:8000') { def app = docker.image('application/name:${IMAGE_ID}') app.pull() app.inside { sh '${COMMAND}' } } }
Running Jenkins Job
After writing up our script, we can test our Jenkins job to make sure our tests are running properly.
Before running the Jenkins job, you have to set definitions to the parameters of your script - you will be prompted to do so before running the job.
Spinnaker Script Stage
Only minor configuration in Spinnaker is needed from this point.
In Spinnaker, open up a pipeline stage and make it a Script stage.
Notice how parameter input boxes appear:
For Command input, the command line will be used to run the test script within the Docker image.
- If you need to use more than one command within the shell, you can add && to the line.
For Image input, you would want to use the most recent Docker image available. If your Spinnaker pipeline is set up to pull the most recent Docker image from your repository, this input can be handled by a simple Spinnaker Pipeline Expression: ${trigger['tag']}
- This expression uses the Docker image tag that was recently pulled from the original pipeline trigger.
If your Jenkins and Spinnaker are configured to talk to each other, then this is the end of the process. You can manually execute your script stage to test if everything is working properly.
Comments
Loading
No results found