Jenkins Freestyle Project 활용

w00j00ng351·2022년 9월 27일
0

devops

목록 보기
3/4
post-thumbnail

1. 개발환경 구축

Gitea 원격 저장소 생성

  • Gitea 가입 및 로그인

  • 우측 위 + 버튼 -> 새 저장소 버튼 클릭

    • 혹은 {GITEA_URL}/repo/create 접속

gitea-create-repo

gitea-new-repo

  • Gitea 토큰 생성
    • 우측 상단 프로필사진 클릭 > 설정 > 어플리케이션
    • 혹은 {GITEA_URL}/{user_name}/settings/applications 접속

gitea-generate-token

개발 컨테이너 생성 및 지역 저장소 생성

  • 개발 컨테이너 생성

    $ docker run -itd --net net-devops --name myproj -h myproj debian
  • 개발 컨테이너 접속

    docker exec -it myproj /bin/bash
    root@myproj:/#
  • 필수 프로그램 설치

    root@myproj:/# apt update -y && apt install -y git vim
  • 프로젝트 폴더 생성 및 지역 저장소 생성

    root@myproj:/# mkdir myproj && cd myproj
    root@myproj:/myproj#
    root@myproj:/myproj# git init
    root@myproj:/myproj# git config --local user.name myuser
    root@myproj:/myproj# git config --local user.email myuser@a.b

원격 저장소 연결

  • 원격 저장소 연결

    root@myproj:/myproj# git remote add origin http://172.18.0.2:3000/myuser/myrepo.git

2. Jenkins Freestyle Project 생성

프로젝트 생성

  • 새로운 Item 클릭
    • 혹은 {JENKINS_URL}/view/all/newJob 접속

jenkins-new-project

  • Freestyle project 선택 후 OK 클릭

프로젝트 설정

  • 소스코드 관리

    • Git 선택
    • Repository URL에 gitea 원격저장소 주소 입력
    • Credentials Gitea Personal Access Token 추가
  • 빌드 유발

    • Poll SCM 클릭
  • Build Steps

    • Add build step 클릭
    • Execute shell 선택
      • shell 내용에 하기내용 작성 작성
        chmod +x main.sh
        ./main.sh

jenkins-new-freestyleproj

jenkins-gitea-credential

3. Gitea 웹훅 등록

웹훅 추가

프로그램 작성 및 원격 저장소 반영

  • main.sh 작성

    • 현재 디렉터리의 message.txt 파일에 시간 정보를 작성하는 프로그램
    • 아래 내용을 main.sh 파일에 작성
    #!/bin/bash
    echo $(date) >> message.txt
  • 커밋 및 원격 저장소에 내용 반영

    root@myproj:/myproj# git add main.sh && git commit -m "Start" && git push origin master

gitea-new-webhook

4. Jenkins 확인

Jenkins UI

  • 대시보드 프로젝트 공간에서 빌드 상황을 확인할 수 있음
  • 스크립트 실행 내용 및 출력 결과를 확인할 수 있음

jenkins-freestyle-project-dashboard

jenkins-freestyle-project-console-output

Jenkins Workspace

  • jenkins 컨테이너 접속
$ docker exec -it jenkins /bin/bash
jenkins@jenkins:/$
  • workspace 폴더로 이동

    $ cd $HOME/workspace/
    jenkins@jenkins:~/workspace$ pwd
    /var/jenkins_home/workspace
    jenkins@jenkins:~/workspace$ ls
    myfreeproj
  • 프로젝트 폴더로 이동 및 출력결과 확인

    $ cd myfreeproj/
    jenkins@jenkins:~/workspace/myfreeproj$ ls
    main.sh  message.txt
    jenkins@jenkins:~/workspace/myfreeproj$ cat message.txt
    Tue Sep 27 13:34:28 UTC 2022
profile
시간이 만든 코드

0개의 댓글