Jenkins python build script

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Python script execution job using the Jenkins pipeline

SharonKarichaly/Jenkins-Python-Pipeline

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Читайте также:  Массив из кнопок java

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

How to execute python script using the Jenkins pipeline

Jenkins is a continuous open-source integration written in Java. it is a Continuous Integration and Continuous delivery tool. We can orchestrate application deployments using Jenkins with a wide range of freely available community plugins and native Jenkins workflows.

Screenshot 2022-07-26 at 9 25 53 PM

Jenkins is commonly used for the following.

  1. Continuous Integration for application and infrastructure code.
  2. Continuously deliver pipeline to deploy the application to different environments using Jenkins Pipeline as a code
  3. Infrastructure component deployment and management.
  4. Run batch operations using Jenkins jobs.
  5. Run ad-hoc operations like backups, cleanups, remote script execution, event triggers, etc

Jenkins pipeline allows us to define a complete list of events that happen in the code lifecycle. Starting from the build, to testing and deployment. We can use a set of plugins that help in the implementation of certain processes as a continuous delivery pipeline. Where pipelines are defined using code by using groovy language to define the processes that would run in the pipeline.

Here we are executing a python script job using the Jenkins pipeline

In this project I pushed my project code to GitHub which is linked with Jenkins, which is responsible to execute a python script job using the Jenkins pipeline

This is a simple python script used to fetch the below details from a server.

2.IP address of the machine

3.Total available memory of the machine

4.Load average of the machine

5.Disk usage of the machine

I used socket and subprocess modules in the python to get this details.

Stage 1: Use checkout option in pipeline syntax generator to link the repository with the Jenkins server and select the branch where code is present. since it’s a public repository, I am not using any credentials/tokens.

Stage 2: Use shell script option in pipeline syntax generator to execute the python script

Stage 3: Use shell script option in pipeline syntax generator to redirect the terminal output to a file

Build and verify the stage logs

Screenshot 2022-07-26 at 10 40 14 PM

Login to the server and verify the details in /var/lib/jenkins/workspace/Repository_folder/

Screenshot 2022-07-26 at 10 46 08 PM

About

Python script execution job using the Jenkins pipeline

Источник

Run a Python Script from Jenkins

Jenkins is a continuous integration and delivery platform that helps automate the software development process. One way to use Jenkins is by defining a Jenkinsfile in the root of your project and checking it into version control. This Jenkinsfile allows you to define your pipeline as code, which can be versioned, reused, and shared across projects.

This tutorial will show you how to run a Python script from a Jenkins pipeline. We will be using the Jenkins Pipeline syntax to accomplish this.

Before we get started, make sure that you have the following prerequisites:

  1. A Jenkins instance
  2. A Python script that you want to run
  3. The Python interpreter installed on the Jenkins controller or Agent

Jenkins Run Python Script in Pipeline

We can run a Python script within a Jenkins pipeline using the sh command in Jenkins. Let us see how we can do this.

Start by creating a new Jenkins pipeline. To do this, go to the Jenkins dashboard, click on the “New Item” link, and then choose the “Pipeline” option.

Give the pipeline a name and click the “OK” button.

Next, we will need to define the Jenkins pipeline. There are two ways to do this:

We will be using the Declarative Pipeline syntax in this tutorial.

To define the pipeline, we need to specify a series of stages where each stage represents a specific step in the pipeline.

In this case, we will create a single stage that runs our Python script. An example pipeline is as shown in the example below:

pipeline {
agent {
label ‘python’
}
stages {
stage ( ‘Run Python Script’ ) {
steps {
sh ‘python3 script.py’
}
}
}
}

Let us go through each section of this Jenkinsfile in more detail:

  1. The pipeline block allows us to define the start of our pipeline.
  2. Next, we use the agent block to specify the agent used to run our pipeline. In this case, we are using the label directive to specify that we want to use a Jenkins agent with the label “python.” This ensures that the pipeline will run on a machine with the Python interpreter installed.
  3. The stages block defines a series of stages in our pipeline. In this case, we only have a stage called “Run Python Script.”
  4. In the next section, the steps block to define the steps that will be executed within the stage. We are using the sh directive to run a shell command in this case. The command we are running is python3 script.py which will execute our Python script.

Once we have defined the pipeline, we can save and run it by clicking the “Build Now” button on the Jenkins dashboard.

If the pipeline runs successfully, we should see the output of the Python script in the Jenkins console output.

We can also use the Console Output to diagnose any errors and fix them for the job to run successfully.

