Thumbnail
Category: Server

Jenkins - Để làm một lập trình viên cao cấp

Date: January 13, 2022
19 views

1. Cài đặt Jenkins

1.1. Aws EC2

1.1.1 Cài đặt EC2 server

Sau khi cài đặt, EC2 ở trạng thái running. Click phải chuột chọn connect để lấy thông tin connect SSH, và kết nối vào server.

1.1.2 Change quyền root


sudo su -

1.1.3 Cài đặt java


$ yum install -y java-1.8.0-openjdk-devel.x86_64
​
// check lại
$ alternatives --config java
$ java -version

1.1.4 Cài jenkins


$ wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat/jenkins.repo
$ rpm --import https://pkg.jenkins.io/redhat/jenkins.io.key
$ yum install -y jenkins
​
// cài lệnh trên lỗi thì sử dụng dưới
sudo amazon-linux-extras install epel -y 
sudo yum update -y 
sudo yum install jenkins java-1.8.0-openjdk-devel 

1.1.5 Start jenkins


systemctl start jenkins
  
// check sau khi start
service jenkins status

1.1.6 Check jenkins đã chạy chưa bằng telnet


yum install telnet
​
telnet localhost 8080

1.1.7 Setting jenkins

Ec2 bạn mở port security inbound 8080 nhé.

Sau đó vào ip:8080 hoặc domain:8080 để cấu hình jenkins.

Bạn vào server lấy mật khẩu như đường dẫn trên màn hình hiển thị.


Chọn install suggest plugin.



Đặt tài khoản mật khẩu


Next tiếp các bước tiếp theo. Ra giao diện này là ok.

2. Jenkins với github

  1. Đã có jenkins
  2. Tạo service dự án, chứa code
  3. Kết nối vào server và change quyền root


sudo su -

4. Cài webserver


#!/bin/bash
yum update -y
yum install httpd -y
yum install git -y
amazon-linux-extras install epel
yum install epel-release
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
yum install -y php70 php70-php php70-php-fpm php70-php-pecl-memcached php70-php-mysqlnd php70-php-xml
ln -s /usr/bin/php70 /usr/bin/php
service httpd start
chkconfig httpd on

 5. Tạo user


useradd www-user

6. Tạo thư mục và change quyền


cd /var/www/html/
mkdir tinhocthatladongian
chown -Rf www-user:apache tinhocthatladongian

7. Cấu hình cho apache


vi /etc/httpd/conf/httpd.conf 
 
// Tìm Listen và thêm
Listen 81
  
// Đến cuối file add
NameVirtualHost *:81
<VirtualHost *:81>
    DocumentRoot /var/www/html/tinhocthatladongian/
    <Directory "/var/www/html/tinhocthatladongian">
      Order deny,allow
      Allow from all
      AllowOverride All
      Require all granted
   </Directory>
</VirtualHost>

8. Restart apache


service httpd restart

9. Chuyển sang account www-user


su - www-user

10. Tạo SSH Key và change quyền 600


$ ssh-keygen -t rsa
$ cd /home/www-user/.ssh/  
  // => Đường dẫn lấy từ key tạo ra hiển thị trên màn hình
$ mv id_rsa.pub authorized_keys
  
chmod 600 authorized_keys

11. cd đến thư mục dự án và clone code


cd /var/www/html/
cd tinhocthatladongian/
git clone https://github.com/tronghao/learn-jenkins.git .
            // => dấu . để clone vào thư mục này luôn không tạo foler mới

12. Lấy private-key server

Nẳm ở file id_rsa của folder /home/www-user/.ssh/


cd /home/www-user/.ssh/
vi id_rsa

13. Server cài jenkins

  • Tạo folder mới => tương ứng tạo user vì là con của folder home
  • Tạo file pem để lưu private-key
  • Paste private-key lúc nãy vào
  • Change quyền cho file và folder


cd /home/
mkdir jenkins
cd jenkins
vi web-server.pem
chmod 400 web-server.pem
  
// ra trước thư mục cha chứa nó để chown. 
                //Mình từng không cd ra ngoài và mất cả đêm để debug
cd ..
chown -Rf jenkins:root jenkins/

14. Trên server jenkins – chuyển sang user mới (tên-foler lúc nãy tạo)


vi /etc/passwd
//Sửa thành bash như này: 
  jenkins:x:996:994:Jenkins Automation Server:/var/lib/jenkins:/bin/bash
    
su - jenkins

15. Thử jenkins truy cập được máy server chưa


ssh tenUser@[Web Server Address] -i duongdanLuuPrivateKeyTrenJenkins
​
// ví dụ:
  ssh www-user@ec2-3-94-171-240.compute-1.amazonaws.com -i /home/jenkins/web-server.pem 

16. add webhook github

Với đường dẫn dạng:

http://[Jenkin URL]:8080/github-webhook/ 


Ví dụ:
http://ec2-3-95-30-87.compute-1.amazonaws.com:8080/github-webhook/

Cách add webhook github:

  • Vào repository cần add
  • Setting => webhook
  • Nhập url
  • Nhập secret => Nhập để người khác không hack được webhook
  • Add

17. Cài git cho server jenkins


yum install git -y

18. Tạo jenkins item

  • New Item
  • Đặt tên và chọn Freestyle project => OK
  • Cấu hình git
  • + Pass link clone vào
  • + Add credential: Thêm username và password github
  • Chọn branch mà server giữ code
  • Tick chọn “GitHub hook trigger for GITScm polling” => sinh ra webhook đã cấu hình lúc nãy.
  • Mục Build:
  • + chọn excute shell
  • + pass code build vào


#!/bin/bash
su - jenkins
ssh -T -i /home/jenkins/web-key.pem www-user@[Web Server Address] << EOF
cd /var/www/html/tinhocthatladongian
git pull
EOF
  • Nhấn save

19. Build thử

Nhấn Build Now ở jenkins

20. Test lại auto build bằng commit push


Copyright © 2025 All Right Reserved