获取 Jenkins 节点 workDir 的正则表达式

Regular expression to fetch the Jenkins node workDir

我正在尝试在脚本中获取 jenkins 节点的秘密和工作目录。

curl -L -s -u user:password -X GET https://cje.example.com/computer/jnlpAgentTest/slave-agent.jnlp

这给出了

<jnlp codebase="https://cje.example.com/computer/jnlpAgentTest/" spec="1.0+">
    <information>
        <title>Agent for jnlpAgentTest</title>
        <vendor>Jenkins project</vendor>
        <homepage href="https://jenkins-ci.org/"/>
    </information>
    <security>
        <all-permissions/>
    </security>
    <resources>
        <j2se version="1.8+"/>
        <jar href="https://cje.example.com/jnlpJars/remoting.jar"/>
    </resources>
    <application-desc main-class="hudson.remoting.jnlp.Main">
        <argument>b8c80148ce36de10c9358384fac9e28fbba941055a9a6ab2277e75ddc29a8744</argument>
        <argument>jnlpAgentTest</argument>
        <argument>-workDir</argument>
        <argument>/tmp/jnlpAgenttest</argument>
        <argument>-internalDir</argument>
        <argument>remoting</argument>
        <argument>-url</argument>
        <argument>https://cje.example.com/</argument>
    </application-desc>
</jnlp>

这里我要获取secret和workDir,secret可以使用这个命令获取

curl -L -s -u admin:password -H "Jenkins-Crumb:dac7ce5614f8cb32a6ce75153aaf2398" -X GET https://owood.ci.cloudbees.com/computer/jnlpAgentTest/slave-agent.jnlp | sed "s/.*<application-desc main-class=\"hudson.remoting.jnlp.Main\"><argument>\([a-z0-9]*\).*//"
b8c80148ce36de10c9358384fac9e28fbba941055a9a6ab2277e75ddc29a8744

但我找不到获取 workDir 值的方法,这里是 /tmp/jnlpAgenttest,它紧跟在标签 -workDir

之后

您可以使用

sed -n '/-workDir/{n;s/.*>\([^<>]*\)<.*//p}'
sed -n -e '/-workDir/{n' -e 's/.*>\([^<>]*\)<.*//p' -e '}'

参见online demo

详情:

  • /-workDir/ - 找到带有 -workDir 标签的行
  • n - 将下一行读入模式 space 丢弃前一行 read
  • s/.*>\([^<>]*\)<.*// - 匹配 > 以内的任何文本,然后将 <> 以外的任何零个或多个字符捕获到组 1 中,然后匹配 < 和字符串的其余部分并替换为第 1 组值
  • p - 打印替换结果。