ABOUT ME

-

Total
-
  • Jenkins: Rust 프로젝트 테스트
    컴퓨터/Jenkins 2023. 4. 17. 19:35
    728x90
    반응형

    해볼 것: Jenkins 빌드 오류 시 메일 보내기

     

    Jenkins를 경험하기 위해 Rust 프로젝트 테스트를 하는 것을 해보았다.

    Dockerfile을 이용하여 러스트 프로젝트 테스트를 실행할 것이다.

    Windows11에서 하려고 했으나 복잡해져서 Zorin OS 듀얼 부팅으로 넘어왔다.

     

    윈도우 에러

    설치 시에 this account either does not have the privilege to logon as a service 부터 막혀서

    Windows Tools - 로컬 보안 정책 - 로컬 정책 - 사용자 권한 할당 - 서비스로 로그온 - 사용자 추가를 눌러 현재 계정을 넣었다.

    비번은 windows 에 연결된 마이크로소프트 비번이었다.

     

    localhost에 돌리는 만큼 아래 플러그인을 받아서 

    Dashboard - Jenkins 관리 - Plugin Manager - Advanced settings에서 직접 플러그인을 넣어서 설치하고 plugin을 받았다.

    https://updates.jenkins-ci.org/download/plugins/skip-certificate-check/

     

    Jenkins pipeline 을 만들어서 Github plugin을 깔았기 때문에

    Build Triggers - GitHub hook trigger for GITScm polling 을 체크해 주었다.

     

    Docker를 돌리는데 에러가... 삽질을 계속하다가 너무 꼬여서 Linux로 옮겨갔다.

    C:\Users\dev\AppData\Local\Jenkins\.jenkins\workspace\Rust-Test>docker inspect -f . "ecdd3515c8d864fff5c8527ae17d22684b74db98" 
    .
    [Pipeline] }
    [Pipeline] // withEnv
    [Pipeline] withDockerContainer
    Jenkins does not seem to be running inside a container
    $ docker run -d -t -w C:/Users/dev/AppData/Local/Jenkins/.jenkins/workspace/Rust-Test/ -v C:/Users/dev/AppData/Local/Jenkins/.jenkins/workspace/Rust-Test/:C:/Users/dev/AppData/Local/Jenkins/.jenkins/workspace/Rust-Test/ -v C:/Users/dev/AppData/Local/Jenkins/.jenkins/workspace/Rust-Test@tmp/:C:/Users/dev/AppData/Local/Jenkins/.jenkins/workspace/Rust-Test@tmp/ -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** ecdd3515c8d864fff5c8527ae17d22684b74db98 cmd.exe
    [Pipeline] // withDockerContainer
    [Pipeline] }
    [Pipeline] // withEnv
    [Pipeline] }
    [Pipeline] // node
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] }
    [Pipeline] // withEnv
    [Pipeline] End of Pipeline
    java.io.IOException: Failed to run image 'ecdd3515c8d864fff5c8527ae17d22684b74db98'. Error: docker: Error response from daemon: the working directory 'C:/Users/dev/AppData/Local/Jenkins/.jenkins/workspace/Rust-Test/' is invalid, it needs to be an absolute path.
    See 'docker run --help'.

     

    Linux

    Zorin OS에서 다시 시작했다. Jenkins 서버를 열고 아래와 같이 Dockerfile을 만들었다.

    FROM rust:latest
    
    RUN apt-get update && \
        apt-get install -y \
        build-essential \
        pkg-config \
        libssl-dev \
        libpq-dev \
        && \
        rm -rf /var/lib/apt/lists/*
    
    WORKDIR /usr/src/app
    
    COPY . .

     

    그런데 빌드할 때 아래와 같은 오류가 나와서

    + docker build -t my-linux:4 -f Dockerfile.linux .
    ERROR: permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get "http://%2Fvar%2Frun%2Fdocker.sock/_ping": dial unix /var/run/docker.sock: connect: permission denied

    이 명령어를 호스트에서 해줘야 했다.

    sudo usermod -aG docker jenkins

     

    테스트 실패 시 이메일을 보내고 싶어서 Gmail SMTP 서버를 사용하려다가 내 구글 계정은 2단계 인증이 되어 있어서

    앱 비밀번호를 만들어야 했다 (https://support.google.com/accounts/answer/185833)

    메뉴에 없는데 2단계 인증에 들어가면 맨 아래에 앱 비밀번호를 생성할 수 있다.

    메일 - 윈도우 컴퓨터 설정

     

    Jenkins 관리 - Configure System에 들어간다. 아래와 같이 설정하고 Crendentials에서 Add를 눌러

    자신의 구글 계정과 앱 비밀번호 생성한 것을 복붙 하면 된다.

     

    Jenkinsfile

    cargo test에서 실패하면 post.failure가 작동한다. always로 메일 계속 보내도 된다.

    pipeline {
        agent none
    
        environment {
            RUST_BACKTRACE = 1
            CARGO_TERM_COLOR = 'never'
        }
    
        stages {
            stage('Checkout') {
                agent any
                steps {
                    checkout scm
                }
            }
            
            stage('Build and Test') {
                agent {
                    dockerfile {
                        filename 'Dockerfile.linux'
                        dir '.'
                    }
                }
                steps {
                    sh 'cargo test'
                }
            }
        }
    
        post {
            failure {
                emailext (
                    subject: "Jenkins Rust CI Failed: ${currentBuild.fullDisplayName}",
                    body: "The build has failed. Check the build log at ${env.BUILD_URL}",
                    to: '@kakao.com', // replace with your email address
                    replyTo: '@kakao.com' // replace with the desired reply-to email address
                )
            }
        }
    }

     

    일부러 Rust 테스트에 오류를 만들고 빌드해 보았다.

     

     

    실패하니 아래처럼 메일이 자동으로 왔다.

    728x90

    댓글