未找到 ENDSSH 命令

ENDSSH command not found

我正在编写一个 jenkins 管道 jenkinsfile,在脚本子句中我必须通过 ssh 连接到一个框和 运行 一些命令。我认为问题与我在引号中使用的环境变量有关。我收到 ENDSSH 命令未找到错误,我不知所措。任何帮助将不胜感激。

stage("Checkout my-git-repo"){
    steps {
      script {
        sh """
          ssh -o StrictHostKeyChecking=accept-new -o LogLevel=ERROR -o UserKnownHostsFile=/dev/null -i ${JENKINS_KEY} ${JENKINS_KEY_USR}@${env.hostname} << ENDSSH
          echo 'Removing current /opt/my-git-repo directory'
          sudo rm -rf /opt/my-git-repo
          echo 'Cloning new my-git-repo repo into /opt'
          git clone ssh://${JENKINS_USR}@git.gitbox.com:30303/my-git-repo
          sudo mv /home/jenkins/my-git-repo /opt
          ENDSSH
        """
      }
    }
  }

-bash: line 6: ENDSSH: command not found

我个人对 jenkins 不熟悉,但我猜问题出在 ENDSSH

之前的空格

White space in front of the delimiter is not allowed.

(https://linuxize.com/post/bash-heredoc/)

尝试删除缩进:

stage("Checkout my-git-repo"){
    steps {
      script {
        sh """
ssh -o StrictHostKeyChecking=accept-new -o LogLevel=ERROR -o UserKnownHostsFile=/dev/null -i ${JENKINS_KEY} ${JENKINS_KEY_USR}@${env.hostname} << ENDSSH
echo 'Removing current /opt/my-git-repo directory'
sudo rm -rf /opt/my-git-repo
echo 'Cloning new my-git-repo repo into /opt'
git clone ssh://${JENKINS_USR}@git.gitbox.com:30303/my-git-repo
sudo mv /home/jenkins/my-git-repo /opt
ENDSSH
        """
      }
    }
  }

或确保空格只是制表符并将 << 替换为 <<-:

Appending a minus sign to the redirection operator <<-, will cause all leading tab characters to be ignored. This allows you to use indentation when writing here-documents in shell scripts. Leading whitespace characters are not allowed, only tab.