Monday, October 21, 2024

[Jenkins] Build Docker Image

-

Bài viết này mình sẽ hướng dẫn build 1 image sử dụng Pipeline của Jenkins.

Mình sẽ tạo 1 job mới với type là Pipeline

Hãy vào kho repository copy đường dẫn chứa source code của bạn, kho này có ít nhất 2 file đó là Jenkinsfile và Dockerfile.

– Dockerfile: Dùng để khai báo các bước thực hiện trong 1 job

– Dockerfile: Khai báo các bước để build 1 docker image

Nội dung Dockerfile đơn giản mình chỉ update os và cài đặt Nginx và systemctl sử dụng image gốc là Ubuntu 20.04

FROM ubuntu:20.04
RUN apt update -y
RUN apt install -y nginx systemctl
WORKDIR /etc/nginx

Nội dung Jenkinsfile mình thiết lập 3 bước thực hiện cho job này

Bước 1: Clone code về từ repository

Bước 2: Build image

Bước 3: Thông báo đã build image thành công

pipeline {
    agent any
    stages{
        stage('Git clone'){
            steps{
                git branch: 'main', url: 'https://github.com/hoanghd164/build-docker-image.git'
            }  
        }

        stage('Docker build image'){
            steps{
                sh "docker build -t nginx:ver1 --force-rm -f Dockerfile ."
            }  
        }

        stage('Build complete'){
            steps{
                echo "Docker build complete"
            }  
        }
        
    }
}

Hãy copy url repository này vào Repository URL trong job của Jenkins, hãy nhớ trỏ tên nhánh của bạn ở Branch Specifier (blank for ‘any’)

https://github.com/hoanghd164/build-docker-image.git

Kéo xuống dưới tại Script Path hãy trỏ đường dẫn tới Jenkinsfile của bạn và lưu lại

Sau khi thiêt lập xong job bạn sẽ có 1 job mới như dưới

Hãy vào job này và bấm build now bạn sẽ thấy lần chạy đầu tiên đã thành công, các bước hoàn thành job này được thể hiện tại phần Stage View

Vào console ta sẽ thấy quá logs chi tiết của job

Do log quá dài nên mình sẽ copy nó xuống đây để các bạn tham khảo

Started by user Hà Đăng Hoàng
Obtained Jenkinsfile from git https://github.com/hoanghd164/build-docker-image.git
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/build-docker-image
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Declarative: Checkout SCM)
[Pipeline] checkout
Selected Git installation does not exist. Using Default
The recommended git tool is: NONE
No credentials specified
 > git rev-parse --resolve-git-dir /var/jenkins_home/workspace/build-docker-image/.git # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url https://github.com/hoanghd164/build-docker-image.git # timeout=10
Fetching upstream changes from https://github.com/hoanghd164/build-docker-image.git
 > git --version # timeout=10
 > git --version # 'git version 2.30.2'
 > git fetch --tags --force --progress -- https://github.com/hoanghd164/build-docker-image.git +refs/heads/*:refs/remotes/origin/* # timeout=10
 > git rev-parse refs/remotes/origin/main^{commit} # timeout=10
Checking out Revision 58cfdfdb1e7afdea5f4251671f682d7e373c82ad (refs/remotes/origin/main)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 58cfdfdb1e7afdea5f4251671f682d7e373c82ad # timeout=10
Commit message: "Update Jenkinsfile"
First time build. Skipping changelog.
[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Git clone)
[Pipeline] git
Selected Git installation does not exist. Using Default
The recommended git tool is: NONE
No credentials specified
 > git rev-parse --resolve-git-dir /var/jenkins_home/workspace/build-docker-image/.git # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url https://github.com/hoanghd164/build-docker-image.git # timeout=10
Fetching upstream changes from https://github.com/hoanghd164/build-docker-image.git
 > git --version # timeout=10
 > git --version # 'git version 2.30.2'
 > git fetch --tags --force --progress -- https://github.com/hoanghd164/build-docker-image.git +refs/heads/*:refs/remotes/origin/* # timeout=10
 > git rev-parse refs/remotes/origin/main^{commit} # timeout=10
Checking out Revision 58cfdfdb1e7afdea5f4251671f682d7e373c82ad (refs/remotes/origin/main)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 58cfdfdb1e7afdea5f4251671f682d7e373c82ad # timeout=10
 > git branch -a -v --no-abbrev # timeout=10
 > git branch -D main # timeout=10
 > git checkout -b main 58cfdfdb1e7afdea5f4251671f682d7e373c82ad # timeout=10
Commit message: "Update Jenkinsfile"
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Docker build image)
[Pipeline] sh
+ docker build -t nginx:ver1 --force-rm -f Dockerfile .
Sending build context to Docker daemon  98.82kB

Step 1/4 : FROM ubuntu:20.04
 ---> 680e5dfb52c7
Step 2/4 : RUN apt update -y
 ---> Using cache
 ---> 8ed6a46a319a
Step 3/4 : RUN apt install -y nginx systemctl
 ---> Using cache
 ---> aa58764cd983
Step 4/4 : WORKDIR /etc/nginx
 ---> Using cache
 ---> 4dd7fd31d424
Successfully built 4dd7fd31d424
Successfully tagged nginx:ver1
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Build complete)
[Pipeline] echo
Docker build complete
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

Chúc các bạn thành công

LEAVE A REPLY

Please enter your comment!
Please enter your name here

4,956FansLike
256FollowersFollow
223SubscribersSubscribe
spot_img

Related Stories