如何为Django应用程序编写Groovy Jenkins文件来运行我的测试?

mat*_*gan 5 django groovy jenkins

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

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

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'
}
Run Code Online (Sandbox Code Playgroud)

小智 1

venv/bin/activate无非是建立适当的环境路径。

env.WORKSPACE您可以通过在开头添加(假设这是您的项目目录)来自己完成此操作:

env.PATH="${env.WORKSPACE}/venv/bin:/usr/bin:${env.PATH}"
Run Code Online (Sandbox Code Playgroud)

稍后,如果你想调用 virtualenved python,只需在其前面加上指定的路径,如下所示:

stage 'Test'
sh "${env.WORKSPACE}/venv/bin/python34 manage.py test --keepdb'
Run Code Online (Sandbox Code Playgroud)

或者调用 pip

sh "${env.WORKSPACE}/venv/bin/pip install -r requirements.txt"
Run Code Online (Sandbox Code Playgroud)