How can I create a CIFS Share configuration in a Jenkins pipeline? - jenkins

I need to reconfigure a custom host and credentials for a cifsPublisher, beginning with the following:
cifsPublisher(publishers:
[[
configName: 'fooConfig',
transfers:
[[
cleanRemote: true,
excludes: '',
flatten: false,
makeEmptyDirs: false,
noDefaultExcludes: false,
patternSeparator: '[, ]+',
remoteDirectory: "/fooRemote/",
remoteDirectorySDF: false,
removePrefix: '/build',
sourceFiles: "build/**"
]],
usePromotionTimestamp: false,
useWorkspaceInPromotion: false,
verbose: false
]]
)
I need to change it to something similar to the below:
cifsPublisher(publishers:
[[
config: [[
hostName: Ipv4,
user: domain\username,
password: secret,
share: baseDirectory
]],
transfers:
[[
cleanRemote: true,
excludes: '',
flatten: false,
makeEmptyDirs: false,
noDefaultExcludes: false,
patternSeparator: '[, ]+',
remoteDirectory: "/public/",
remoteDirectorySDF: false,
removePrefix: '/share',
sourceFiles: "share/**"
]],
usePromotionTimestamp: false,
useWorkspaceInPromotion: false,
verbose: true
]]
)
My CloudBees server is sending 30 deployments per day to different hosts and with different credentials, and this level of activity will continue to grow. Many cifs configs are being created daily, which results in the config page and cifs configurations becoming difficult to manage.

I solved this issue using sambclient tool with sh jenkins plugin as follows
sh: "smbclient \\\\\\\\${host}\\\\${sharedFolder}-U=${domain}\\\\${user}%${secret} -c 'prompt OFF; recurse ON; lcd ${buildpath} ; mput * ;'"
smbclient let you connect with and execute options using -c perfect to embedded in a script.sh, this is the same as
smbclient \\${host}\${sharedFolder}-U=${domain}\${user}%${secret}
>prompt OFF --This avoid asking for a prompt in any file
>recurse ON --Recursive mode
>lcd ${buildpath} --Declare my local path to smbclient
>mput * --mput copy all files that matches with a mask (global pattern)

Related

publish over ftp jenkins plugin is not uploading subfolder

I have a Jenkinsfile that deploys my angular app code to a site using the Publish over FTP plugin. All of the files in the dist folder are transferred except an assets subfolder. I have tried putting in the following values for the sourceFiles parameter with no success: 'webapp/dist/', 'webapp/dist/**', 'webapp/dist/**/*'.
Here is the publish over FTP part of my Jenkinsfile:
stage('Deploy') {
steps {
echo 'Deploying....'
ftpPublisher paramPublish: null, masterNodeName:'', alwaysPublishFromMaster: true, continueOnError: false, failOnError: true, publishers: [
[configName: 'Angular app', verbose: true, transfers: [
[asciiMode: false, cleanRemote: true, makeEmptyDirs:true, excludes: '', flatten: false,
noDefaultExcludes: false, patternSeparator: '[, ]+',
remoteDirectory: "webapp",
removePrefix: "webapp/dist",
remoteDirectorySDF: false,
sourceFiles: 'webapp/dist/**/*']
], usePromotionTimestamp: false, `enter code here`useWorkspaceInPromotion: false]
]
}
}
I've looked at the Publish over FTP pipeline documentation: https://jenkins.io/doc/pipeline/steps/publish-over-ftp/ and couldn't find anything parameters that I was missing. I'm stuck.
I was able to solve the issue. I changed the title of the pipeline to all lowercase letters without any spaces. I then changed the file path of the workspace folder to 'C:/jenkinsworkspace/${ITEM_FULL_NAME}' by modifying the workspaceDir entry in the config.xml located in the Jenkins root directory. I stopped the Jenkins service before modifying the config.xml. Both the assets folder and the favicon got generated in the build. It was one of the solutions mentioned in https://github.com/angular/angular-cli/issues/9230. Thanks for your help #Alberto L. Bonfiglio.

