jenkins - 构建 jenkins Groovy 管道脚本的正确方法

标签 jenkins groovy

我编写了一个与 jenkins 一起使用的管道,但作为 jenkins 脚本的新手,我有很多不清楚的东西,这是整个脚本,我将在下面表达问题

脚本:

    node()
{
    def libName = "PROJECT"
    def slnPath = pwd();
    def slnName = "${slnPath}\\${libName}.sln"
    def webProject = "${slnPath}\\PROJECT.Web\\PROJECT.Web.csproj"
    def profile = getProperty("profiles");
    def version = getProperty("Version");
    def deployFolder = "${slnPath}Deploy";
    def buildRevision = "";
    def msbHome = "C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Professional\\MSBuild\\15.0\\Bin\\msbuild.exe"
    def msdHome = "C:\\Program Files (x86)\\IIS\\Microsoft Web Deploy V3\\msdeploy.exe"

    def nuget = "F:\\NugetBin\\nuget.exe";

    def assemblyScript = "F:\\Build\\Tools\\AssemblyInfoUpdatePowershellScript\\SetAssemblyVersion.ps1";

    def webserverName ="192.168.0.116";

    def buildName = "PROJECT";
    def filenameBase ="PROJECT";

    stage('SCM update')
    {
        checkout([$class: 'SubversionSCM', additionalCredentials: [], excludedCommitMessages: '', excludedRegions: '', excludedRevprop: '', excludedUsers: '', filterChangelog: false, ignoreDirPropChanges: false, includedRegions: '', locations: [[credentialsId: '08ae9e8c-8db8-43e1-b081-eb352eb14d11', depthOption: 'infinity', ignoreExternalsOption: true, local: '.', remote: 'http://someurl:18080/svn/Prod/Projects/PROJECT/PROJECT/trunk']], workspaceUpdater: [$class: 'UpdateWithRevertUpdater']])
    }

stage('SCM Revision')
    {
    bat("svn upgrade");
    bat("svn info \"${slnPath}\" >revision.txt");

        for (String i : readFile('revision.txt').split("\r?\n"))
        {
            if(i.contains("Last Changed Rev: "))
            {
                def splitted = i.split(": ")

                echo "Revisione : "+ splitted[1];

                buildName += "." + splitted[1];
                currentBuild.displayName = buildName;
                buildRevision += version + "." + splitted[1];
            }
        }
    }
    stage("AssemblyInfo update")
    {
            powerShell("${assemblyScript} ${buildRevision} -path .") 
     }
    stage('Nuget restore')
    {
        bat("${nuget} restore \"${slnName}\"")
    }

    stage('Main build')
    {
        bat("\"${msbHome}\" \"${slnName}\" /p:Configuration=Release /p:PublishProfile=Release /p:DeployOnBuild=true /p:Profile=Release ");

        stash includes: 'Deploy/Web/**', name : 'web_artifact'
        stash includes: 'PROJECT.Web/Web.*', name : 'web_config_files'

        stash includes: 'output/client/release/**', name : 'client_artifact'
        stash includes: 'PROJECT.WPF/App.*', name : 'client_config_files'

        stash includes: 'PROJECT.WPF/Setup//**', name : 'client_setup'
    }

    stage('Profile\'s customizations')
    {
        if (profile != "")
        {
            def buildProfile = profile.split(',');

        def stepsForParallel = buildProfile.collectEntries {
            ["echoing ${it}" : performTransformation(it,filenameBase,buildRevision)]

        }

        parallel stepsForParallel;
        } 
    }

post
{
    always
    {
        echo "mimmo";
    }
}

}


def powerShell(psCmd) {
bat "powershell.exe -NonInteractive -ExecutionPolicy Bypass -Command \"\$ErrorActionPreference='Stop';[Console]::OutputEncoding=[System.Text.Encoding]::UTF8;$psCmd;EXIT \$global:LastExitCode\""
}

