Python Builds with Artifactory - Scripted Pipeline Syntax

JFrog Integrations Documentation

Content Type
Integrations
ft:sourceType
Paligo

Tip

We recommend using the integration with the JFrog Jenkins Plugin, rather than using the following DSL.

Python builds can resolve dependencies, deploy artifacts and publish build-info to Artifactory. To run Python builds with Artifactory start by following these steps, to make sure your Jenkins agent is ready:

  1. Make sure Python is installed on the build agent and that the python command is in the PATH.

  2. Install pip. You can use the Pip Documentation and also Installing packages using pip and virtual environments.

  3. Make sure wheel and setuptools are installed. You can use the Installing Packages Documentation.

  4. Validate that the build agent is ready by running the following commands from the terminal:

Output Python version:
> python --version

Output pip version:
> pip --version

Verify wheel is installed:
> wheel -h

Verify setuptools is installed:
> pip show setuptools

Verify that virtual-environment is activated:
> echo $VIRTUAL_ENV

In your Pipeline script, you first need to create an Artifactory server instance, as described in the Creating an Artifactory Server Instance section.

Here's an example:

def server = Artifactory.server 'my-server-id'

The next step is to create an Artifactory a Pip Build instance:

def rtPip = Artifactory.newPipBuild()

Now let's define where the build should download its dependencies from. We set the Artifactory server instance we created earlier and the repository name on the resolver:

rtPip.resolver repo: 'pypi-virtual', server: server

It is mostly recommended to run pip commands inside a virtual environment, to achieve isolation for the pip build. To follow this recommendation, create a shell command which sets up a virtual environment. Let's save this shell command in a variable, we'll soon use:

def virtual_env_activation = "source /Users/myUser/venv-example/bin/activate"

Now we can download our project's dependencies as follows:

def buildInfo = rtPip.install args: "-r python-example/requirements.txt", envActivation: virtual_env_activation

You'll need to adjust value of theargsargument in the above command, to match your Python project. Notice that we sent the command for activating the virtual env as the value of the envActivationargument. This argument is optional.

The above method returns a buildInfo instance. If we already have a buildInfo instance we'd like to reuse, we can alternatively send the buildInfo as an argument as shown below. Read thePublishing Build-Info to Artifactory section for more details.

rtPip.install buildInfo: buildInfo, args: "-r python-example/requirements.txt", envActivation: virtual_env_activation

Jenkins spawns a new java process during this step's execution.

You have the option of passing any java args to this new process, by passing the javaArgs argument:

def buildInfo = rtPip.install args: "-r python-example/requirements.txt", javaArgs: '-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005' 

In most cases, your build also produces artifacts. The artifacts produced can be deployed to Artifactory using the server.upload method, as described in the Uploading and Downloading Files section in this article.

You can now publish the build-info to Artifactory as described in the Publishing Build-Info to Artifactory section

Examples

It is highly recommended to use these example projects as a reference, when setting up yout first pip build.