I'm trying to use sshPut commands to put files into a remote directory. The issue is that the directory doesn't exist. Can sshPut be used to simply create a remote directory? This is what my steps look like now:
sshPut failOnError: true, remote: dev_repo, from: './MyProgram.exe', into: "/home/releases/${env.MYPROG_VERSION}/"
sshPut failOnError: true, remote: dev_repo, from: './MyProgram.pdb', into: "/home/releases/${env.MYPROGAM_VERSION}/"
sshPut failOnError: true, remote: dev_repo, from: './ThirdParty.dll', into: "/home/releases/${env.MYPROGAM_VERSION}/"
where env.MYPROGRAM_VERSION is set earlier in the Jenkinsfile.
The issue is that the directory does not exist, and I want to create it. I don't have a directory with that name on the build machine.
You could add one step before the sshPut: a sshCommand
sshCommand remote: remote, command: "mkdir -p "/home/releases/${env.MYPROGAM_VERSION}"
That way, the folder is created first, then sshPut can operate successfully.
Related
I'm building war file by using maven and sending this build to another server via Jenkins ssh Publisher plugin.
stage('Deploy develop build to Test Server') {
when {
branch 'develop'
}
steps {
// sends war file to test_server
dir("${WORKSPACE}/ag.mycompany.feature/target/") {
sshPublisher(
alwaysPublishFromMaster: true,
continueOnError: false,
failOnError: true,
publishers: [
sshPublisherDesc(
configName: "test_server",
transfers: [sshTransfer(sourceFiles: "vc-admin_${VERSION}_${BUILD_NUMBER}_dev.war", remoteDirectory: '/wars/main_vc_admin/develop')],
verbose: true
)
]
)
}
}
}
this part working very well, I can see my war file with version and build number
then, I'm trying to cp these files (in remote server) to another folder and build a docker image via docker-compose.
connection to remote server via withCredentials is OK, and I tested with some sudo command like 'docker ps' and working well.
but I can't pass variables like VERSION and BUILD_NUMBER to sshCommand
is there any way to pass these variables?
And all examples in internet that I saw are in script{ ... } block for withCredentials and sshPublisher, that's why I used script block in declerative pipeline. I don't know how to create remote variable by using declerative pipeline.
stage("build docker containers") {
when {
branch 'develop'
}
steps {
script{
withCredentials([sshUserPrivateKey(credentialsId: 'test_server',
keyFileVariable: 'test_user',
passphraseVariable: '',
usernameVariable: 'test_user')]) {
// code block
def remote = [ : ]
remote.name = "MY_TEST_SERVER"
remote.host = "1.1.1.1"
remote.user = "user"
remote.password = "pass"
remote.allowAnyHosts = true
remote.identityFile = test_user
remote.pty = true
sshCommand remote: remote, command="cp /home/user/wars/main_vc_admin/develop/vc-admin_${VERSION}_${BUILD_NUMBER}_dev.war /home/user/main-vc-deployment/backend/vc-admin.war"
sshCommand remote: remote, sudo: true, command: "docker-compose --file /home/user/main-vc-deployment build && docker-compose --file /home/user/main-vc-deployment run -d"
}
}
}
}
On my Jenkins server, I have a simple test pipeline to check into copying build data to a remote server.
I'm using the SSH Pipeline Steps plugin.
My code is:
stage('Remote SSH') {
steps {
sshRemove remote: remote, path: "/mnt/test"
sshCommand remote: remote, command: "mkdir /mnt/test"
writeFile file: './tst/abc.sh', text: 'ls -lrt'
sshPut remote: remote, from: './tst/', into: '/mnt/test/.'
}
The result is that in /mnt/test I have the created "tst" folder and it's contents.
What I wanted was just the contents of this tst folder to be transfered to the target.
How should I be formatting the sshPut step?
Looking at source-code and reading a bit about copy commands in Java it seems like this is just the way the filecopy operations work in Java.
That one would have to iterate through the directory contents to do it another way.
Since I'm creating the source folder (even in my real build example), it seems that the best option is just to rename the folder before the move:
sshRemove remote: remote, path: "/mnt/test"
writeFile file: './tst/abc.sh', text: 'ls -lrt'
sh "mv ./tst ./test"
sshPut remote: remote, from: './test/.', into: '/mnt/'
I'd happily listen if someone has another solution.
There is an issue about this very use case on Jenkins'Jira https://issues.jenkins-ci.org/browse/JENKINS-58778
I would expect the following to work but it apparently does not:
remote.fileTransfer = 'SCP'
sshPut remote: remote, from: './tst/*', into: '/mnt/test/'
Another way could be to do one sshCommand more after sshPut to 'mv' the files as you please
Usually I Go to https://<myJenkins>/pipeline-syntax and use the step builder. A shame it's not working for sshPut
I am trying to use Jenkins stash/unstash for the first time, and I am unable to successfully stash my files.
This is the path to the file:
/opt/cicd/jenkins/workspace/DEVOPS/stageParams.yaml
This is my stash command:
{stash name: "config", includes: "/DEVOPS/stageParams.yaml"}
I keep receiving this error:
ERROR:No files included in stash 'config'
Please help!
/opt/cicd/jenkins/workspace/DEVOPS/stageParams.yaml
/DEVOPS/stageParams.yaml
Two path above are different.
Let try:
{stash name: "config", includes: "DEVOPS/stageParams.yaml"}
I'm trying to execute a jar file built in a different jenkins job. For that I use the CopyArtifact plugin.
Copying the artefact seems to work. The following code
step ([$class: 'CopyArtifact',
projectName: '/BuildJob/master',
filter: 'target/mytool.jar',
target: './']);
finishes with
Copied 1 artifact from "BuildJob ยป master" build number 1
But how can I access this copied jar file? I tried the following:
sh(returnStdout: true, script: "java -jar mytool.jar")
but this ended up in the following error message:
Error: Unable to access jarfile mytool.jar
I created playbooks with tags.
I want to use ansible-container to test my playbooks.
According to the Ansible documentation on ansible-container build,
it is possible to add playbook parameters, but it doesn't work.
ansible-container build -- --tags "pre-install, install"
I get the following error:
ansible-container_1 | ERROR! the playbook: [--tags, could not be found
ansible_ansible-container_1 exited with code 1
Remove the space between tags:
--tags 'pre-install,install' instead of --tags 'pre-install, install'