관리 메뉴

개발그래머

Jenkins[10] Jenkins + Ansible 연동 본문

Devops/Jenkins

Jenkins[10] Jenkins + Ansible 연동

임요환 2023. 3. 26. 21:30

1. Ansible 서버 등록

젠킨슨관리 -> 시스템 -> publish over ssh 

 

2. Ansible project item 생성(기존 docker project를 카피하여 생성)

 

3. ssh publishers 변경

 

4. 지금 빌드 실행 -> Ansible 서버에서 war파일 생성되었는지 확인

 

5. ansible 서버에서 playbook 생성

vi first-devops-playbook.yml //플레이북 생성

- hosts: all
#   become: true

  tasks:
  - name: build a docker image with deployed war file
    command: docker build -t cicd-project-ansible .   //Docker파일과 war를 이용하여 docker image 생성
    args:
        chdir: /root
Dockerfile

FROM tomcat:9.0
LABEL org.opencontainers.image.authors="edowon0623@gmail.com"
COPY ./hello-world.war /usr/local/tomcat/webapps

6. hosts 파일 생성 (자기 자신만 설정)

vi hosts

172.17.0.4 //Ansible server ip

 

7. ansible-playbook -i hosts first-devops-playbook.yml

 

8. 도커 이미지 생성 확인

 

9. jenkins 구성 SSH Publishers에서 ansible-playbook -i hosts first-devops-playbook.yml 커맨드 추가

 

10. 이미지 생성 후 도커 컨테이너까지 실행하도록 playbook 파일 수정

- hosts: all
#   become: true

  tasks:
  - name: build a docker image with deployed war file
    command: docker build -t cicd-project-ansible .
    args:
        chdir: /root

  - name: create a container using cicd-project-ansible image
    command: docker run -d --name my_cicd_project -p 8080:8080 cicd-project-ansible

11. poll SCM 설정 -> 깃 커밋 후 컨테이너 실행되는지 확인

 

12. 다시 빌드 시 아직 unstable 에러 발생

 

13. 해결하기 위해 playbook 파일 수정(docker 중지 + 컨테이너 삭제 + 이미지 삭제 추가)

- name: create a container using cicd-project-ansible image
- hosts: all
#   become: true

  tasks:
  - name: stop current running container
    command: docker stop my_cicd_project
    ignore_errors: yes

  - name: remove stopped cotainer
    command: docker rm my_cicd_project
    ignore_errors: yes

  - name: remove current docker image
    command: docker rmi cicd-project-ansible
    ignore_errors: yes

  - name: build a docker image with deployed war file
    command: docker build -t cicd-project-ansible .
    args:
        chdir: /root

  - name: create a container using cicd-project-ansible image
    command: docker run -d --name my_cicd_project -p 8080:8080 cicd-project-ansible

14. 다시 빌드 후 배포 확인

15.  docker 허브로 관리하는 부분 추가

https://limyohwan.tistory.com/27

 

Ansible[5] Docker 이미지 관리

1. 도커 허브에 도커 이미지 만들어서 push 하기 -> docker hub 확인 docker tag cicd-project-ansible dyghks7102/cicd-project-ansible // 기존 이미지에 태그 삽입 docker login //나의 도커허브 로그인 docker push dyghks7102/cicd

limyohwan.tistory.com

 

16.  jenkins 구성 SSH Publishers 수정