Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I write a Groovy Jenkinsfile for a Django application to run my tests?

I have recently started using Jenkins and I am wanting to use Multibranch Pipelines so I can test the various feature branches in my project.

The project is using django 1.8. So far my Jenkinsfile looks like this and fails in the testing stage as django can not find my settings file even though it is there:

node {
    // Mark the code checkout 'stage'....
    stage 'Checkout'

    // Get the code from a GitHub repository
    git credentialsId: 'mycredentials', url: 'https://github.com/<user>/<project>/'

    // Mark the code build 'stage'....
    stage 'Build'

    env.WORKSPACE = pwd()

    sh 'virtualenv --python=python34 venv'
    sh 'source venv/bin/activate'

    sh 'pip install -r requirements.txt'

    env.DJANGO_SETTINGS_MODULE = "<appname>.settings.jenkins"

    // Start the tests
    stage 'Test'
    sh 'python34 manage.py test --keepdb'
}
like image 739
mattjegan Avatar asked Nov 09 '22 12:11

mattjegan


1 Answers

venv/bin/activate does no more than setting up proper environmental paths.

You can do it on your own by adding at the beginning assuming that env.WORKSPACE is your project directory:

env.PATH="${env.WORKSPACE}/venv/bin:/usr/bin:${env.PATH}"

Later, if you want to call virtualenved python, just need to prepend it with specified path like here:

stage 'Test'
sh "${env.WORKSPACE}/venv/bin/python34 manage.py test --keepdb'

Or to call pip

sh "${env.WORKSPACE}/venv/bin/pip install -r requirements.txt"
like image 109
Rafal Avatar answered Nov 15 '22 05:11

Rafal