68

With Jenkins 2 Pipeline plugin, there's a useful feature allowing a quick overview of the pipeline stages and status of steps, including logging output.

However, if you use the "Shell script" (sh) step, there doesn't seem to be a way to label that script with a useful name, so the display merely shows a long list of "Shell Script" (shown in the image below).

How can I assign a useful name, or how can I use some other step to accomplish the same effect?

enter image description here

Vadim Kotov
  • 7,103
  • 8
  • 44
  • 57
goofballLogic
  • 31,055
  • 8
  • 37
  • 50

8 Answers8

39

Update Feb 2019:

According to gertvdijk's answer below, it is now possible to assign an optional label to the sh step, starting from v2.28, and for those who can't upgrade yet, there's also a workaround. Please check his answer for details and comments!


Previous version (hover to see it):

As far as I know, that's currently not possible. In the Jenkins tracker, there is a Name or alias Shell Script Step (sh) issue which is similar to your situation:

The sh step adds a "Shell Script" step in the Pipeline. However, there could be multiple such steps including steps from various plugins (e.g., Docker), which makes it hard to distinguish the steps. We should perhaps add an optional parameter to sh to specify a name or alias which would then appear in the pipeline steps. e.g., the following can be the step for npm which would show as "Shell script: npm" in the pipeline view.

sh cmd:"npm install", name: "npm"
However, it was closed as a duplicate of the older Allow stage to operate as a labelled block which has been fixed recently and seems to be included in v2.2 of the pipeline-stage-step-plugin (see changelog).

It seems that stages can now be nested and they will appear in the view table, but I don't think it's what you're looking for.
Morfic
  • 14,178
  • 3
  • 40
  • 55
  • 1
    Nested stages will just display them in the chronological order where they are found, so I'd say you can't properly "nest stages" – Pom12 Sep 13 '16 at 08:22
  • As long as we get human readable descriptions instead of just "Shell script" in the UI, I don't care how it's done. I will try to follow the reported changelog and see if we have any luck. – goofballLogic Sep 13 '16 at 12:00
  • We've tried but can't find the correct syntax for nesting stages: https://gist.github.com/goofballLogic/abd62cd1e0a700c8d849ed2f7038e338 – goofballLogic Sep 14 '16 at 19:34
  • We finally got this working and it does not resolve the problem - nested stages appear as a new column - not as named steps within the existing column – goofballLogic Sep 19 '16 at 10:56
  • 1
    @goofballLogic indeed, they appear as columns in the table just Pom12 was also mentioning. Because of this and the fact that if you have multiple steps in the stage you can not assign a name, I said it's probably what you're looking for and it's currently not possible to achieve what you want. IMHO the issue I was mentioning should still be open and a different fix provided... – Morfic Sep 19 '16 at 11:16
  • Nope, @jmreicha – goofballLogic Aug 29 '17 at 07:47
  • Maybe the best alternative currently available is to put an echo step before each sh step. Off course it's not as nice as an actual label, but it provides some insight. – Pieter Vogelaar Jul 30 '18 at 15:02
  • 1
    This is now implemented, see [my answer](https://stackoverflow.com/a/54676428/1254292). – gertvdijk Feb 14 '19 at 17:03
23

Version 2.28+ of the "Pipeline Nodes and Processes Plugin" has gained the label option for the sh step now with JENKINS-55410:

label (optional)

Label to be displayed in the pipeline step view and blue ocean details for the step instead of the step type. So the view is more meaningful and domain specific instead of technical.

  • Type: String

E.g.:

sh script: "echo foo", label: "my step"

If you can't upgrade yet, another option is to use the Labelled Pipeline Steps plugin.

tftd
  • 14,540
  • 8
  • 50
  • 99
gertvdijk
  • 21,668
  • 5
  • 32
  • 56
7
sh "echo foo", label: "my step"

Doesn't work for me,

It musst be:

sh script: "echo foo", label: "my step"

https://stackoverflow.com/a/54787322/6847446

Nils
  • 173
  • 2
  • 8
1

Well, desperate times call for desperate measures. If you can use Blue Ocean, you can use parallel step with single execution line.

        parallel(
            "This is my step name" : {
                sh 'env'
            }
        )
whitediver
  • 450
  • 2
  • 11
1

Try this, a good workaround

import org.jenkinsci.plugins.workflow.cps.CpsThread
import org.jenkinsci.plugins.workflow.actions.LabelAction


    def test() {
    def xyz = "Prints PWD"
    try {
        sh script: 'pwd'
    }
    finally {
        CpsThread.current().head.get().addAction(new LabelAction("Shell script ${xyz} "))
    }
}
sandy
  • 19
  • 5
0

I was also trying the same thing but in different context, My team don't want multiple sh log window over log UI, so I did try to use multiple UNIX commands in one line e.g jenkinsPipeline.sh "echo \"PATH: $PATH\";java -version;echo PROJ DIR = $projectDirectory;env > env.txt;cat env.txt;ls && cd $projectDirectory && gradle --refresh-dependencies clean assemble" And it worked for Jenkins pipeline script within the Jenkins job. but if I use shared library for extending pipeline and same strategy, then it was not working or else creating multiple windows as usual for sh log in UI.

ARUN007
  • 1
  • 1
0

It's not perfect, but I generally find it adequate to add an echo step that describes what the following bat or sh step is trying to accomplish. Someone that's never seen it before should be able to figure it out quickly.

echo "Testing with Ping"
bat "ping www.stackoverflow.com"
echo "Getting IPs"
bat "nslookup www.stackoverflow.com"
0

Following sandy excellent answer, I created a little script wrapper that encapsulates the sh step in a try/finally block.

Basic usage:

wrapper.script script: 'echo the invisible script', returnStdout: true, stepName: "description #1"

Will show "description #1" instead of the generic text.

Full source code and install instructions here https://github.com/ael-computas/jenkins-script-wrapper

Can easily be installed as a library on your jenkins server.

Anders Elton
  • 606
  • 4
  • 10