Проблемы при создании файла в сценариях оболочки в сценарии конвейера jenkins - PullRequest
0 голосов
/ 27 сентября 2018

Я пытаюсь создать многострочный файл в сценарии конвейера Jenkins, используя следующие команды:

    sh "echo \"line 1\" >> greetings.txt"
    sh "echo \"line 2\" >> greetings.txt"
    echo "The contents of the file are"
    sh 'cat greetings.text'
    sh 'rm -rf greetings.txt'

К сожалению, я не могу создать файл с именем greetings.txt.Может кто-нибудь, пожалуйста, дайте мне знать, где я иду не так.

Результаты в консоли Jenkins:

[tagging] Running shell script
+ echo 'line 1'
[Pipeline] sh
[tagging] Running shell script
+ echo 'line 2'
[Pipeline] echo
The contents of the file are
[Pipeline] sh
[tagging] Running shell script
+ cat greetings.text
cat: greetings.text: No such file or directory

Любые предложения будут полезны.

Спасибо!

Ответы [ 2 ]

0 голосов
/ 27 сентября 2018

Это можно решить с помощью одинарных кавычек с sh, поэтому вам не нужно использовать экранирование.Также вы должны создать исходный файл с > и добавить содержимое с >>:

pipeline{
    agent any

    stages{
        stage('write file'){
            steps{
                sh 'echo "line 1" > greetings.txt'
                sh 'echo "line 2" >> greetings.txt'
                echo "The contents of the file is"
                sh 'cat greetings.txt'
                sh 'rm -rf greetings.txt'
            }
        }
    }
}

, вывод:

[test] Running shell script
+ echo line 1
[Pipeline] sh
[test] Running shell script
+ echo line 2
[Pipeline] echo
The contents of the file is
[Pipeline] sh
[test] Running shell script
+ cat greetings.txt
line 1
line 2
[Pipeline] sh
[test] Running shell script
+ rm -rf greetings.txt
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
0 голосов
/ 27 сентября 2018

Он не находит файл с именем greetings.text, потому что вы его не создали (небольшая опечатка в расширении в строке cat).Попробуйте sh 'cat greetings.txt' или, что еще лучше, измените ваш скрипт:

sh "echo \"line 1\" >> greetings.txt"
sh "echo \"line 2\" >> greetings.txt"
echo "The contents of the file are"
sh 'cat greetings.txt'
sh 'rm -rf greetings.txt'

Если вы хотите использовать многострочные команды, вы также можете использовать этот синтаксис:

sh """
echo \"line 1\" >> greetings.txt
echo \"line 2\" >> greetings.txt
echo "The contents of the file are:"
cat greetings.txt
rm -rf greetings.txt
"""

Из последнего примера:это должно генерировать вывод как:

Running shell script
+ echo 'line 1'
+ echo 'line 2'
+ echo 'The contents of the file are:'
The contents of the file are:
+ cat greetings.txt
line 1
line 2
+ rm -rf greetings.txt
...