이번 포스팅은 Docker를 통한 Jenkins가 설치되어있다고 생각하고 진행합니다.
Webhook, 특정 애플리케이션에게 이벤트 발생을 실시간을 알려주기 위한 방법
ex) 젠킨스의 Webhook URL을 열고 Gitlab에서 Webhook URL로 요청을 받아 이벤트 발생 사실을 확인
Job(잡)은 특정 작업을 수행하는 단위로, 일반적으로 소프트웨어의 빌드, 테스트, 배포 등을 자동화하기 위해 설정됩니다.
Job을 생성하는 방식은 크게 2가지로 나눠진다.
GUI보다는 코드 기반으로 작성하는 것이 편리하다는 의견을 확인하여 파이프라인으로 진행
파이프라인은 크게 2가지 방식으로 나눠진다.
Declarative 스타일로 많이 이동된다고 하여 Declarative 방식 선택
Jenkins에서 관련 플러그인을 설치
Credentials(자격증명) 추가하여 Jenkins가 GitLab에 접근해서 pull or push or merge 할 수 있도록 정보를 등록
새로운 Item 클릭
이름 작성 후 Pipeline 선택 후 OK
script 작성(Declarative 방식)
pipeline {
agent any
stages {
stage('Build') {
steps {
//
}
}
stage('Test') {
steps {
//
}
}
stage('Deploy') {
steps {
//
}
}
}
익숙해진다면 코드를 작성하는 것이 좋지만 익숙하지 않다면 Snippet Generator를 사용해서 작성한다.
pipeline {
agent any
stages {
stage('Clone') {
steps {
git branch: 'dev', credentialsId: 'qkrtprjs', url: 'https://lab.ssafy.com/qkrtprjs456/test.git'
}
post {
failure {
echo 'Repository clone failure !'
}
success {
echo 'Repository clone success !'
}
}
}
}
}
Dashboard -> dev -> Configuration
- Build when a change is pushed to GitLab 체크
- 원하는 트리거 설정
- 하단에 고급버튼 클릭
- Secret token에 Generate 클릭 후 토큰 기억하기
dev
"Invalid token" 발생
...Secret Token 빼먹었다
하지만 GitLab에서는 정상적으로 테스트가 완료되었고 Jenkins로 넘어왔지만 Jenkins에서 에러가 발생
ERROR: Error cloning remote repo 'origin'
hudson.plugins.git.GitException: Command "git fetch --tags --force --progress -- https://lab.ssafy.com/qkrtprjs456/test.git +refs/heads/*:refs/remotes/origin/*" returned status code 128:
stdout:
stderr: remote: HTTP Basic: Access denied. The provided password or token is incorrect or your account has 2FA enabled and you must use a personal access token instead of a password. See https://lab.ssafy.com/help/topics/git/troubleshooting_git#error-on-git-fetch-http-basic-access-denied
위 와 같은 에러가 발생 내용을 보면 2FA 인증이나 토큰과 관련된 에러같은데 해결해보자
Webhook을 통해 프로젝트의 push를 인식하고 clone하도록 정상적으로 확인
이어서 빌드와 배포 과정도 실행하도록 구현해보자