在當今快節奏的開發環境中,持續整合和持續交付 (CI/CD) 是快速可靠地交付高品質軟體的基本實踐。在此專案中,我們將利用 Jenkins、Docker、Trivy、SonarQube 和 Nexus 等業界標準工具,從頭開始建立強大的 CI/CD 管道。本指南專為希望以可擴展且安全的方式自動化建置、測試和部署應用程式的流程的開發人員和 DevOps 愛好者而設計。
我們將首先在 AWS 上設定必要的基礎設施,然後安裝和設定 Docker、Jenkins、Trivy、Nexus 和 SonarQube。最後,我們將建立一個 Jenkins 管道,自動執行整個 CI/CD 流程,確保您的應用程式持續建置、掃描漏洞、分析程式碼品質並以最少的手動介入進行部署。
讓我們深入研究並利用這個強大的 CI/CD 設定來轉變您的軟體交付流程。
- Go to [AWS Management Console](https://aws.amazon.com/console/).
- Sign in with your AWS account credentials.
- Type "EC2" in the search bar or select "Services" > "EC2" under the "Compute" section.
- Click "Instances" in the EC2 dashboard sidebar.
- Click the "Launch Instance" button.
- Select "Ubuntu" from the list of available AMIs.
- Choose "Ubuntu Server 24.04 LTS".
- Click "Select".
- Select an instance type (e.g., t2.micro for testing).
- Click "Next: Configure Instance Details".
- Configure optional settings or leave them as default.
- Click "Next: Add Storage".
- Specify the root volume size (default is usually fine).
- Click "Next: Add Tags".
- Optionally, add tags for better organization.
- Click "Next: Configure Security Group".
- Allow SSH access (port 22) from your IP address.
- Optionally, allow other ports (e.g., HTTP port 80, HTTPS port 443).
- Click "Review and Launch".
- Review the instance configuration.
- Click "Launch".
- Select an existing key pair or create a new one.
- Check the acknowledgment box.
- Click "Launch Instances".
- Use an SSH client like MobaXterm:
- Open MobaXterm and click "Session" > "SSH".
- Enter the public IP address of your instance.
- Select "Specify username" and enter "ubuntu".
- Under "Advanced SSH settings", select "Use private key" and browse to your key pair file (.pem).
- Click "OK" to connect.
逐步安裝:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io -y
sudo chmod 666 /var/run/docker.sock
透過執行這些步驟,您應該已經在 Ubuntu 系統上成功安裝了 Docker。現在您可以開始使用 Docker 來容器化和管理您的應用程式。
逐步安裝:
sudo apt-get update
sudo apt-get upgrade -y
sudo apt install -y fontconfig openjdk-17-jre
sudo wget -O /usr/share/keyrings/jenkins-keyring.asc https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key
echo "deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] https://pkg.jenkins.io/debian-stable binary/" | sudo tee /etc/apt/sources.list.d/jenkins.list > /dev/null
sudo apt-get update
sudo apt-get install -y jenkins
sudo systemctl start jenkins
sudo systemctl enable jenkins
- Open a web browser and go to [http://your_server_ip_or_domain:8080](http://your_server_ip_or_domain:8080/).
- You will see a page asking for the initial admin password. Retrieve it using:
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
- Enter the password, install suggested plugins, and create your first admin user.
逐步安裝:
sudo apt-get install wget apt-transport-https gnupg lsb-release
wget -qO - https://aquasecurity.github.io/trivy-repo/deb/public.key | sudo apt-key add -
echo deb https://aquasecurity.github.io/trivy-repo/deb $(lsb_release -sc) main | sudo tee -a /etc/apt/sources.list.d/trivy.list
sudo apt-get update
sudo apt-get install trivy
逐步安裝:
sudo docker pull sonatype/nexus3
sudo docker run -d -p 8081:8081 --name nexus -v nexus-data:/nexus-data sonatype/nexus3
- Open a web browser and go to [http://your_server_ip_or_domain:8081](http://your_server_ip_or_domain:8081/).
- The default username is `admin`. Retrieve the initial admin password from the log:
sudo docker logs nexus 2>&1 | grep -i password
- Complete the setup wizard.
逐步安裝:
sudo docker network create sonarnet
sudo docker run -d --name sonarqube_db --network sonarnet -e
POSTGRES_USER=sonar -e POSTGRES_PASSWORD=sonar -e
POSTGRES_DB=sonarqube -v postgresql:/var/lib/postgresql -v
postgresql_data:/var/lib/postgresql/data postgres:latest
sudo docker run -d --name sonarqube --network sonarnet -p 9000:9000 -e sonar.jdbc.url=jdbc:postgresql://sonarqube_db:5432/sonarqube -e sonar.jdbc.username=sonar -e sonar.jdbc.password=sonar -v sonarqube_data:/opt/sonarqube/data -v sonarqube_extensions:/opt/sonarqube/extensions sonarqube:latest
- Open a web browser and go to [http://your_server_ip_or_domain:9000](http://your_server_ip_or_domain:9000/).
- The default login is `admin` with the password `admin`.
- Complete the setup by configuring the SonarQube server.
逐步管道設定:
- In Jenkins, click on "New Item" and select "Pipeline".
- Name the pipeline and click "OK".
- Scroll down to the "Pipeline" section.
- Select "Pipeline script" and define your pipeline stages using Groovy.
pipeline {
agent any
stages {
stage('Clone Repository') {
steps {
git 'https://github.com/your-repo.git'
}
}
stage('Build with Maven') {
steps {
sh 'mvn clean install'
}
}
stage('Docker Build and Push') {
steps {
script {
docker.build("your-app:latest").push("your-docker-repo/your-app:latest")
}
}
}
stage('Security Scan with Trivy') {
steps {
sh 'trivy image your-docker-repo/your-app:latest'
}
}
stage('Quality Analysis with SonarQube') {
steps {
withSonarQubeEnv('SonarQube Server') {
sh 'mvn sonar:sonar'
}
}
}
}
}
- Save the pipeline configuration.
- Click "Build Now" to run the pipeline.
該管道將自動化整個 CI/CD 流程,從克隆儲存庫到建置應用程式、使用 Trivy 掃描漏洞以及使用 SonarQube 分析程式碼品質。
恭喜!您已使用 Jenkins、Docker、Trivy、SonarQube 和 Nexus 成功設定了完整的 CI/CD 管道。該管道不僅可以自動化建置和部署流程,還整合了關鍵的安全和品質檢查,確保安全且有效率地交付您的應用程式。無論您是處理個人專案還是管理大規模生產環境,此設定都為持續整合和交付提供了堅實的基礎,幫助您更快地發佈軟體,而不會影響品質或安全性。
現在您已經掌握了基礎知識,請考慮使用其他階段或工具來擴展此管道,探索更高級的功能,並自訂流程以滿足您的特定需求。部署愉快!
加入我們的Telegram 社群||在 GitHub 上關注我以獲取更多 DevOps 內容!