如何在管道的后期部分定义变量?

时间:2019-11-25 11:20:38

标签: jenkins jenkins-pipeline jenkins-groovy

我想在管道的发布部分的不同条件下使用一些通用值,因此我尝试了以下操作-

1。

post {  
      script {
           def variable = "<some dynamic value here>"
      }
      failure{
           script{
                  "<use variable  here>"
           }
     }
     success{
          script{
                "<use variable  here>"
         }
    }
}

2

post {  
          def variable = "<some dynamic value here>"
          failure{
               script{
                      "<use variable  here>"
               }
         }
         success{
              script{
                    "<use variable  here>"
             }
        }
    }

但是它导致编译错误。

您能否建议我如何在post节中声明一个可以在各种条件下使用的变量?

1 个答案:

答案 0 :(得分:0)

您可以使用guaranteed to be executed before any other conditions之类的IF NOT EXISTS(SELECT * FROM [TestHelper].[__EFMigrationsHistory] WHERE [MigrationId] = N'20191004135334_db_sp-CsvImport') BEGIN CREATE PROCEDURE [TestHelper].[CsvImportService] @DvseImportId INT, @Path NVARCHAR(255) AS BEGIN -- DROP TEMP TABLE IF EXITSTS DROP TABLE IF EXISTS [TestHelper].[_tmpDvseImportData] -- CREATE TEMP TABLE CREATE TABLE [TestHelper].[_tmpDvseImportData] ( DataSupplierID INT , DataSupplier NVARCHAR(255) , ArticleNumber NVARCHAR(255) , ArticleNumberNorm NVARCHAR(255) , GenNo INT , GenDescription NVARCHAR(255) , State BIT ) -- BULK INSERT DATA FROM FILESHARE EXEC('BULK INSERT [TestHelper].[_tmpDvseImportData] FROM ''' + @Path + ''' WITH ( FIELDTERMINATOR = '';'',ROWTERMINATOR = ''\n'')') -- STEP 1: FORMAT DATA -- REMOVE DOUBLE QUOTES = CHAR(34) UPDATE [TestHelper].[_tmpDvseImportData] SET DataSupplier = REPLACE(DataSupplier, CHAR(34), '') , ArticleNumber = REPLACE(ArticleNumber, CHAR(34), '') , ArticleNumberNorm = REPLACE(ArticleNumberNorm, CHAR(34), '') , GenDescription = REPLACE(GenDescription, CHAR(34), '') -- STEP 2: REMOVE DUPLICATES ... -- Drop table DROP TABLE [TestHelper].[_tmpDvseImportData] END END; GO IF NOT EXISTS(SELECT * FROM [TestHelper].[__EFMigrationsHistory] WHERE [MigrationId] = N'20191008093824_db_view-DvseSuppliersWithMetaData') BEGIN CREATE VIEW [TestHelper].[DvseSuppliersWithMetaData] AS SELECT supplier.Id , supplier.[Key] , supplier.Code , supplier.[Name] , (SELECT COUNT(*) FROM [TestHelper].[DvseArticle] WHERE SupplierId = supplier.Id) as TotalArticles , (SELECT COUNT(*) FROM [TestHelper].[DvseArticle] WHERE SupplierId = supplier.Id AND IsActive = 1) as TotalActive FROM [TestHelper].[DvseSupplier] AS supplier GROUP BY supplier.Id , supplier.[Key] , supplier.Code , supplier.[Name] END; GO 条件,例如alwayssuccess。如果要存储failure值,则可以使用use String变量来存储它(环境变量总是将给定值强制转换为字符串)。另外,您可以在env之外定义一个全局变量,然后在pipeline条件内用期望的动态值对其进行初始化。考虑以下示例:

always

输出:

def someGlobalVar

pipeline {
    agent any

    stages {
        stage("Test") {
            steps {
                echo "Test"
            }
        }
    }

    post {
        always {
            script {
                env.FOO = "bar"
                someGlobalVar = 23
            }
        }

        success {
            echo "FOO is ${env.FOO}"
            echo "someGlobalVar = ${someGlobalVar}"
        }
    }
}