构造jenkins groovy管道脚本的正确方法

时间:2017-10-25 12:31:51

标签: jenkins groovy

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

SCRIPT:

    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. 我没有在Jenkins工作流内部看到并行性,即使我有4个执行器处于空闲状态。
  4. 我无法调用post管道事件来清除工作区(现在是rigth,它只是en echo

1 个答案:

答案 0 :(得分:1)

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