Jenkins在我的服务器中克隆我的仓库时,它将项目放置在哪里?构建后,它会自动删除所有内容吗?还是应该在Jenkinsfile中添加一些内容来做到这一点?
我正在尝试使用以下方式列出文件:
pipeline {
agent any
stages {
stage('just testing') {
steps {
ls -l
}
}
}
}
但是它返回无效的语法。我也尝试过sh "ls -l"
。
非常感谢您。
答案 0 :(得分:0)
运行Jenkins文件并从源存储库获取该文件时,Jenkins会自动为该作业动态创建一个工作空间,并且默认情况下,它将项目中的任何其他文件放入该工作空间。
在您的示例中,您使用了“ agent any”,然后在您的阶段中使用了特定于Linux的命令,例如“ sh'ls -l”来列出文件。首先要注意的是,“ agent any”可以在Jenkins服务器上配置的任何从属服务器上运行,因此根据配置,它可以在Linux或Windows从属服务器上运行。因此,如果“ sh”步骤尝试在Windows从站上运行,则该步骤可能会失败。您可以使用带有标签的代理来更具体地选择节点/从属服务器(可用的标签取决于您的Jenkins从配置):
agent {
label "LINUX"
}
构建后,它不会自动删除所有内容,因为它会将作业的每个构建的工作空间保留在Jenkins主数据库上。您可以通过两种方式解决此问题:
1)在管道中使用选项部分来丢弃旧版本:
options {
// Keep 4 builds maximum
buildDiscarder(logRotator(numToKeepStr: '4'))
}
2)使用后处理程序的“始终”部分进行清理:
post {
always {
deleteDir()
}
}
以下管道可能有助于您入门:
pipeline {
agent any
options {
buildDiscarder(logRotator(numToKeepStr: '4'))
}
stages {
stage('List files in repo on Unix Slave') {
when {
expression { isUnix() == true }
}
steps {
echo "Workspace location: ${env.WORKSPACE}"
sh 'ls -l'
}
}
stage('List files in repo on Windows Slave') {
when {
expression { isUnix() == false }
}
steps {
echo "Workspace location: ${env.WORKSPACE}"
bat 'dir'
}
}
}
post {
always {
deleteDir()
}
}
}
这是我编辑的一些Git聊天记录的输出:
Pipeline] node
Running on master in /var/jenkins_home/jobs/macg33zr/jobs/testRepo/branches/master/workspace
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Declarative: Checkout SCM)
Cloning the remote Git repository
Cloning with configured refspecs honoured and without tags
Cloning repository https://github.com/xxxxxxxxxx
[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (List files in repo on Unix Slave)
[Pipeline] isUnix
[Pipeline] echo
Workspace location: /var/jenkins_home/jobs/macg33zr/jobs/testRepo/branches/master/workspace
[Pipeline] sh
[workspace] Running shell script
+ ls -l
total 8
-rw-r--r-- 1 jenkins jenkins 633 Oct 1 22:07 Jenkinsfile
-rw-r--r-- 1 jenkins jenkins 79 Oct 1 22:07 README.md
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (List files in repo on Windows Slave)
Stage 'List files in repo on Windows Slave' skipped due to when conditional
[Pipeline] isUnix
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Declarative: Post Actions)
[Pipeline] deleteDir
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline