Month: October 2017

Local Jenkins Master + Slave with Docker

Today, I’ll be adding to my last post about using Docker and Jenkins to have a quick local Jenkins instance locally. The only thing I would recommend changing is to mount a local folder as your Jenkins home directory. This saves your state and doesn’t require you to reconfigure and install your plugins each time. Helpful if you plan to use this occasionally. What I’ll be adding to the original post is how to add your local laptop or workstation as a build slave to the Jenkins master container. Super easy to do and very helpful testing little things (looking at you $LASTEXITCODE) before you commit & push.

 

 

I have already allowed Docker to mount a volume on my laptop. This is in the options menu. Let’s get my Jenkins master container going. Remember, I’m going to mount a local volume to use as the container’s jenkins_home directory. Locally, this is just a directory I created for messing around. I won’t put anything in here that I can’t lose.
docker run -p 8080:8080 -p 50000:50000 -v C:/_docker/jenkins:/var/jenkins_home jenkins/jenkins:lts

If this is your first time setting Jenkins up, make sure to browse my previous post for any manual configurations. You will also need to Install Java. Any old Java will do, I don't think you need the SDK version or anything. Make sure your Java installation is available on your $env:PATH. Anyway, once the Jenkins master comes up, navigate to http://localhost:8080. You can grab your initial admin token from $jenkins_home/secrets/initialAdminPassword with $jenkins_home being the volume configured in the docker run command.

Add a new node:

Jenkins Home >> Manage Jenkins >> Manage Nodes >> New Node

new_node_screen

The new node will be your workstation but it can be configured like a real build agent. You can set labels, executors, environment variables. You will need a slave workspace. I threw mine alongside my $jenkins_home directory.
new_node_config
Now that I have a new node created, I just need to bring it online. Finding the screen is always a little tricky but clicking the offline node object in the home page:
node_config_pane_view
Will display some options to bring the slave online. I think the easier is using the "Launch agent from browser" option. This downloads a .jnlp file which specific configurations for connecting to your Jenkins master instance. You could easily run the slave.jar file if you have a specific version you need.
new_node_launch_command
The new node should be online and ready to use. Because I have added a "powershell" tag to my node configuration for this new slave, I can create a new test pipeline job and run:
node('powershell') {
    stage('Execute Batch') {
        bat 'powershell -command "echo something awesome"'
    }
}
And of course when you get this error you can adjust quickly (because you have a local Jenkins instance to mess around with):
Started by user admin
[Pipeline] node
Running on windows_slave in C:\_docker\jenkins_slave\workspace\test-1
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Execute Batch)
[Pipeline] bat
[test-1] Running batch script
C:\_docker\jenkins_slave\workspace\test-1>powershell -command "echo something awesome"
. : File C:\Users\nhudacin\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 cannot be loaded because
running scripts is disabled on this system. For more information, see about_Execution_Policies at
http://go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:3
+ . 'C:\Users\nhudacin\Documents\WindowsPowerShell\Microsoft.PowerShell ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : SecurityError: (:) [], PSSecurityException
+ FullyQualifiedErrorId : UnauthorizedAccess
something
awesome
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
node('powershell') {
    stage('Execute Batch') {
       bat 'powershell -ExecutionPolicy ByPass -command "echo something awesome"'
    }
}
Pretty cool, right!? Thanks for the read, any questions, comments, or corrections please feel free to comment below!
Advertisements