如何配置Jenkins在没有Gitlab插件的情况下访问Gitlab私有仓库?

2j4z5cfb  于 2023-03-01  发布在  Jenkins
关注(0)|答案(2)|浏览(236)

我已经在Ubuntu服务器上安装了最新的Jenkins,并在Gitlab上的项目库中安装了Jenkinsfile。
我可以使用Jenkins项目管道配置上的用户名/密码凭据连接到Gitlab上的私有存储库,而不使用Jenkins Gitlab插件。我觉得这不安全。我如何使用Gitlab API令牌代替Jenkins的用户名/密码来访问远程私有Gitlab存储库,而不使用Jenkins Gitlab插件。另一个选项是在Jenkins服务器上设置ssh私钥来验证Gitlab存储库。这一选择是否可行?
Jenkins Gitlab插件没有得到官方支持,也没有得到很好的维护,因为Gitlab希望客户使用他们自己的CI/CD解决方案,以便出于营销原因将客户与他们的平台联系起来。

sxpgvts3

sxpgvts31#

A relatively safe way to handle this situation is to store your credentials is the credentials system in Jenkins (that way you do not have to include the credentials in the JenkinsFile), and using a deploy token (available for Gitlab 10.7 and later) for the relevant repository. That token allows you to provide read-only rights to the repository.

Step 1 - setup the deploy token in GitLab

From the GitLab documentation
You can create as many deploy tokens as you like from the settings of your project:

  1. Log in to your GitLab account.
  2. Go to the project you want to create Deploy Tokens for.
  3. Go to Settings > Repository.
  4. Click on “Expand” on Deploy Tokens section.
  5. Choose a name and optionally an expiry date for the token.
  6. Choose the desired scopes.
  7. Click on Create deploy token.
  8. Save the deploy token somewhere safe. Once you leave or refresh the page, you won’t be able to access it again.

Step 2 - Saving the deploy token in Jenkins' credentials system

Since the deploy tokens have a username and password, pick that as the type in the steps below. Write down the id you will use in this step (see below) as you will need it in your pipeline declaration.
From the Jenkins documentation
To add new global credentials to your Jenkins instance:

  1. If required, ensure you are logged in to Jenkins (as a user with the Credentials > Create permission).
  2. From the Jenkins home page (i.e. the Dashboard of the Jenkins classic UI), click Credentials > System on the left.
  3. Under System, click the Global credentials (unrestricted) link to access this default domain.
  4. Click Add Credentials on the left. Note: If there are no credentials in this default domain, you could also click the add some credentials link (which is the same as clicking the Add Credentials link).
  5. From the Kind field, choose the type of credentials to add.
  • From the Scope field, choose either:
  • Global - if the credential/s to be added is/are for a Pipeline project/item. Choosing this option applies the scope of the credential/s to the Pipeline project/item "object" and all its descendent objects.
  • System - if the credential/s to be added is/are for the Jenkins instance itself to interact with system administration functions, such as email authentication, agent connection, etc. Choosing this option applies the scope of the credential/s to a single object only.
  • Add the credentials themselves into the appropriate fields for your chosen credential type:

(...)

  • Username and password - specify the credential’s Username and Password in their respective fields. (...)
  • In the ID field, specify a meaningful credential ID value - for example, jenkins-user-for-xyz-artifact-repository. You can use upper- or lower-case letters for the credential ID, as well as any valid separator character. However, for the benefit of all users on your Jenkins instance, it is best to use a single and consistent convention for specifying credential IDs. Note: This field is optional. If you do not specify its value, Jenkins assigns a globally unique ID (GUID) value for the credential ID. Bear in mind that once a credential ID is set, it can no longer be changed.
  • Specify an optional Description for the credential/s.
  • Click OK to save the credentials.
    Step 3 - Use the credentials in your pipeline declaration

You can use the credentials in your jenkinsFile like so:

pipeline {
  stages {
    stage('Clone stage') {
       steps {
         git url: 'https://gitlab.com/[username]/[my-repo].git', branch: 'master', credentialsId: 'my-gitlab-repo-creds'
       }
    }
  }    
}

In the above example I assume you picked the id my-gitlab-repo-creds in step 2.

3j86kqsm

3j86kqsm2#

Jenkins连接私有回购Jenkins[Ubuntu] #的用户数据以安装Jenkins

curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo tee \/usr/share/keyrings/jenkins-keyring.asc > /dev/null echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \ https://pkg.jenkins.io/debian-stable binary/ | sudo tee \ /etc/apt/sources.list.d/jenkins.list > /dev/null
sudo apt-get update -y 
sudo apt-get install jenkins

设置1:-在Jenkins服务器上打开SSH登录

sudo vim /etc/passwd 
edit -> jenkins:x:115:122:Jenkins,,,:/var/lib/jenkins:/bin/bash (false)
if false then make it bash

步骤2:-切换到Jenkins用户

sudo su - jenkins 
          ssh-keygen 
          cd /.ssh/
          cat id_rsa.pub   #copy public key 
          add public key into gitlab/github account in ssh keys 
          try to clone private repo in Jenkins user using ssh url
          If it is successfully clone then you can proceed 
          rm -rf clonedir   #remove clone dir

第3步:-转到JenkinsWeb用户界面[http://public-ip:8080]

Open manage Jenkins 
           Go to manage credentials and add new credentials 
           Select option ssh username with private key 
           username -> jenkins
           private key -> Enter Directly 
           paste jenkins user private key (sudo cat /var/lib/jenkins/.ssh/id_rsa)

第3步:-转到JenkinsWeb用户界面[http://public-ip:8080]

Select new item [any type of job]
           Select source management -> git 
           paste your git repo 
           Select jenkins credentials 
           select branch in which you want to work on.

注意:如果你使用的是内部部署的gitlab,那么你的ssh url应该是

  1. ssh://git@gitlab.主机名/目录/存储库.git
    1.你必须将你的jenkins服务器ip加入gitlab内部防火墙的白名单。

相关问题