什么是 CI/CD 管道?
CI/CD 管道是为了交付新版本的软件而必须执行的一系列步骤。CI/CD 管道引入了监控和自动化来改进应用程序开发过程,特别是在集成和测试阶段,如以及在交付和部署期间。尽管可以手动执行 CI/CD 管道的每个步骤,但 CI/CD 管道的真正价值是通过自动化实现的。
CI/CD 管道的元素
- Build — 编译应用程序的阶段。
- 测试——测试代码的阶段。这里的自动化可以节省时间和精力。
- 发布——应用程序交付到存储库的阶段。
- 部署——在这个阶段,代码被部署到生产环境中。
- 验证和合规性——验证构建的步骤取决于组织的需求。图像安全扫描工具,如 Clair,可以通过将图像与已知漏洞 (CVE) 进行比较来确保图像质量。
Tools
Overview
这就是我们将在本文中创建的内容。
我们用什么?
- Ec2 Ubuntu
- Java, Jenkins, Maven
- Tomcat
- Docker
设置
1:准备2台Ubuntu服务器。 为“詹金斯”命名一个。 一个为“Tomcat” 推荐超过 t2.small 实例类型。
2:确保您可以 ssh 进入两台服务器。
设置 Jenkins 服务器
1:更新
sudo apt-get update -y
2:安装Java(Java运行环境)
sudo apt search openjdk
3:安装JDK
sudo apt-get install default-jdk -y
4:检查你的版本
ubuntu@jenkins:~$ javac -version javac 11.0.13ubuntu@jenkins:~$ java -version openjdk version "11.0.13" 2021-10-19 OpenJDK Runtime Environment (build 11.0.13+8-Ubuntu-0ubuntu1.20.04) OpenJDK 64-Bit Server VM (build 11.0.13+8-Ubuntu-0ubuntu1.20.04, mixed mode, sharing)
5:添加 Jenkins 存储库
curl -fsSL https://pkg.jenkins.io/debian/jenkins.io.key | sudo tee \ /usr/share/keyrings/jenkins-keyring.asc > /dev/null echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \ https://pkg.jenkins.io/debian binary/ | sudo tee \ /etc/apt/sources.list.d/jenkins.list > /dev/null
6:安装詹金斯
sudo apt update sudo apt-get install jenkins
7:确认它正在运行。
ubuntu@jenkins:~$ sudo systemctl status jenkins ● jenkins.service - LSB: Start Jenkins at boot time Loaded: loaded (/etc/init.d/jenkins; generated) Active: active (exited) since Fri 2021-12-31 23:28:46 UTC; 57s ago Docs: man:systemd-sysv-generator(8) Tasks: 0 (limit: 2355) Memory: 0B CGroup: /system.slice/jenkins.serviceDec 31 23:28:45 jenkins systemd[1]: Starting LSB: Start Jenkins at boot time... Dec 31 23:28:45 jenkins jenkins[4565]: Correct java version found Dec 31 23:28:45 jenkins jenkins[4565]: * Starting Jenkins Automation Server jenkins Dec 31 23:28:45 jenkins su[4599]: (to jenkins) root on none Dec 31 23:28:45 jenkins su[4599]: pam_unix(su-l:session): session opened for user jenkins by (uid=0) Dec 31 23:28:45 jenkins su[4599]: pam_unix(su-l:session): session closed for user jenkins Dec 31 23:28:46 jenkins jenkins[4565]: ...done. Dec 31 23:28:46 jenkins systemd[1]: Started LSB: Start Jenkins at boot time.
还要检查 IP + 端口 8080 以查看控制台。
请输入此命令检查密码。
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
8:安装推荐的插件
插件安装完成后,进入控制台,选择“manage jenkins”
完成之后,
9:安装Docker
更新 apt 包索引并安装包以允许 apt 通过 HTTPS 使用存储库:
sudo apt-get update sudo apt-get install \ ca-certificates \ curl \ gnupg \ lsb-release
添加 Docker 的官方 GPG 密钥:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
使用以下命令设置稳定存储库。要添加 nightly 或 test 存储库,请在以下命令中的单词 stable 之后添加单词 nightly 或 test(或两者)。
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
安装 Docker 引擎
更新apt包索引,安装最新版本的Docker Engine和containerd,或者下一步安装具体版本:
sudo apt-get update sudo apt-get install docker-ce docker-ce-cli containerd.io
列出您的存储库中可用的版本:
apt-cache madison docker-ce
使用第二列中的版本字符串安装特定版本,例如,
sudo apt-get install docker-ce=5:20.10.12~3-0~ubuntu-focal docker-ce-cli=5:20.10.12~3-0~ubuntu-focal containerd.io
通过运行 hello-world 映像来验证 Docker 引擎是否已正确安装。
sudo docker run hello-world
Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 2db29710123e: Pull complete Digest: sha256:2498fce14358aa50ead0cc6c19990fc6ff866ce72aeb5546e1d59caac3d0d60f Status: Downloaded newer image for hello-world:latestHello from Docker! This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (amd64) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal.To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bashShare images, automate workflows, and more with a free Docker ID: https://hub.docker.com/For more examples and ideas, visit: https://docs.docker.com/get-started/
检查状态
ubuntu@jenkins:~$ sudo systemctl status docker ● docker.service - Docker Application Container Engine Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled) Active: active (running) since Fri 2021-12-31 23:40:27 UTC; 4min 55s ago TriggeredBy: ● docker.socket Docs: https://docs.docker.com Main PID: 6261 (dockerd) Tasks: 9 Memory: 34.4M CGroup: /system.slice/docker.service └─6261 /usr/bin/docke
让我们将我们的用户名添加到 docker 组
sudo usermod -aG docker jenkins
安装 Maven
sudo apt updatesudo apt install mavenubuntu@jenkins:~$ mvn -version Apache Maven 3.6.3 Maven home: /usr/share/maven Java version: 11.0.13, vendor: Ubuntu, runtime: /usr/lib/jvm/java-11-openjdk-amd64 Default locale: en, platform encoding: UTF-8 OS name: "linux", version: "5.11.0-1022-aws", arch: "amd64", family: "unix"
设置 Tomcat 服务器
1:安装java(请看前面的步骤)
ubuntu@tomcat:~$ java -version
openjdk version "11.0.13" 2021-10-19
OpenJDK Runtime Environment (build 11.0.13+8-Ubuntu-0ubuntu1.18.04)
OpenJDK 64-Bit Server VM (build 11.0.13+8-Ubuntu-0ubuntu1.18.04, mixed mode, sharing)
2:安装tomcat
mkdir /prod cd /prod sudo wget https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.56/bin/apache-tomcat-9.0.56.zip sudo apt install unzipsudo unzip apache-tomcat-9.0.56.zip
3:启动tomcat
cd apache-tomcat-9.0.56/bin chmod +x catalina.sh ubuntu@tomcat:/prod/apache-tomcat-9.0.56/bin$ sudo bash startup.sh Using CATALINA_BASE: /prod/apache-tomcat-9.0.56 Using CATALINA_HOME: /prod/apache-tomcat-9.0.56 Using CATALINA_TMPDIR: /prod/apache-tomcat-9.0.56/temp Using JRE_HOME: /usr Using CLASSPATH: /prod/apache-tomcat-9.0.56/bin/bootstrap.jar: /prod/apache-tomcat-9.0.56/bin/tomcat-juli.jar Using CATALINA_OPTS: Tomcat started.
我们可以看到它正在运行。
4:配置设置(对于tomcat 9)
sudo vi /prod/apache-tomcat-9.0.56/webapps/manager/META-INF/context.xml
请注释掉
添加 <role rolename="manager-gui"/>
添加 <user username="tomcat" password="<yourpassword>" roles="manager-gui"/>
另外,我们需要添加用户名
cd ../bin/ ubuntu@tomcat:/prod/apache-tomcat-9.0.56/bin$ sudo bash shutdown.sh ubuntu@tomcat:/prod/apache-tomcat-9.0.56/bin$ sudo bash startup.sh
在 Jenkins 中创建构建管道
创建一个新项目(管道)
为您的新项目提供一个名称(例如 Pipeline webapp)并选择 Multibranch Pipeline
单击添加源按钮,选择要使用的存储库类型并填写详细信息。
输入您的 GitHub 存储库地址,然后单击验证。
https://github.com/meibutuoyaji/webapp.git
如果没有凭据,则显示正常,然后单击保存。
保存后,您可以在控制台上看到它。
转到您在前面步骤中放置的 GitHub 存储库并在那里创建新文件。
制作一个名为“jenkinsfile”的文件和里面
pipeline { agent { docker { image 'maven:3.8.4-openjdk-11-slim' } } stages { stage('build') { steps { sh 'mvn --version' } } } }
如果这篇文章对您有帮助,请点击下方的鼓掌👏按钮数次以表示您对作者的支持👇
原文:https://faun.pub/devops-create-your-first-ci-cd-pipeline-ed054ba1404f
最新内容
- 1 week 1 day ago
- 1 week 1 day ago
- 1 week 1 day ago
- 1 week 1 day ago
- 1 week 1 day ago
- 2 weeks ago
- 2 weeks 1 day ago
- 2 weeks 3 days ago
- 2 weeks 3 days ago
- 2 weeks 3 days ago