大网数据广告活动
服务器运维问答

Per-job environments in Jenkins with virtualenv

· 52CE 我爱测

I'm trying to use virtualenv to programmatically manage Python environments for each job on a Jenkins server, implemented via a Shared Library extension to activate environments on a per job basis. E.g.: /vars/activateEnvironment…

问题原文

原帖链接 · cwh · CC BY-SA 3.0

I'm trying to use virtualenv to programmatically manage Python environments for each job on a Jenkins server, implemented via a Shared Library extension to activate environments on a per job basis. E.g.:

/vars/activateEnvironment.groovy:

def call(String env = "/usr/local/etc/environments/jenkins-$JOB_NAME") {

    sh """
    mkdir ${env}
    virtualenv ${env}
    source ${env}/bin/activate
    """
}

Pipeline script, in which the virtualenv-scripts repository contains the above file:

@Library('virtualenv-scripts') _

pipeline {
    agent any
    stages {
        stage("Test") {
            steps {
                activateEnvironment()
                sh 'which pip'
                sh 'echo \$PATH'
            }
        }
    }
}

Running this pipeline script, I get the following output:

[Pipeline] sh
[example-pipeline] Running shell script
+ echo /sbin:/usr/sbin:/bin:/usr/bin
/sbin:/usr/sbin:/bin:/usr/bin
[Pipeline] sh
[example-pipeline] Running shell script
+ which pip
/bin/pip

I've tried using this answer to make Jenkins use a login shell, but that still reloads the environment with every sh call.

I also saw this answer which would require pasting extra text every time a sh step is used in a Pipeline -- not ideal.

Is there a good way to have environment persist between sh commands? Alternately, is there a better way to achieve per-job environments with virtualenv? Thanks for all help/suggestions!

已采纳解答

原帖链接 · jayhendren · CC BY-SA 3.0

I had the same issue. After talking with some veteran Jenkins admins, this is the solution I arrived at:

def runCommandInMyEnvironment(cmd) {
  sh "setup_environment_command; source ./some/file; ${cmd}"
}

pipeline {
  agent any
  stages {
    stage("My Stage") {
      steps {
        runCommandInMyEnvironment('first_command')
        runCommandInMyEnvironment('second_command')
        // and so on
      }
    }
  }
}

It's not pretty and it can muddy up the console output quite a bit, but it's also the most reliable way to do this.

Another approach would be to parse the output of some command and chop it up into a bunch of environment variables and then pass those to a withEnv block, but this can be a very tricky and unreliable approach.

In any case, as you alluded to, Jenkins doesn't support persisting environments without withEnv, so ultimately there's no real good or clean way to do it.

There may be a better way to use virtualenvs with Jenkins, but I've never written a Jenkins job that runs tasks in a virtualenv, so I can't say. There is this plugin, but another stackoverflow answer suggests that the approach I gave in this answer is the preferred method for working with virtualenvs in Jenkins.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-02-16)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。