김붕어87 2022. 5. 16. 14:00
반응형

Jenkins 이란?


Jenkins은 CI/CD 툴이다.

> 지속적인 통합(Continuous Intergration), 지속적 배포(Continuous Delivery)

Jenkins으로 빌드, 테스트, 배포 프로세스를 자동화하여 개발 생산을 올릴 수 있다.

 

Jenkins을 사용하는 이유는 git에 데이터를 올리면 jenkins가 트리거 받아서 자동으로 JOB을 실행할 수 있다.

복잡한 빌드,배포를 자동화로 간편작업 할 수 있다.

개발자는 git에 데이터만 올리기만하면 나머지 빌드는 자동화으로 되기 때문에 개발에만 집중 시킬 수 있다.

 

 

 

jenkins trigger 설정


1. github 접속

 

2. git webhook 설정

github에 데이터가 push되면 jenkins으로 webhook 전달하기 위한 설정.

> git repository 접근 > repository settings > webhooks 클릭

> Payload URL : jenkinsURL:8080/github-webhook/   ( / 슬래쉬 꼭 넣기 )
> Content Type : application/json

> Add webhook 클릭

3. git personal access tokens 생성

jenkins에서 github 접근할 수 있도록 token 생성

 

> settings > developer settings > personal access tokens > generate new token 클릭

repo 체크

admin:org 체크 

admin:repo_hook 체크

 

4. Jenkins 플러그인 설치

github 연동할 수 있도록 github 플러그인 설치

 

> jenkins dashboard > jenkins 관리 > 플러그인 관리 > 철기 가능 > "GitHub Integration Plugin" 설치 및 재시작

 

5. github servers 설정

> jenkins dashboard > jenkins 관리 > 환경 설정 > github server 설정

 

6. Jenkins Job 생성 - 1

Github에 데이터 push가 되면 trigger 받아서 job이 자동으로 실행 

 

> jenkins dashboard > 새로운 Item > Freestyle project 생성

 

7. jenkins job 생성 - 2 

scripts 설정


[ jenkins job scripts 내용 ] 

 

node { 
    stage('1 - build.sh ') { 
        echo 'build.sh start' 
            
            echo "git clone"
            sh 'rm -rf ./dongwook8706'
            sh 'git clone https://github.com/dongwook8706/dongwook8706.git'
            > git clone으로 데이터 복제


            echo "chart build"
            sh 'sh ./dongwook8706/chart.sh'
            > repository에 있는 chart.sh 실행


            sh 'cat list.txt > ekstest.properties'
            archiveArtifacts 'ekstest.properties'

            > spinnaker에게 property 파일 전달하기 위한 설정


        echo 'build.sh end' 
    } 
}

 


[ chart.sh 내용 ]

#!/bin/bash

echo "helm package"
rm -rf *.tgz
helm package ./dongwook8706/docker/nginx/

list=`ls |grep nginx|grep tgz`
name=`ls |grep nginx|grep tgz |awk -F - '{print $1}'`
version=`ls |grep nginx|grep tgz |awk -F - '{print $2}' |sed 's/.tgz//g'`
echo $list
echo $name
echo $version
echo "version=$version" > list.txt

echo ""
echo "start - chartmuseum upload"
helm repo update
curl -X DELETE http://localhost:8081/api/charts/$name/$version
curl --data-binary "@$list" http://localhost:8081/api/charts
helm repo update
echo "end - chartmuseum upload"
echo ""


echo ""
echo "start - image ecr upload"
cd ./dongwook8706/build
sh build.sh $version
echo "end - image ecr upload"
echo ""


[ build.sh 내용 ]

#!/bin/bash

echo "build.sh start"
echo $1
echo "ekstest - nginx v$1" > index.html
echo "build.sh end"

echo "dockerfile start"
docker build -t nginx-test:$1 .
docker images |grep nginx-test
echo "dockerfile end"



echo "ecr_upload start"
sh ecr_upload.sh $1
echo "ecr_upload end"


[ ecr_upload.sh 내용 ]

#!/bin/bash


echo "ecr login"
aws ecr get-login-password --region ap-northeast-2 | docker login --username AWS --password-stdin ACCOUNT계정.dkr.ecr.ap-northeast-2.amazonaws.com

echo "docker tag"
docker tag nginx-test:$1 ACCOUNT계정.dkr.ecr.ap-northeast-2.amazonaws.com/ekstest1:$1
docker push ACCOUNT계정.dkr.ecr.ap-northeast-2.amazonaws.com/ekstest1:$1

반응형