如何为Django应用程序编写Groovy Jenkins文件来运行我的测试? [英] How can I write a Groovy Jenkinsfile for a Django application to run my tests?

查看:115
本文介绍了如何为Django应用程序编写Groovy Jenkins文件来运行我的测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我开始使用Jenkins,我想要使用Multibranch Pipelines,所以我可以测试我的项目中的各种功能分支。



该项目正在使用django 1.8。到目前为止,我的Jenkinsfile看起来像这样,在测试阶段失败,因为django找不到我的设置文件,即使它在那里:

  node {
//标记代码检出'stage'....
stage'Checkout'

//从GitHub存储库获取代码
git credentialsId:'mycredentials',url:'https://github.com/<user>/<project>/'

//标记代码生成'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

//开始测试
stage'Test'
sh'python34 manage.py test --keepdb'
}


解决方案

venv / bin / activate 设置适当的环境路径。



您可以自行添加,假设 env.WORKSPACE 是您的项目目录:

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

稍后,如果要调用virtualenved python,只需要用这样的指定路径预先添加:

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

或调用pip

  sh$ {env.WORKSPACE} / venv / bin / pip install -r requirements.txt


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'
}

解决方案

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"

这篇关于如何为Django应用程序编写Groovy Jenkins文件来运行我的测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