Docker

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

Exploring Jenkins DSL – Preface: Up and Running with Docker & Jenkins

While working on my multi-part series covering the Job-DSL plugin for Jenkins, I wanted to get my own Jenkins instance stood up so that I could test & play without affecting any of our shared instances. That’s exactly what’s being covered here. Getting a brand new, personal Jenkins instance up and running so that we can test DSL code without affecting anyone else. I’m going to be using Docker for this walk thru and I can’t believe how easy/simple the whole process was!! So please, if you’re following along in my DSL exploration – start here and get your own environment to destroy!!

Assuming you have Docker already installed, let’s go ahead and pull the latest Jenkins image which amounts to just this line of code:

docker pull jenkins

 

docker_pull_jenkins

And once the image is available locally, we can go ahead and run it like this:

docker run -p 8080:8080 -p 50000:50000 jenkins

In the latest version (I think all versions starting at 2.x), Jenkins requires an admin password. The initial admin password will be located in the output somewhere. I've highlighted it here, although yours will absolutely be different:

docker_run_admin_password

Copy the initial admin password because you'll need it when trying to login to your Jenkins instance. My PowerShell session isn't returned to me, but that's ok. When I see the line "setting agent port for jnlp" I know that I'm ready to go. In my browser, all I need to do is navigate to localhost:8080 where I should see a Jenkins login screen. Remember that admin password you copied to your clipboard? This is where you'll use it. At the next screen, I'm just going to select "Install Suggested Plugins" for this demo:

jenkins_customize_plugins

After the recommended plugins are installed, I'll install the job-dsl & greenballs plugins via the "Manage Jenkins" >"Manage Plugins" screen. Greenballs? Why must we have that for the demo? Because, in my mind, a representation of a successful job is (and always will be) green. I just wish they'd change the default color of successful runs from blue to green so I didn't need to install an additional plugin.

jenkins_install_plugin

That's it! Surprisingly, I was up and running in just a few minutes with very little Docker experience!  I slotted a couple of hours for this exercise figuring the learning curve on Docker alone would drag me down a bit. Nope! Now I have a sandbox Jenkins instance to experiment on without risking a bad script taking down my production Jenkins instance (or even my test instance).