Conclusion

In this article, you learned how to use the sh directive in a Jenkins pipeline to run a Python script.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list

Источник

Automate Jenkins Job builds with Python

We often come across a situation when we have to manually run a Jenkins job many times each possibly having different parameters. One way to do it is using the Jenkins UI and pressing the build with parameters button each time and passing on the parameter values each time. This can be quite tiresome and repetitive. Fortunately, I am too Lazy to do it like that xD. So, I found another way, we can trigger the Jenkins jobs using the Jenkins REST API. In this post, we will demonstrate how to design our Jenkins jobs effectively for this purpose and use python to make use of the Jenkins API to automate triggering a huge number of jobs and then take a chill pill 🙂

Step 1: Enable Triggering Jenkins jobs from remote scripts

Trigger builds remotely in Jenkins

Navigate to your Jenkins Job configuration page and scroll down to the Build Triggers option. Enable the Trigger builds remotely (e.g., from scripts) flag and put some secret string in the Authentication Token field.

Step 2: Parameterize Job -> GIT SCM (BRANCH, REPO ETC.)

Jenkins Job Parameterize project

We need to make our Job bash script read from parameters we passed in to the Job so that we can easily control these parameters from our python script. Furthermore, if there’s a git repo/branch our Job needs to checkout we can easily control that from python as well.
Enable the This project is parameterized flag from the Jenkins job configuration page and add the required parameters like below,
Jenkins supports various different types of parameters including Strings, Boolean, Choices, File etc. Make sure you are choosing them appropriately for your choice. In my case for example, I had to deploy a bunch of Ansible playbooks to a Kubernetes cluster so, I needed the following parameters:

PLAYBOOK - name of the yaml ansible playbook file GIT_REPO_ID - git repo to checkout GIT_BRANCH_NAME - branch name of the repo (particularly useful when you are testing/reviewing something on a feature branch) HELM_REPOSITORY - helm repository name that my ansible playbooks needs 

Now, access these parameters wherever you need inside the Jenkins config using the $ syntax. For example my job bash script looks like the one just below where, I am utilizing the PLAYBOOK and HELM_REPOSITORY parameters.

#!/bin/bash set -xe pip3 install --quiet -r requirements.txt cd ansible ANSIBLE_FORCE_COLOR=true ansible-playbook -e "helm_repository=$HELM_REPOSITORY>" \ playbooks/$PLAYBOOK> -vvvv 

Jenkins GIT SCM plugin parameters

Here’s another example where I am utilizing the GIT SCM parameters,

Step 3: Generate Jenkins Crumb

Jenkins API will need us to pass in a Jenkins-Crumb header to our requests. Therefore, we need to generate this crumb value from the Jenkins CrumbIssuer before making the actual build trigger requests. Here’s how we can generate the Jenkins Crumb using python:

def jenkins_crumb(): session = requests.Session() session.auth = ("", "") crumb = session.get("https:///crumbIssuer/api/json") return crumb.request.headers["Authorization"], crumb.json()["crumb"] 

Step 4: Trigger Jenkins Job from Python

TOKEN = "" # the token value you set in STEP-1 def deploy_service( service_name, playbook, branch="master", repo="ashiqursuperfly/jenkins-job-config", chart_repo="ashiqursuperfly-portfolio-dev" ): JOB_URL = f"https:///job//buildWithParameters?token=TOKEN>" url = f"JOB_URL>&PLAYBOOK=playbook>&GIT_BRANCH_NAME=branch>&GIT_REPO_ID=repo>&HELM_REPOSITORY=chart_repo>" cookie, crumb = jenkins_crumb() headers = "Jenkins-Crumb": crumb> session = requests.Session() session.auth = ("", "") res = session.get(url=url, headers=headers) if res.ok: print(f"Started deploy job for service service_name> with playbook playbook>") else: print( f"Error deploy job for service service_name> with playbook playbook>\nres.json()>" ) if __name__ == "__main__": list_of_service = list_services() for service in list_of_service: res, playbook = get_playbook_name(service) deploy_service(service, playbook) sleep(100) 

Buy Me A Beer

Now, we can invoke the following method as many times as we want. In my case, I just looped through my list of services to deploy and invoked this method for each service and just watched Jenkins complete all the tedious work for me as I sipped my coffee, basked in the brightness of my monitor’s light, and wondered why life is so simple when you can’t stop thinking automation 🤓 If you found this post helpful, CLICK BELOW 👇

Источник

Оцените статью