Skip to content

Blog Công Nghệ

MENUMENU
  • Trang chủ
  • Giới Thiệu
  • Lập Trình
    • Lập Trình Website
      • Laravel
        • Phân Tích Dự Án
      • PHP
      • SQL
      • HTML
      • CSS
      • Javascipt
      • My Project
      • Wordpress
    • Luyện Skill
    • Lập trình winform
    • CSDL
    • Lập Trình Android
    • Trí tuệ nhân tạo
    • Khai Khoáng Dữ Liệu
    • Arduino
    • Khác
    • Đồ án
  • Phần Mềm
    • Powerpoint
    • Tool
  • Cuộc sống và Giải trí
    • Hợp âm
    • web5ngay - youtube
    • Công Giáo
    • Kỹ Năng Sống
    • Street Workout
  • Danh sách bài viết
  • Guide line
    • Guild line phỏng vấn
    • Guide lines Laravel
    • Guide line Module Frontend
  • Tóm tắt sách
  • Fanpage

Blog Công Nghệ

Nơi chia sẻ kiến thức

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

14 Tháng Một, 2022 by admin
Lượt xem: 17

Contents

  • 1. Cài đặt Jenkins
    • 1.1. Aws EC2
      • 1.1.1 Cài đặt EC2 server
      • 1.1.2 Change quyền root
      • 1.1.3 Cài đặt java
      • 1.1.4 Cài jenkins
      • 1.1.5 Start jenkins
      • 1.1.6 Check jenkins đã chạy chưa bằng telnet
      • 1.1.7 Setting jenkins
  • 2. Jenkins với github

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

Related posts:

  1. Những công cụ hữu ích cho quá trình học tập của một lập trình viên
  2. Xoắn não với lập trình viên
  3. Lệnh cơ bản của Git, đơn giản, hiệu quả – điểm cộng cho các lập trình viên Việt Nam
  4. Thiết kế giao diện quản trị admin nhanh với Laravel và thư viện Infyom

Post navigation

Previous Post:

Kinh nghiệm Safari

Next Post:

Kinh nghiệm Linux

Trả lời Hủy

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *

Ẩn sidebar

Tìm kiếm

Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages

Blog Công Nghệ

Bài viết mới

  • Master typescript
  • Sendmail trong Laravel sử dụng dịch vụ SES, SQS của Amazon
  • Install SSL in Nginx Ubuntu
  • Docker study
  • Bảo vệ: Hướng dẫn code bot Telegram easy game

Lượng truy cập

0074611
Visit Today : 207
Visit Yesterday : 178
This Month : 882
Who's Online : 3
© 2025 Blog Công Nghệ | WordPress Theme by Superbthemes