Automated deployment via Jenkins
If you are someone who is supposed to manage deployment on multiple environments or on clustered environment, you know how painful or time-consuming it can be manually. This post will enable you to automate deployment across multiple environments, from Jenkins branch of your choice.
Well there are few prerequisites before we start, read below:
- You have your shell script(s) ready on servers you are looking to deploy build
- ssh from your Jenkins server to target servers is enabled
- You will need a project in Jenkins which you can configure as Auto-Deployment project. Preferably copy of your existing project which you are currently using for so that you get most of the config copied
- Publish over SSH, Build Authorization Token Root Plugin, Slack Plugin plugins are installed in Jenkins(explained in Step- 2)
Step- 1
Create standard structure on each target server, e.g. /home/Autobuild or /u01/mydummyproject/Autobuild
Within Autobuild folder, keep your deployment script which Jenkins will be calling and also create one more directory within Autobuild folder named as build
Step- 2
Login to Jenkins via admin user and navigate to manage Jenkins>>manage Plugins
There search above listed plugins in the available tab, if not found go to advance tab where you can upload .hpi file for each plugin and activate it.
Step- 3
Navigate to your Jenkins project’s customize tab to start customizing it.
Add two parameters by checking ‘This build is Parameterized’ option.
- First Parameter will be ‘Choice Parameter’ which will be used to decide on which server/instance you want to deploy your build on. Give some logical name to this parameter, e.g. server
- Second Parameter will be ‘List subversion tags’ which will enable you to select desired repository while triggering build. Mention path till root repository in Repository URL box. Give some logical name to this parameter, e.g. repository
Step-4
Within Source Code Management section, Replace build number in all repository URLs with ${<second_parameter>}, e.g. ${repository}
You’ll have to pass this parameter while triggering your Auto-Build, in Jenkins or remotely.
Step- 5
- Navigate to Send build artifacts over SSH block and add all your servers there mentioning artifacts you want to copy(Source Files), Remote Directory where those should be copied(Remote Directory) and path of shell script you want to trigger(Exec command)
- You can go on adding as many servers you want from your clustered environment
- Click on Advanced Setting within each Server block you added and add username-password of target server followed by a test to make sure SSH is enabled from Jenkins server to target server.
- Jenkins will decide on what all servers to deploy build based on ‘Label’ option available here in Advanced section where you have configured credentials in above step. For each Server block, add value you from the Parameter values you configured in Step-3, e.g. QC Box. You’ll have to pass this parameter while triggering your Auto-Build, in Jenkins or remotely.
Done!!! You can now trigger the build in configured Project, pass on the parameters and life should be easy for you after that.
Trigger Jenkins build from #Slack or direct URL
Well, you have very little left to do here if you are done with above section of configuration. If not, complete it first.
Step-1: Go to Configure page of your Project in Jenkins again. There look for option Build Triggers>>Trigger builds remotely check box and check it(You’ll see this only if ).
Step-2: Define a token there which you will be using as payload while triggering build remotely via URL.
Step-3: Create a URL using format available under Authentication token box.
e.g.http://<jenkins_ip>:<jenkins_port>/job/myTestProject/buildWithParameters?token=123456789101112131415&repository=trunk&server=QC_Box
Step-4: Integrate your Jenkins CI app with your slack team from here
Step-5: If your Jenkins is accessible externally(public URL) then
You can add Slack command to your team(which will trigger Jenkins build and fetch result back to slack channel) by following create custom command option from this link
OR
Alternatively, you can use a bot app called pogo after integrating it with slack. You’ll have to add him to your team where you can feed him with Jenkins URL we constructed in Step-3 and give key word to associate with it.
Your team members can then hit the keyword which will fetch the URL saved. Click it and relax, you shall receive all notifications, test results, build changes back in your channel.
Thank you for reading, hope it was useful in someway. Post your feedback/questions in comments.
Preethishetty
23 Mar 2018The way of explanation in step by step process is so interesting and understandable. Thanks for sharing the suggestions that you are contributed here…
Jenkins Online Training
Dinesh
31 Jul 2019Hi
In this given example URL
http://:/job/myTestProject/buildWithParameters?token=123456789101112131415&repository=trunk&server=QC_Box
My requirement is to give dynamic value for “trunk”
How can i achieve this