Sign up to take part
Registered users can ask their own questions, contribute to discussions, and be part of the Community!
Added on September 30, 2020 3:17PM
Likes: 3
Replies: 6
In this post, we will show how to set up a sample CI/CD (continuous integration / continuous deployment) pipeline built on Jenkins for our Dataiku DSS project. It follows our blog post Continuous integration and continuous deployment (CI/CD) in Dataiku DSS that presents the concepts and some important questions in order to fully optimize CI/CD.
In order to be at ease with this article, you will need to know about DSS flows, scenarios, and automation. I strongly recommend following the Operationalization series from the Dataiku Academy. In addition, an understanding of Jenkins and the basics of Artifactory are required, plus basic Python skills, including pytest ideally.
Our CI/CD environment will be made of:
We will use the Dataiku DSS Prediction churn sample project, with a twist at the end with a Python recipe. The code itself is not really important; we need some Python code to showcase code analysis.
Note: You can find all the files used in this project attached to this article as dss_pipeline-master.zip
The first step is to create a project in Jenkins of the “Pipeline” type. Let’s call it dss-pipeline-cicd
.
We will use the following parameters for this project:
The pipeline contains five stages and one post action:
The post action will be used to clean up the bundle zip file from the local jenkins workspace to save space and will also retrieve all the xUnit test reports.
As additional global notes, we are using a global variable bundle_name
so that we can pass this information from one stage to another. This variable is computed using a shell script with the date & time of the run (the script is explained after it is displayed).
You can find the groovy file of the pipeline in the zip: pipeline.groovy. In this file, you have the definition of the different stages and for each stage the details of the steps.
Let’s review those steps one by one.
This stage is used to build a proper workspace. The main tasks are to get all the CI/CD files we need from your GitHub project and build the right Python3 environment using the requirements.txt file.
Since the Dataiku DSS API python package is retrieved from a node directly, we are using the Design node URL provided as parameter for that.
This stage contains mostly Python scripts used to validate that the project respects internal rules for being production-ready. Any check can be performed at this stage, be it on the project structure, setup, or the coding parts, such as the code recipes or the project libraries.
Note that we are using pytest capability to use command-line arguments by adding a conftest.py.
This is very specific to each installation, but here are the main takeaways:
‘TEST_’
.If this state is OK, we know we have a properly written project, and we can package it.
The first part of this stage is using a Python script to create a bundle of the project and download it locally on the Jenkins executor.
The second part is using a Jenkins stage to publish this bundle on our Artifactory repository generic-local/dss_bundle/
. Note that the stage will fail if no file is published (the “failNoOp: true” option) so there is no need for an extra check.
In this stage, we are deploying the bundle produced at the previous stage on our DSS PREPROD instance and then running tests.
The bundle import is done in import_bundle.py and is straightforward: import, preload, activate. In this example, we consider connection mappings are automatically done. If you need specific mappings, this requires some more work using the API (see XXX)
The following script run_test.py executes all the scenarios named TEST_XXX and fails if a result is not a success.
You can check on the blog article why we are using DSS scenarios.
This pytest configuration has an additional twist. If you have only one test running all the TEST_XXX scenarios, they will be reported to Jenkins as a single test, successful or failed.
Here, we make this nicer by dynamically creating one unit test per scenario. In the final report, we will have one report per scenario executed, making the report more meaningful. This requires some understanding of pytest parameterization. Note that you can perfectly keep one test that will run all your scenarios if you are not feeling at ease with this.
The previous stage verified that we have a valid package. It’s time to move it to production!
Again using Python with script import_bundle.py, we will upload the bundle to the production node using the same logic as in PREPROD.
The second script prod_activation.py will handle the activation and the rollback. For that, here are the main steps:
The Post Actions phase allows us to clean locally downloaded zip files and publish all test xUnit reports in Jenkins to have a nice test report. Those reports were produced all along the pipeline by pytest and are here aggregated into a single view to have something like:
Now we have seen a step-by-step demonstration of how to build a solid CI/CD pipeline with Jenkins. If you want to use this and adapt it to your setup, here is a checklist of what you need to do:
And then hit ‘Build with parameters’.
You can of course improve this startup kit, and here are some ideas:
And of course, add as many test scenarios as possible that will ensure a reliable continuous deployment.
Refer to this issue:
Pipeline-fails-with-import-error-for-dataiku
Can you share an example on how to use pylint here instead of radon?
I think there is error in the file 'prod_activation.py' line 50:
should be 'previous_bundle_id' instead of 'bundle_id'
Thanks @arielma2304
for your feedbacks on this article. The code has been fixed in the sample.
As for the pylint sample, I would suggest to try the following: grab the python code from DSS and save it as a python file locally and then pass this file to pylint with something like pylint mymodule.py.
Hi
About the file 1_project_validation/run_test.py, and idea how can I can results for multiple scenarios which starts with TEST? assuming I have 5 scenarios in the project, and 2 of them start with TEST, I want to see 2 test results
Good morning,
The example for the preproduction unit tests is using a different approach to tests by using pytest parametrization with the pytest_generate_tests() function. This allows you to run and return as many test results as there are scenarios to execute.
I am not sure I see what would be the status check you want to perform in your case. The preparation step in the demo is about controlling that the structure of the project is good so the existence or number of scenarios is the metric to control. Here, do you want to execute the scenario and check their status? (in which case, the 3_preprod_test example is good) or do you just want to have one fake test result for each existing scenario (using the parametrization with a simple 'assert True' would do the job, although it looks a bit weird)?