def performTransformation(profile,filename,buildRevision) {
 return {
    node {
        def ctt ="F:\\Build\\Tools\\ConfigTransformationTool\\ctt.exe";
        def nsiTool = "F:\\Build\\Tools\\NSIS\\makensis.exe";
        def slnPath = pwd();

        unstash 'web_artifact'
        unstash 'web_config_files'

        def source = 'Deploy/Web/Web.config';
        def transform = 'PROJECT.Web\\web.' + profile + '.config';

        bat("\"${ctt}\" i s:\"${source}\" t:\"${transform}\" d:\"${source}\"" )

        def  fname= filename + "_" + profile + "_" + buildRevision + "_web.zip";

        if (fileExists(fname))
            bat("del "+ fname);

        zip(zipFile:fname, dir:"Deploy\\Web")

        archiveArtifacts artifacts: fname

        //Now I generate the client part
        unstash 'client_artifact'
        unstash 'client_config_files'
        unstash 'client_setup'

        def sourceClient = 'output/client/release/PROJECT.WPF.exe.config';
        def transformClient = 'PROJECT.WPF/App.' + profile + '.config';

        bat("\"${ctt}\" i s:\"${sourceClient}\" t:\"${transformClient}\" d:\"${sourceClient}\"" )


        def directory = new File(pwd() + "\\output\\installer\\")

        if(!directory.exists())
        {
            bat("mkdir output\\installer");
        }

        directory = new File( pwd() + "\\output\\installer\\${profile}")

        if(!directory.exists())
        {
            echo " directory does not exist";
            bat("mkdir output\\installer\\${profile}");
        }
        else
        {
            echo " directory exists";
        }

           def  filename2= filename + "_" + profile + "_" + buildRevision + "_client.zip";

            bat("${nsiTool} /DAPP_VERSION=${buildRevision} /DDEST_FOLDER=\"${slnPath}\\output\\installer\\${profile}\" /DTARGET=\"${profile}\" /DSOURCE_FILES=\"${slnPath}\\output\\client\\release\" \"${slnPath}\\PROJECT.WPF\\Setup\\setup.nsi\"  ");

           if (fileExists(filename2))
              bat("del "+ filename2);
           zip(zipFile:filename2, dir:"output\\installer\\" + profile);

           archiveArtifacts artifacts: filename2
    }
 }
 };

这一系列问题是:

  1. 我见过一些脚本,其中所有内容都包含在管道 {} 中,这是必要的还是 Jenkins 管道插件会粘贴它?
  2. 我真的不喜欢将所有这些定义放在节点内,然后在下面复制。
  3. 即使我有 4 个执行器处于空闲状态,我也没有在 Jenkins 工作流程中看到并行性。
  4. 我无法调用管道后事件来清除工作区(现在它只是 en echo

最佳答案

  1. 有两种类型的管道。像您所写的那样的直接groovy被称为脚本化管道。周围有 pipeline{} block 的样式是声明式样式管道。对于管道的新用户来说,声明式往往更容易,并且是开始使用管道的不错选择。许多管道不需要脚本所允许的复杂性。
  2. 这太棒了。如果你想声明一堆变量,你必须在某个地方做。否则,您将在脚本中的某个位置对这些值进行硬编码。在 groovy 中,您不必声明每个变量,但必须在某个地方定义它,除非您知道声明将如何影响作用域,否则您应该只声​​明它们。大多数编程语言都需要某种变量声明,特别是当您必须担心范围时,所以我不认为这是一个问题。我认为在顶部的一处定义所有变量值是非常干净的。维护更方便。
  3. 乍一看,你的并行执行看起来应该可以工作,但除非我设置它并运行它,否则很难说。可能是并行部分运行得足够快,导致 UI 没有更新。您应该能够在控制台输出中看到它们是否并行运行。
  4. post 管道 block 在脚本化管道中不可用。这是声明性管道语法的一部分。在脚本中,要执行类似的操作,您必须使用 try/catch 来捕获错误并运行 post-type 的操作。

关于jenkins - 构建 jenkins Groovy 管道脚本的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46932539/

相关文章:

git - ERROR : Couldn't find any revision to build. 验证此作业的存储库和分支配置

grails - 如何一般性地访问此Groovy JSON对象?

java - Groovy-修改xml文件值

json - Groovy/Grails-Json Web服务,收到的 map 带有其他属性

json - Jenkins/groovy : How to pretty-print a net. sf.json.JSONObject 为空?

java - 使用 Maven 从 Ubuntu 在 Jenkins 中集成 Selenium

build - 是否可以将 Jenkins 自由式作业转换为多配置作业?

java - 如何在 Java 或 Groovy 中计算 "Friday before"?

groovy - Nexus 3 查询构建器如何按降序检索人工制品

android - 停止 Robotium 测试执行