首页 > 解决方案 > 基于隐藏文件是否存在的条件阶段

问题描述

我正在尝试在 Jenkins 中创建一个管道,其中第一阶段可能会或可能不会生成要存储的文件以供以后使用。我想根据是否存在隐藏文件来使下一阶段成为条件。我不确定如何执行此操作,因为 unstash 步骤出现在“何时”表达式之后。

粗略的轮廓:

第 1 阶段:运行一个脚本,该脚本可以在任何地方生成 0-3 个文件(被隐藏并用作 Ansible 的库存文件)

第 2 阶段:如果 file1 存在,则以 file1 作为清单运行 playbook

第 3 阶段:如果 file2 存在,则使用 file2 运行 playbook

等等..

我尝试使用 fileExists 选项在环境块中设置一个变量,但这将在任何阶段运行之前进行评估。

environment {
  FILE_EXISTS = fileExists './filename'
}
stages {
  stage ('Conditional Stage') {
    when {
      allOf {
        expression { params.FILE_EXISTS == true }
        expression { env.BRANCH_NAME != 'master' }
      }
    }
    steps {
      unstash 'files'
      sh 'ansible ... '
    }
  }
}

(尝试了 params.FILE_EXISTS 和 env.FILE_EXISTS)

还以类似的方式尝试了舞台内的“何时”表达。

stage ('Conditional Stage') {
  when {
    allOf {
      expression { fileExists './filename' }
      ...
    }
  }
  steps {
    unstash 'files'
    sh 'ansible ...'
}

由于有条件,这两种方法都跳过了阶段。

如果可能的话,我想避免归档文件,因为它们只会在当前构建中需要。

有没有人能够做到这一点,或者我错过了一些简单的东西?

一个额外的注意事项:文件将存储在 Windows 从站上,然后在 Linux 从站上取消存储。这不是存储/取消存储的问题,但值得一提的是,文件将始终在与存储位置不同的从站上取消存储。

标签: jenkinsjenkins-pipeline

解决方案


您可以定义一个全局变量,即def FILE_CREATED = 0 并在管道中间修改它,一旦满足特定条件,在这种情况下确保文件存在并将 FILE_CREATED 的值从 0 更新为 1(参见代码以下)。

您可以稍后在一个阶段使用when指令检查该变量的值,方法是使用expression.

下一个管道已经过测试并且可以很好地用于演示目的,它运行阶段仅在特定文件存在时运行一些回显操作,仅当文件“/tmp/demotime1”存在时。

#!/usr/bin/env groovy

def FILE_CREATED = 0

pipeline {

    agent {
        label 'linux'
    }

    stages{

        stage('Show if file exists'){
            steps {
                echo "At first, var FILE_EXISTS value is: $FILE_CREATED"
                echo 'Now check if /tmp/demotime1 exists'
                echo '/tmp/demotime1 It should not exists on the very first execution'
                script {
                    def demofile = new File('/tmp/demotime1')
                    if (!demofile.exists()) {
                        println('That is expected, /tmp/demotime1 file has NOT been created yet')
                    }
                }
            }
        }

        stage('Force file creation and change env var'){
            steps {
                echo 'creating a file'
                sh """ echo 'Demo time file content here' > /tmp/demotime1 """
                echo 'At this time /tmp/demotime1 exists see:'
                sh """ cat /tmp/demotime1 """
                echo "Now let's change env var to true"
                script {
                    if (new File('/tmp/demotime1').exists()){
                        println('File exists and FILE_CREATED should be 1 now')
                    }
                    FILE_CREATED = 1
                }
                echo "Now FILE_EXISTS value is: $FILE_CREATED"
            }
        }

        stage('Run some echo actions ONLY if a specific file exists'){
            when {
                expression {
                    FILE_CREATED == 1
                }
            }
            steps {
                echo 'The file exists so I will do something original...'
                echo 'Hello World, the  $FILE_CREATED has been created'
            }
        }

    }
}

推荐阅读