TY_Daily

젠킨슨 백업 본문

SSAFY프로젝트

젠킨슨 백업

TY_TaeYeon 2024. 8. 7. 02:07

Docker CLI 설치


# /var/run/docker.sock 파일의 권한을 666으로 변경하여 그룹 내 다른 사용자도 접근 가능하게 변경 
sudo chmod 666 /var/run/docker.sock 

# ubuntu 유저를 docker 그룹에 추가 
sudo usermod -a -G docker ubuntu

출처: https://buildabetterworld.tistory.com/179 [Seungyeup's blog:티스토리]

 

pipeline{
agent any
    
    stages{
     stage('gitclone'){
         steps{
             git branch: 'master',
              credentialsId: 'KTY2', 
             url: "https://lab.ssafy.com/s11-webmobile3-sub2/S11P12D101.git"
            }
        }
        
         
        
        
        
        
        stage('Kiosk Front_Build and Deploy Kiosk Front') {
            steps {
                dir('/var/jenkins_home/workspace/D101-final/FE/kiosk/'){
                   
                    sh 'ls -al'
                    sh 'whoami'
                    
                    sh 'docker ps -a'
                    // react_container 존재하면 멈추고 삭제하기
                    sh '''
                      if [ "$(docker ps -aq -f name=react_container)" ]; then
                            docker stop react_container
                            docker rm react_container
                        fi
                    '''
                    
                    
                    
                    // 'react-image'이라는 이미지가 존재하는지 확인하고 삭제
                    sh '''
                        if [ "$(docker images -q react-image 2> /dev/null)" != "" ]; then
                            docker rmi react-image
                        fi
                    '''

                    sh 'docker build --no-cache -t react-image .'
                    // 위 명령어까지 진행되면 
                    
                    // 컨테이너가 존재하면 삭제
                    sh '''
                    if [ "$(docker ps -aq -f name=react_container)" ]; then
                        docker rm -f react_container
                    fi
                    '''
                    // 새로운 컨테이너 생성
                    sh 'docker run -d --name react_container -p 3000:3000 react-image'
                    
                    
                     }
                
                
            }
        }
        
        
        
        
        
        
        
        //  stage('aws property file download play cp execute') {
       // steps {
        //     withCredentials([file(credentialsId: 'db_properties', variable: 'dbConfigFile')]) {
        //     script {
        //         // 파일 존재 여부 확인
        //         def filePath = '/var/jenkins_home/workspace/D101-final/BE/o2o/src/main/resources/application-aws.properties'
        //         def fileExists = sh(script: "test -f ${filePath} && echo true || echo false", returnStdout: true).trim()

        //         if (fileExists == 'true') {
        //             // 파일이 존재하면 삭제 후 복사
        //             sh "rm ${filePath}"
        //         }
        //         // 파일 복사
        //         sh "cp ${dbConfigFile} ${filePath}"
        //     }
      //  }
       // }
        //  }
         
         
         
        
        
        

        
        
        // stage('Docker_Build and Deploy Backend') {
        //     steps {
        //         dir('/var/jenkins_home/workspace/D101-final/BE/o2o/'){
                   
        //             sh 'ls -al'
        //             sh 'whoami'
                    
        //             sh 'groups jenkins'
        //             sh 'docker ps -a'
        //             // my_container가 존재하면 멈추고 삭제하기
        //             sh '''
        //               if [ "$(docker ps -aq -f name=my_container)" ]; then
        //                     docker stop my_container
        //                     docker rm my_container
        //                 fi
        //             '''
                    
                    
                    
        //             // 'tem'이라는 이미지가 존재하는지 확인하고 삭제
        //             sh '''
        //                 if [ "$(docker images -q tem 2> /dev/null)" != "" ]; then
        //                     docker rmi tem
        //                 fi
        //             '''
        //             sh 'ls -al'
        //             sh 'docker build --no-cache -t tem .'
        //             // 위 명령어까지 진행되면 
                    
                    
        //             // 새로운 컨테이너 생성
        //             sh 'docker run -d --name my_container -p 8000:8000 tem'
                    
                    
        //              }
                
                
        //     }
        // }
        
        
        
        
        
        
        
        
        stage('Nginx Build and Deploy Nginx') {
            steps {
                dir('/var/jenkins_home/workspace/D101-final/'){
                   
                  sh 'docker-compose --version'
                    
                    // react_container 존재하면 멈추고 삭제하기
                    sh '''
                      if [ "$(docker ps -aq -f name=nginx_container)" ]; then
                            docker stop nginx_container
                            docker rm nginx_container
                        fi
                    '''
                    
                    sh '''
                      if [ "$(docker ps -aq -f name=d101-final-nginx-1)" ]; then
                            docker stop d101-final-nginx-1
                            docker rm d101-final-nginx-1
                        fi
                    '''
                    
                    
                    
                    // 'nginx-image'이라는 이미지가 존재하는지 확인하고 삭제
                    sh '''
                        if [ "$(docker images -q nginx-image 2> /dev/null)" != "" ]; then
                            docker rmi nginx-image
                        fi
                    '''
                    
                    // certbot 존재하면 멈추고 삭제하기
                    sh '''
                      if [ "$(docker ps -aq -f name=certbot)" ]; then
                            docker stop certbot
                            docker rm certbot
                        fi
                    '''
                    
                    
                    
                    
               
                    
                    
                    sh 'docker build --no-cache -t nginx-image .'

                    //sh  'docker-compose -f docker-nginx-certbot.yaml up --build'
                    
                    // 위 명령어까지 진행되면 
                    
                    // 컨테이너가 존재하면 삭제
                    
                    
                     // 컨테이너가 존재하면 삭제
                    sh '''
                    if [ "$(docker ps -aq -f name=nginx_container)" ]; then
                        docker rm -f nginx_container
                    fi
                    '''
                    
                    
                     // 컨테이너가 존재하면 삭제
                    sh '''
                    if [ "$(docker ps -aq -f name=certbot)" ]; then
                        docker rm -f certbot
                    fi
                    '''
                    // // 새로운 컨테이너 생성
                    // sh 'docker run -d --name nginx_container -p 80:80 -p 443:443 -v ./FE/kiosk/public/static:/usr/share/nginx/html/static nginx-image'
                    //sh 'docker run -d --name nginx_container -p 80:80 -p 443:443 -v $(pwd)/FE/kiosk/public:/usr/share/nginx/html nginx-image'
                    sh '''docker run -d --name nginx_container -p 80:80 \
    -v $(pwd)/FE/kiosk/:/usr/share/nginx/html/kiosk \
    -v $(pwd)/FE/web/:/usr/share/nginx/html/web \
    nginx-image'''
                    // // 새로운 컨테이너 생성
                    // sh '''
                    //  docker run -d --name certbot -v ./data/certbot/conf:/etc/letsencrypt -v ./data/certbot/www:/var/www/certbot my-certbot
                    // '''
                     }
                
                
            }
        }
        
        
       
        
        
        
        
        

    }
}