how to download protected resources in a jenkins workflow? - jenkins

Many times I have the need to download files (e.g. archives) from a remote repository (e.g. maven-repo) which is protected by username/password.
Its easy to get such a file with curl or wget, but when I don't like to see the password on the logs, maybe there is a better/embedded way to do this from within a jenkins workflow? And how do I combine it with credentials managed by jenkins credential plugin?

Use the Credentials Binding plugin, which integrates with Workflow.

Related

Upload Build artifacts from Jenkins Pipeline to Sharepoint

I have a requirement to upload the build artifacts to SharePoint at the end of the build. using jenkins pipeline.
I didn't find any plugin to achieve this.
My Jenkins instance is running on a Linux machine. Please suggest the best way to achieve this
I am using CLI for Microsoft 365 for this (available for linux as well). Looks somewhat like this:
m365 login --authType password --userName user#contoso.com --password pass#word1
m365 spo file add --webUrl https://contoso.sharepoint.com/sites/project-x --folder 'Shared Documents' --path 'C:\MS365.jpg'
https://pnp.github.io/cli-microsoft365/cmd/login/
https://pnp.github.io/cli-microsoft365/cmd/spo/file/file-add/
You may need to add special account to your sharepoint for your jenkins, or add an app password to be able to upload file from a daemon process (if your sharepoint requires MFA authentication)

global tool configurations in jenkins through cli?

Is there a way to add global tool configurations for artifactory and aws in jenkins through cli?
I'm trying to write chef cookbook for automating creation of Jenkins job, but I don't know how to add credentials for tools.
Credentials don't depend on the tools which will use them.
You can register credentials in general through the JENKINS Credentials Plugin API: see for instance
How to create jenkins credentials via the REST API? (similar to jenkins_api_client issue 162)
update Jenkins credentials by script
You can then use those credentials in association with a Jenkins Job.
Your question is twofold.
Credentials
The Jenkins chef cookbook offers a jenkins_credentials resource, which allows you to pipe credentials (using Jenkins API internally) into your Jenkins instance.
Global Tool Configuration
You can use the jenkins_script resource of the same cookbook to execute any Groovy script. This allows you to configure your Jenkins instance. You now just have to figure out exactly, what the code is to, e.g., select the previously defined credentials. But the code looks similarly to the example given in the cookbook's README.

How to allow only specific credential for project in jenkins?

I need to prevent using credentials by every projects. I.e. test1 project should be able to access only test1-cred and only if it is started by user1 from Gitlab or directly in Jenkins. Also this should be supported by Jenkins declarative pipeline. If you use specific credentials in Jenkinsfile which are not allowed for current project - Jenkins shouldn't allow to use them.
How can I implement such a behavior?
As far as I know there are three common places to configure credentials in Jenkins: by user, by folder or globally
It seems that the folder plugin would suit your use case. It allows to organize your projects in folders. For each folder you can configure access rights and credentials.
More informations on credentials: https://plugins.jenkins.io/credentials

SSH errors with Jenkins git plugin

There are lots of question on here about Permission denied (publickey) errors when using the Jenkins git plugin.
Can someone explain the authentication flow this plugin uses to check out a repository? I can't find a good description on the plugin page.
I want to just SSH into the build slave, checkout the repository there, then run my job, but clearly that is not how it works.
I guess I could add my credentials to the jenkins master, but I dont want any code there. I want it on my build slave.
Issue has nothing to do with git really. As their documentation states, it relies on git runtime which in its turn relies on system environment when it comes to secure connections. Ssh requires client to have valid key to connect and fails to that message if client does not provide one. Without any additional actions, key is not injected into environment, so client could not provide any valid key.
What you actually can use is ssh agent plugin. That allows to add key to ssh-agent on slave that will be catched up by git.

Working with jenkins credentials

I want to know how to Create the credentials that can be used by Jenkins and by jobs running in Jenkins to connect to 3rd party services.
You should specify which 3rd party service you will work on.
Below is an example of credentials with bitbucket
I am now working with Jenkins ver. 1.568.
By default, there's Credentials feature. So, if you want to add a credential, just click on Add Credentials. For example, I'd like to add SSH Username with password, so I can use it in checking out code from bitbucket
Credentials plugin - provides a centralized way to define credentials that can be used by your Jenkins instance, plugins and build jobs.
Credentials Binding plugin - allows you to configure your build jobs to inject credentials as environment variables.
The third party plugins need to be installed in your Jenkins instance. For example, Assembla Auth Plugin allows you to authenticate to an Assembla repository.
Which 3rd party services are you working with?
Instead of using SSH Username with private key you can simply use username with password option

Resources