无法在jenkins管道内运行sh脚本

qncylg1j  于 2022-12-03  发布在  Jenkins
关注(0)|答案(1)|浏览(231)

团队,我尝试了几个在线链接,但不能弄清楚为什么我的 shell 脚本不运行内Jenkins管道。任何提示?
我的整个渠道

pipeline {
    options {
        buildDiscarder( logRotator( artifactDaysToKeepStr: '15', artifactNumToKeepStr: '15', daysToKeepStr: '15', numToKeepStr: '15'))
        disableConcurrentBuilds()
        skipDefaultCheckout true
        timeout(time: 30, unit: 'MINUTES')
        ansiColor('xterm')
    }
    parameters {
        booleanParam( name: 'Refresh', defaultValue: false, description: 'Reload job from the Jenkinsfile and then exit')
    }
    agent {
        label 'built-in'
    }
    triggers {
        cron '0 8 * * *'   
    }

    stages {
        stage('Build') {
            when { expression { !params.Refresh } }
            steps {
                script {
                    sh ''' set +x
                    apt-get update && apt install dnsutils -y
                    nslookup git.github-private.com | grep "Non-authoritative answer" -A 3
                    git_av_url=$(nslookup git.github-private.com | grep "Non-authoritative answer" -A 3 | grep git | awk \'{print $2}\')
                    git_av_ip=$(nslookup git.github-private.com | grep "Non-authoritative answer" -A 3 | grep Address | awk \'{print $2}\')
                    echo $git_av_ip
                    echo $git_av_ip $git_av_url >> /etc/hosts
                    cat /etc/hosts'''                 
                }
            }
        }
    }
}

输出功率

09:28:53  [Pipeline] stage
09:28:53  [Pipeline] { (Build)
09:28:53  [Pipeline] script
09:28:53  [Pipeline] {
09:28:53  [Pipeline] sh
09:28:53  + set -ex
09:28:53  + apt-get update
09:28:53  Hit:1 http://security.debian.org/debian-security bullseye-security InRelease
09:28:53  Hit:2 http://deb.debian.org/debian bullseye InRelease
09:28:53  Hit:3 http://deb.debian.org/debian bullseye-updates InRelease
09:28:54  Reading package lists...
09:28:54  + apt install dnsutils -y
09:28:54  
09:28:54  WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
09:28:54  
09:28:55  Reading package lists...
09:28:55  Building dependency tree...
09:28:55  Reading state information...
09:28:55  dnsutils is already the newest version (1:9.16.33-1~deb11u1).
09:28:55  0 upgraded, 0 newly installed, 0 to remove and 43 not upgraded.
09:28:55  + nslookup git.github-private.com
09:28:55  + grep Non-authoritative answer -A 3
09:28:55  [Pipeline] }
09:28:55  [Pipeline] // script
09:28:55  [Pipeline] }
09:28:55  [Pipeline] // stage
09:28:55  [Pipeline] }
09:28:55  
09:28:55  [Pipeline] // ansiColor
09:28:55  [Pipeline] }
09:28:55  [Pipeline] // timeout
09:28:55  [Pipeline] }
09:28:56  [Pipeline] // node
09:28:56  [Pipeline] End of Pipeline
09:28:56  ERROR: script returned exit code 1
09:28:56  Finished: FAILURE
daupos2t

daupos2t1#

我翻转了我的变量值计算的逻辑,它工作,我还放置了#!/bin/bash。从这里得到帮助ssh script format下面是样本管道。

steps {    
   sh '''#!/bin/bash
                git_av_url=$(awk '{print $2}' <<< $(nslookup git.github-private.com | grep -e "git" -A 3))
                git_av_ip=$(awk '{print $2}' <<< $(nslookup git.github-private.com | grep -e "Non-authoritative answer" -A 3))
                echo $git_av_ip $git_av_url >> /etc/hosts
                grep $git_av_url /etc/hosts
}

输出功率

10.1.0.1 git.github-private.com

相关问题