JenkinsFile. sshPublisherDesc property

I attempting understand meaning sshPublisherDesc property from jenkins ssh publish plugin. Does anybody know what is it?
I checked docs (https://jenkins.io/doc/pipeline/steps/publish-over-ssh/) but they don't containt any description about that. Could anyone to explain it for me a bit?
ps. All examples of using this plugin contain this property but i can't image what is it.
It's the descriptor of each publisher, the api gives you this way to combine configName, transfers ...
https://github.com/jenkinsci/publish-over-ssh-plugin/blob/f75ee1db7a52f3cfddd138aae5149e02a4fc8258/src/main/java/jenkins/plugins/publish_over_ssh/descriptor/BapSshPublisherDescriptor.java
As I know this is ssh connection name for remote server
You will config it in the below image
Step 1:
Step 2: Add 2 sections
Step 3: Run Jenkins scripts with the above configuration
script {
sshPublisher(publishers: [sshPublisherDesc(configName: '10.90.9.11', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand:
'''
sudo docker stop, start command.......
''', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: 'tmp')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
}
Hope to help you!

Warning: Identity file D:WorkKeyPairsDeployTech-O-Dex-APII not accessible: No such file or directory

I'm painfully new to Jenkins so I'm sorry for the silly question. I'm trying to get my jenkins to publish my .jar via SSH using the following:
sshPublisher(publishers: [sshPublisherDesc(configName: 'Tech-O-Dex-API', transfers: [sshTransfer(excludes: '', execCommand: 'ssh -i D:\\Work\\KeyPairs\\Deploy\\Tech-O-Dex-APII -n -f ec2-user#ec2-<IP>.compute-1.amazonaws.com "sh -c \'cd ~/; java -jar Tech-O-Dex-0.1.0.jar /dev/null &\\\'"', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: ' \'target/*.jar\'')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
however, I'm getting the following error:
Warning: Identity file D:WorkKeyPairsDeployTech-O-Dex-API not
accessible: No such file or directory.
I've tried specifying the key file, pem and ppk, nothing works? The file exists becasue I can use
scp -i D:\Work\KeyPairs\Tech-O-Dex-API D:\Jenkins\workspace\Tech-O-Dex\target\Tech-O-Dex-0.1.0.jar ec2-user#ec2-<IP>.compute-1.amazonaws.com:~/
inf the cmd line and it works fine to my EC2?
I'm on a windows machine which I'm almost positive is what is causing all of these headaches haha. Any help would be appreciated. Thanks
sources tried reference: Can't ssh to AWS EC2: Identity file not accessible

How to use publish over ssh plugin in pipeline

I would like to SSH to linux server from Jenkins hosted on windows and execute a command over in linux machine, I tried installing publish over ssh plugin and tested the connection in global config and it works fine, I don't know how to proceed next in pipeline. Any help would be appreciated.
If you are using a pipeline project and a Jenkinsfile, then all you need to do is go into your project in Jenkins and click configure. In the pipeline section of the configuration, at the bottom there is a link "pipeline syntax". It will take you to the Snippet Generator. Its self explanatory and in our case it allows to generate "publish over ssh" snippets that you would add to your Jenkinsfile (add it to a steps section inside a stage definition). In the generator you can define what to publish, options to run a shell command, etc. source
In case you were looking for the syntax for a declarative pipeline (Jenkinsfile) for Publish-Over-SSH, (Instead of the scripted pipeline, which is all I could find). Here's what finally worked for me.
pipeline{
agent any
environment {
RELEASENAME="yourProject-ci"
}
stages{
stage("Get the charts..."){
steps {checkout scm}
}
stage('SSH transfer') {
steps([$class: 'BapSshPromotionPublisherPlugin']) {
sshPublisher(
continueOnError: false, failOnError: true,
publishers: [
sshPublisherDesc(
configName: "kubernetes_master",
verbose: true,
transfers: [
sshTransfer(execCommand: "/bin/rm -rf /opt/deploy/helm"),
sshTransfer(sourceFiles: "helm/**",)
]
)
]
)
}
}
stage('Deploy Helm Scripts'){
steps([$class: 'BapSshPromotionPublisherPlugin']) {
sshPublisher(
continueOnError: false, failOnError: true,
publishers: [
sshPublisherDesc(
configName: "kubernetes_master",
verbose: true,
transfers: [
sshTransfer(execCommand: "cd /opt/deploy/helm;helm upgrade ${RELEASENAME} . --install"),
]
)
]
)
}
}
}
}
I have a checkout that happens first and then I copy some helm charts from the checkout to my kubernetes master and then run the charts.
configName: "kubernetes_master" is something I setup in the Publish_over_ssh plugin configuration section (Found under Manage Jenkins > Configure System) so I could reference it. It includes a username, sshkey, destination hostname, and base directory for the destination which I put as /opt/deploy.
FYI execCommand does not use the base directory... it assumes you will use full pathing.
Hope that helps.
edit: I should probably mention that there are lots more options for the sshPublisher than what I used. You can find them here: https://jenkins.io/doc/pipeline/steps/publish-over-ssh/
Based on levis answer, the below has worked for me.
stage('Deploy') {
agent any
steps {
sh 'mv target/my-app-0.0.1-SNAPSHOT.jar my-app.jar'
sshPublisher(
continueOnError: false,
failOnError: true,
publishers: [
sshPublisherDesc(
configName: "my-ssh-connection",
transfers: [sshTransfer(sourceFiles: 'my-app.jar')],
verbose: true
)
]
)
}
}
I got this question some time ago, and here is the answer. Change the code according to your requirement.
pipeline {
agent any
options { timestamps () }
stages {
stage('Publish over ssh plugin in pipeline') {
steps([$class: 'BapSshPromotionPublisherPlugin']) {
script {
List SERVERS_LIST = ["Server_1", "Server_2"]
for(cr_server in SERVERS_LIST){
sshPublisher(
publishers: [
sshPublisherDesc(
configName: cr_server,
transfers: [
sshTransfer(
cleanRemote: false,
excludes: '',
execCommand: '',
execTimeout: 120000,
flatten: false,
makeEmptyDirs: false,
noDefaultExcludes: false,
patternSeparator: '[, ]+',
remoteDirectory: '',
remoteDirectorySDF: false,
removePrefix: '',
sourceFiles: '**/*'
)
],
usePromotionTimestamp: false,
useWorkspaceInPromotion: false,
verbose: false
)
]
)
}
}
}
}
}
}
I don't know how helpful this'll be but I found a tutorial on something that should work until they have a nicer way to do it.

Declarative Jenkinsfile CIFS share

i have another question about the jenkins pipeline.
How can i publish the build artifacts to a windows share? In normal build jobs there is a "CIFS Publisher" post build action. But how can i use it in
post{
success {
//publish build artifacts
}
}
Is there any example?
I've succesfully managed it in this way:
cifsPublisher alwaysPublishFromMaster: false, continueOnError: false, failOnError: false, publishers: [[
configName: 'NAME_OF_THE_CIFS_CONFIG', transfers: [[
cleanRemote: false,
excludes: '',
flatten: false,
makeEmptyDirs: false,
noDefaultExcludes: false,
patternSeparator: '[, ]+',
remoteDirectory: '$BUILD_NUMBER',
remoteDirectorySDF: false,
removePrefix: '',
sourceFiles: 'myfile']],
usePromotionTimestamp: false,
useWorkspaceInPromotion: false,
verbose: true
]]
Please use the auto generate tool in Jenkins to help you.
As you can see the highlight link. Click it.
Fill all the required value that you expected.
After scroll down and click Generate pipeline script, you will see the syntax that you need.
Last step, copy the generated script into success clause.

Resources