问题:构建jenkins groovy管道脚本的正确方法

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

脚本:

    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 pipeline 事件来清除工作区(现在它只是 en echo

解答

  1. 流水线有2种。像你写的那样直接的 groovy 被称为 scripted 管道。周围有pipeline{}块的样式是 declarative 样式管道。对于新的 Pipeline 用户来说,声明式往往更容易,并且是从管道开始的不错选择。许多管道不需要脚本允许的复杂性。

  2. 这很时髦。如果你想声明一堆变量,你必须在某个地方做。否则,您将这些值硬编码在脚本中的某个地方。在 groovy 中,您不必声明每个变量,但必须在某处定义它,除非您知道声明将如何影响范围,否则您应该声明它们。大多数编程语言都需要某种变量声明,尤其是当您必须担心范围时,所以我不认为这是一个问题。我认为在顶部的一个位置定义所有变量值非常干净。更易于维护。

  3. 乍一看,您的并行执行看起来应该可以工作,但除非我设置并运行它,否则很难说。可能是并行部分运行得足够快,以至于 UI 没有更新。如果这些并行运行,您应该能够在控制台输出中看到。

4.post流水线块在脚本化流水线中不可用。这是声明性管道语法的一部分。在脚本中,要执行类似的操作,您必须使用 try/catch 来捕获错误并运行 post-type 操作。

Logo

CI/CD社区为您提供最前沿的新闻资讯和知识内容

更多推荐