Jenkins AWS Code Utility: a la Carte Menu (agent, templates jobs, others…)

Marvin Ortiz Balliachi
5 min readMar 16, 2022

--

This post is composed by util code recollected in different projects during my personal career like DevOps role using Jenkins as CI/CD server, in fact there are common tasks which need to be implemented again and again in your next project. If you need to improve the performance and make easier the automation process to do a CI/CD Jenkins server instance you must read, copy and paste those useful attributes in your Jenkins server and its pipelines.

Honestly, I could make by each topic presents in this post a different article but it’s totally unnecessary make it, instance try to watch this post like a restaurant menu where you can take your preference and implement in your work, project or wherever you want.

Prerequisite:

Check out my post “Deploy a Simple Jenkins on AWS Using Terraform

Menú / Agenda

  1. Add an agent automatically.

2. Create different jobs from a template, according to a git flow strategic.

Trigger a job from other job.

Customize the label task.

Remember, those additional scripts, files and configurations need to be added in your process to create your Jenkins server automagically.

1. Add an agent automatically.

In short, an agent is a container or machine on which connects to a Jenkins controller in order to execute tasks and for this reason is mandatory to have it, please tab in the following link to depth-in official site. As you can see in following image you will be able to create an agent with those steps in Jenkins.

  1. Create one
  2. Launch the agent by command line or UI Jenkins.

Next part are steps to automate this process and create automatically an agent.

  1. Create a xml file where you could setup a job, watch out the repo. Please add a label which will be called by a pipeline.

2. Add this script in your main function using jenkins-cli to create and launch an agent, please pay attention with the name agentgeneral which is present in the previous xml and the line 4.

Final result, create and launch an agent with a label assoated.

2. Create different jobs from a template, according to a git flow strategic.

Please don’t use an unique job to build, test, merge, release and deploy artifact because tasks like build, merge and others will be mixed inside a unique job and it will so hard to recognize with task in the same job make a deploy or a build process. You could create and use a same pipeline to make the process CI/CD but you need to divide the same pipeline in different process according a devops strategy.

There are several strategies but the following template job was made to set up git flow strategy in your Jenkins project, creating different jobs like deploy, feature, hotfix and so on, relate to an artifact or any application on which you need to implement with CI/CD. To make it follow the next steps.

1. Create a xml file where will be located all jobs, this is a template for your project and could be reused for other project (great !!!). In our case this process is going to create Release, Deploy, Hotfix and Feature jobs. Look into the repo here.

Notice you could customize your job setting up attributes like name job, schedule its execution, user, branch and its url, and so on. Further, you could add another jobs like Merge to manage al branch which will be merged to trunk and other ones, feel free to add them.

2. Using jenkins-cli in your automation process, you will be able to create the template job (line 5) where will could executed to create jobs related to a project. Look at the script here.

Pay attention, “gitflow-generic-template” name could be replaced by other name according your standar.

3. Once you create and deployed your Jenkins server instance the template job will be shown like this image, “gitflow-generic-template” job could be executed to establish all different jobs relate to devops git flow strategy.

NOTE: A good practice is to create a folder relate to your project, this practice is useful to organize all process related to your project and separate your project of the rest of the group. Otherwise, you will find all jobs amount in the same plane, mixed and disorganised.

Tap in New item > type the project name (in our case CRAFT-UP) + select Folder option > click on OK button > Next scream complete the box text + click on Save button.

Execute the template job with your preference values, type the name, where folder will be located (previous step) and the repo URL.

Once the job is executed, the result will be watched with successful goals.

Final result, all jobs related to your git flow strategy (or whatever you establish) created and you will could customize them, look and get in the Deploy job on how this job has its own parameters.

--

--

Marvin Ortiz Balliachi

Software engineer … looking to share and contribute with my knowledge