AdSense

網頁

2024/3/25

AWS EC2 Linux AMI 安裝Jenkins

AWS EC2 Amazon Linux 2023 AMI安裝Jenkins。


事前要求

參考「AWS EC2 建立instance」建立EC2執行個體。硬碟空間要設大一點,測試就用20GB,避免磁碟空間不足導致Jenkins pipeline無法執行。


設定Security Group(Firewall)

建立EC2 instance時有設定security group,也就是防火牆規則。要新增以下傳入規則(inbound rules)以便:

  1. 本機可以SSH連線進instance。
  2. 可以用瀏覽器開啟Jenkins的管理介面。

開啟AWS console前往[Security Groups]頁面,點選instance使用的security group名稱進入,在下方的[Inbound rules]頁籤點選[Edit inbound rules]。

在[Edit inbound rules]頁面,點選[Add rule]按鈕新增規則如下:

Type Protocol Port range Source
SSH TCP 22 My IP [YOUR_IP]
Custom TCP TCP 8080 Anywhere-IPv4 0.0.0.0/0

設定完後按右下角的[Save rules]按鈕確定。

新增後的inbound rules如下:




安裝Jenkins

參考官方教學,過程如下。

本機SSH進入EC2 instance的bash。

~% ssh -i ~/Documents/secret/ec2-demo.pem  ec2-user@43.207.220.173
The authenticity of host '43.207.220.173 (43.207.220.173)' can't be established.
ED25519 key fingerprint is SHA256:l46nQajU6NiPFpJ+t9kct70LWxWxWrjwME5Y0pTp5TQ.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '43.207.220.173' (ED25519) to the list of known hosts.
   ,     #_
   ~\_  ####_        Amazon Linux 2023
  ~~  \_#####\
  ~~     \###|
  ~~       \#/ ___   https://aws.amazon.com/linux/amazon-linux-2023
   ~~       V~' '->
    ~~~         /
      ~~._.   _/
         _/ _/
       _/m/'
[ec2-user@ip-172-31-3-166 ~]$

更新套件管理員,輸入sudo yum update –y

[ec2-user@ip-172-31-3-166 ~]$ sudo yum update –y

設定Jenkins repo,輸入sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo

[ec2-user@ip-172-31-3-166 ~]$ sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
--2024-03-26 05:06:32--  https://pkg.jenkins.io/redhat-stable/jenkins.repo
Resolving pkg.jenkins.io (pkg.jenkins.io)... 151.101.110.133, 2a04:4e42:1a::645
Connecting to pkg.jenkins.io (pkg.jenkins.io)|151.101.110.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 85
Saving to: ‘/etc/yum.repos.d/jenkins.repo’

/etc/yum.repos.d/jenkins.repo                                       100%[===================================================================================================================================================================>]      85  --.-KB/s    in 0s

2024-03-26 05:06:32 (5.13 MB/s) - ‘/etc/yum.repos.d/jenkins.repo’ saved [85/85]

匯入Jenkins的key file啟用package安裝,輸入sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io-2023.key

[ec2-user@ip-172-31-3-166 ~]$ sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io-2023.key

升級package,輸入sudo yum upgrade

[ec2-user@ip-172-31-3-166 ~]$ sudo yum upgrade

安裝Java,為Jenkins的運行環境,輸入sudo dnf install java-17-amazon-corretto -y

[ec2-user@ip-172-31-3-166 ~]$ sudo dnf install java-17-amazon-corretto -y

安裝Jenkins,輸入sudo yum install jenkins -y

[ec2-user@ip-172-31-3-166 ~]$ sudo yum install jenkins -y
Last metadata expiration check: 0:01:37 ago on Tue Mar 26 05:07:50 2024.
Dependencies resolved.
================================================================================================================================================================================================================================================================================
 Package                                                          Architecture                                                    Version                                                                Repository                                                        Size
================================================================================================================================================================================================================================================================================
Installing:
 jenkins                                                          noarch                                                          2.440.2-1.1                                                            jenkins                                                           82 M

Transaction Summary
================================================================================================================================================================================================================================================================================
Install  1 Package

Total size: 82 M
Installed size: 83 M
Downloading Packages:
[SKIPPED] jenkins-2.440.2-1.1.noarch.rpm: Already downloaded
...
Installed:
  jenkins-2.440.2-1.1.noarch

Complete!

這邊安裝的Jenkins版本為2.440.2。


讓系統重啟時啟動Jenkins,輸入sudo systemctl enable jenkins

[ec2-user@ip-172-31-3-166 ~]$ sudo systemctl enable jenkins

啟動Jenkins,輸入sudo systemctl start jenkins

[ec2-user@ip-172-31-3-166 ~]$ sudo systemctl start jenkins

檢視Jenkins狀態,輸入sudo systemctl status jenkins

[ec2-user@ip-172-31-3-166 ~]$ sudo systemctl status jenkins
 jenkins.service - Jenkins Continuous Integration Server
     Loaded: loaded (/usr/lib/systemd/system/jenkins.service; enabled; preset: disabled)
     Active: active (running) since Tue 2024-03-26 05:12:41 UTC; 57min ago
   Main PID: 26777 (java)
      Tasks: 41 (limit: 1114)
     Memory: 554.9M
        CPU: 1min 26.363s
     CGroup: /system.slice/jenkins.service
             └─26777 /usr/bin/java -Djava.awt.headless=true -jar /usr/share/java/jenkins.war --webroot=/var/cache/jenkins/war --httpPort=8080


登入Jenkins

在瀏覽器輸入http://[PUBPLIC_IP]:8080進入Jenkins。[PUBLIC_IP]為instance的公開IP。例如這邊為http://43.207.220.173:8080

第一次登入預設密碼放在/var/lib/jenkins/secrets/initialAdminPassword,輸入sudo cat /var/lib/jenkins/secrets/initialAdminPassword檢視。

[ec2-user@ip-172-31-3-166 ~]$ sudo cat /var/lib/jenkins/secrets/initialAdminPassword
7e3b2760a8f14a719a37f0f208e0d92c



點選[Install suggested plugins]安裝建議插件。



安裝插件中。



建立第一個管理員帳號。



更改URL位址,無特別需要就直接按[Save and Finish]按鈕。



完成基本設定,點選[Start using Jenkins]按鈕。



Jenkins首頁。




沒有留言:

AdSense