엔지니어링/CI-CD

[Jenkins] Gitlab Private Repogitory 가져오기

joon95 2022. 8. 11. 16:49
반응형

오늘은 젠킨스에서 깃랩 프라이빗 레포지토리를 가져오는 걸 남긴다.

방법은 username/password 와 access token 2가지 모두 포스팅한다.

Gitlab Access Token 생성

접근토큰 발급은 너무 간단하다. GItlab에 로그인 한 후 User Settings > Access Tokens 으로 들어가서 토큰이름, 만료일, 허용 권한을 체크한 뒤 발급하면 된다.

Create personal access token 버튼을 클릭하면

위와 같이 토큰이 발급된다.

스크롤을 내리면 생성된 토큰 정보도 확인할 수 있다.

 

Jenkins

New item을 클릭하면 여러 템플릿이 존재하는데 Freestyle Project 와 Pipeline 방식을 해볼거다.

Freestyle

설정(Username/password)

Freestyle 방식으로 생성하고 소스코드 관리탭으로가서 Git 정보를 기입한다.

Credentials 항목에 계정/패스워드 정보를 기입한 뒤 만든 branch 명을 입력

Build탭에서는 간단하게 clone 받아진 목록을 보이게 했다.

테스트

Build Now 를 눌러 돌려보면 명령어가 실행된 로그를 볼 수 있다.

 

pipeline

설정(Access Token)

Pipeline은 내가 순서를 지정하여 빌드과정을 세분화할 수 있다.

여기선 gitlab에서 발급한 access token을 사용하여 접근한다.

Credentials(자격증명)을 등록해야하는데,

 Jenkins 관리 > Manage Credentials > Domainsglobal > Add Credentials 순서로 들어간다

새로운 자격증명을 추가하는 페이지에 들어왔으면

Kind : GitLab API token 로 설정한 뒤 입력해주면된다.

(만약 GitLab API token 이 없다면 gitlab 관련 플러그인을 설치해야함)

이제 다시 파이프라인으로 와서 git 접근 대상, du 명령어를 입력해주었다.

pipeline {
    agent any

    stages {
        stage('clone') {
            steps {
                git branch: 'master', credentialsId: 'gitlab-login-user', url: 'https://gitlab.com/leejongsoo11/nolmyeon_mwohani.git'
            }
        }
        stage('file directory') {
            steps {
                sh 'du'
            }
        }
    }
}

참고로 pipeline 작성 시엔 

Pipeline syntax 라는 링크를 들어가

이런식으로 스크립트를 추출할 수 있다.

 

테스트

 

Jenkins 구성

필자가 생성한 젠킨스는 ARO에 redhat 카탈로그 템플릿을 이용하였고, 젠킨스에서 생성한 Item 들은 ‘/var/lib/jenkins/jobs/’ 아래 생긴다.

bash-4.4$ ls /var/lib/jenkins/jobs/
gitlab-freestyle  gitlab-pipeline

그리고 아무거나 폴더를 들어가보면 아래의 구조가 나온다.

bash-4.4$ ls /var/lib/jenkins/jobs/gitlab-freestyle
builds  config.xml  nextBuildNumber  workspace  workspace@tmp

builds 는 내가 빌드를 돌린 내역(로그 등)들이 존재하고

config.xml item 설정에서 기입한 내역들이 저장되어있다.

<?xml version='1.1' encoding='UTF-8'?>
<project>
  <actions/>
  <description></description>
  <keepDependencies>false</keepDependencies>
  <properties>
    <hudson.plugins.jira.JiraProjectProperty plugin="jira@3.7.1"/>
    <com.dabsquared.gitlabjenkins.connection.GitLabConnectionProperty plugin="gitlab-plugin@1.5.35">
      <gitLabConnection></gitLabConnection>
      <jobCredentialId></jobCredentialId>
      <useAlternativeCredential>false</useAlternativeCredential>
    </com.dabsquared.gitlabjenkins.connection.GitLabConnectionProperty>
  </properties>
  <scm class="hudson.plugins.git.GitSCM" plugin="git@4.11.4">
    <configVersion>2</configVersion>
    <userRemoteConfigs>
      <hudson.plugins.git.UserRemoteConfig>
        <url>https://gitlab.com/dkttkemf/metanet-mydata.git</url>
        <credentialsId>gitlab-login-user</credentialsId>
      </hudson.plugins.git.UserRemoteConfig>
    </userRemoteConfigs>
    <branches>
      <hudson.plugins.git.BranchSpec>
        <name>*/main</name>
      </hudson.plugins.git.BranchSpec>
    </branches>
    <doGenerateSubmoduleConfigurations>false</doGenerateSubmoduleConfigurations>
    <submoduleCfg class="empty-list"/>
    <extensions/>
  </scm>
  <canRoam>true</canRoam>
  <disabled>false</disabled>
  <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
  <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
  <triggers/>
  <concurrentBuild>false</concurrentBuild>
  <builders>
    <hudson.tasks.Shell>
      <command>echo &apos;test&apos;
git show
du</command>
      <configuredLocalRules/>
    </hudson.tasks.Shell>
  </builders>
  <publishers/>
  <buildWrappers/>

Workspace 는 빌드 시 작업이 진행되는 공간으로 git 에서 떙겨온 파일들이 존재한다.

bash-4.4$ pwd
/var/lib/jenkins/jobs/gitlab-freestyle/workspace
bash-4.4$ ls -al
total 20
drwxr-sr-x. 3 1000690000 1000690000 4096 Aug 11 06:06 .
drwxr-sr-x. 5 1000690000 1000690000 4096 Aug 11 06:24 ..
drwxr-sr-x. 8 1000690000 1000690000 4096 Aug 11 06:24 .git
-rw-r--r--. 1 1000690000 1000690000 6741 Aug 11 04:19 README.md

 

끝!

반응형