Jenkins


3 분 소요

설치

https://www.jenkins.io/doc/book/installing/docker/

위 내용에 따라 설치하면 된다. docker 기반으로 설치하여 확장성 및 snapshot에 유리하도록 한다.

총 두개의 docker를 사용한다.

  • Docker in Docker: Jenkins 내에서 docker를 사용하기 위해 사용한다. privileged 옵션을 줘야한다.
  • Jenkins Blueocean: pipeline 시각화 도구인 Blueocean 이미지를 사용한다.

Jenkins Blueocean 이미지를 사용하기 위해 Docker build를 하게 되는데, 이 때, project build에 필요한 환경세팅을 함께 한다. (make, gradle 등 설치)

Jenkinsfile

Pipeline을 사용하기 위한 파일이다. groovy 문법을 사용한다.

dir: Change current directory

steps 내에서 사용할 수 있고, 문법은 다음과 같다.

steps {
    dir('디렉토리') {
        echo '~~~'
    }
}

해당 디렉토리가 없으면 생성한다.

Variable

def를 통해 정의할 수 있다. scope를 통해 지역/전역 구분이 된다. 최소 단위는 Step()인 듯.

def name = value

sh : Shell Script

sh 'Shell scripts like "ls -al"'

'''
Multi line shell scripts like
IP='localhost'
ssh user@$IP -p 22
'''

sh "Use double quotes for using variables like 'echo ${name}'"

"""
Multi line shell
echo ${name}
"""

쌍따옴표로 쉘을 사용하는 경우, Jenkins 변수를 사용할 수 있지만, 쉘 내 변수는 $를 이스케이프 해주어야 한다.

echo "${JENKINS_VARIABLE} \$SHELL_VARIABLE"

agent : Run step by agent

해당 stage를 동작할 agent를 선택. 일반적으로 Docker를 사용. dind 이미지가 작동하고 있어야 하며, Host의 docker namespace와 dind의 docker namespace가 분리되어 있음. image가 dind에 없는 경우, docker hub에서 다운 받으므로, 업로드 해놔야 함.

docker {
  image '이미지'
  reuseNode true
}

Multibranch Pipeline

새로운 Item -> Multibranch Pipeline

  • Display Name: 프로젝트 명
  • Branch Sources: Git
    • Project Repository: Https Git 주소
    • Credentials
      • Username with password: 계정 이름과 패스워드
        • App password를 할당받아야함(Github or Bitbucket)
    • Behaviours
      • Discover branches
      • Discover tags
  • Build Configuration
    • Mode: by Jenkinsfile
      • Script Path: Jenkinsfile.groovy
        • Repository에 저장된 jenkinsfile path
  • Scan Multibranch Pipeline Triggers
    • Build when a change is pushed to BitBucket

Build Triggers

  1. Jenkins Plugin 설치
    • Multibranch Scan Webhook Trigger
  2. Item 혹은 pipeline 설정
    • Scan Multibranch Pipeline Triggers
    • Scan by webhook 체크
      • Trigger token 설정(ex, trigger-jenkins)
  3. BitBucket Repository Webhook 설정
    • Add Webhook
    • Title
    • URL
      • 위에서 설정한 Trigger token을 이용해 URL 설정
      • http://jenkins-host-url:port/multibranch-webhook-trigger/invoke?token=trigger-jenkins
    • Status: Active 체크
    • Triggers 체크
      • Repository: Push
      • Pull request: Created, Updated

Build in Docker

Build 전용 docker image를 사용한 agent에서 Build를 진행한다.

Host의 docker objects(containers, images)와 독립적인 objects pool을 가지고 있으므로, 서로 공유가 되지 않는다. 즉, docker image를 dind 컨테이너에서 Host docker와 별도로 가지고 있어야 한다.
docker inspect -f . {OBJECT} 명령을 통해서 OBJECT 존재 여부를 확인할 수 있다.
예시: docker exec {DIND CONTAINER} docker inspect -f . ubuntu

Error

Build container 내에서 직접 빌드하면 되는데, Jenkins를 거쳐서 빌드하면 Check C Compiler에서 막힘. ㅠㅠ

Std Output

-- Check for working C compiler: /gcc-arm-none-eabi-10.3-2021.10/bin/arm-none-eabi-gcc
CMake Error: Generator: execution of make failed. Make command was: "/usr/bin/gmake" "cmTC_64b0b/fast"
-- Check for working C compiler: /gcc-arm-none-eabi-10.3-2021.10/bin/arm-none-eabi-gcc -- broken
CMake Error at /usr/share/cmake-3.10/Modules/CMakeTestCCompiler.cmake:52 (message):
  The C compiler

    "/gcc-arm-none-eabi-10.3-2021.10/bin/arm-none-eabi-gcc"

  is not able to compile a simple test program.

  It fails with the following output:

    Change Dir: /var/jenkins_home/workspace/Light_FW_dev/build/CMakeFiles/CMakeTmp

    Run Build Command:"/usr/bin/gmake" "cmTC_64b0b/fast"
    No such file or directory
    Generator: execution of make failed. Make command was: "/usr/bin/gmake" "cmTC_64b0b/fast"


  CMake will not be able to correctly generate this project.

CMake는 C compiler가 정상적으로 동작하는지 확인하기 위해 int main(void){ return -1;}을 컴파일 해보는데, 이 때, 에러가 나면 위와 같은 오류가 난다. (–broken)

CMakeError.log

Compiling the C compiler identification source file "CMakeCCompilerId.c" failed.
Compiler: /gcc-arm-none-eabi-10.3-2021.10/bin/arm-none-eabi-gcc
Build flags: --std=gnu99;-Wall;-Wno-attributes;-Wno-format;-ffunction-sections;-fdata-sections;-fno-strict-aliasing;-fno-builtin;--short-enums
Id flags:

The output was:
1
/gcc-arm-none-eabi-10.3-2021.10/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld: /gcc-arm-none-eabi-10.3-2021.10/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/lib/libc.a(lib_a-exit.o): in function `exit':
exit.c:(.text.exit+0x2c): undefined reference to `_exit'
collect2: error: ld returned 1 exit status


Determining if the C compiler works failed with the following output:
Change Dir: /var/jenkins_home/workspace/Light_FW_dev/build/CMakeFiles/CMakeTmp

Run Build Command:"/usr/bin/gmake" "cmTC_d0fb0/fast"
No such file or directory
Generator: execution of make failed. Make command was: "/usr/bin/gmake" "cmTC_d0fb0/fast"

해결

Build 환경을 기존에는 Jenkins 내에 구축하였다가, Build 전용 Container로 옮기면서 생기는 문제였음. 디렉토리는 volume으로 공유하는데, container 환경이 바뀌면서 생겼던 문제. Cache를 지워버리고 새로 컴파일하니 해결 됨.

사이트 공유

|tag|목적|링크| |—|—|—| |multibranch|공식 생성하는 법|https://www.jenkins.io/doc/book/pipeline/multibranch/| |webhook|깃허브|https://velog.io/@jae_cheol/%EB%A9%80%ED%8B%B0%EB%B8%8C%EB%9E%9C%EC%B9%98-%ED%8C%8C%EC%9D%B4%ED%94%84%EB%9D%BC%EC%9D%B8|

관련 내용에 대한 질문이나 태클을 환영합니다. 댓글 남겨주세요.



태그:

카테고리:

작성:

업데이트:

댓글남기기