Shawn's Blog
蒸汽兔
Jenkins相关笔记
字数:652 字
阅读时长:约 3 分钟
阅读次
安装
使用docker 安装
# 拉取jenkins镜像
[ashsh@pro-data-sh002 ~]$ docker pull jenkinsci/blueocean
# 运行镜像
[ashsh@pro-data-sh002 jenkins]$ docker run \
-u root \
--rm \
-d \
-p 9089:8080 \
-p 50000:50000 \
-v /mnt/app/server/jenkins/data:/var/jenkins_home \
-v /mnt/app/server/maven/apache-maven-3.1.1:/mnt/app/server/maven/apache-maven-3.1.1 \
-v /usr/java/jdk1.8.0_181-cloudera:/usr/java/jdk1.8.0_181-cloudera \
-v /var/run/docker.sock:/var/run/docker.sock \
-v $(which docker):/usr/bin/docker \
jenkinsci/blueocean
安装后引导
第一次访问新的Jenkins实例时,系统会要求您使用自动生成的密码对其进行解锁。
- 浏览到
http://localhost:8080
(或安装时为Jenkins配置的任何端口),并等待 解锁 Jenkins 页面出现。
- 从Jenkins控制台日志输出中,复制自动生成的字母数字密码(在两组星号之间)
[ashsh@pro-data-sh002 jenkins]$ docker logs -f 2cc3b6acc7d7
Running from: /usr/share/jenkins/jenkins.war
webroot: EnvVars.masterEnvVars.get("JENKINS_HOME")
2021-08-23 07:09:14.396+0000 [id=1] INFO org.eclipse.jetty.util.log.Log#initialized: Logging initialized @371ms to org.eclipse.jetty.util.log.JavaUtilLog
......
2021-08-23 07:09:27.292+0000 [id=60] INFO jenkins.install.SetupWizard#init:
*************************************************************
*************************************************************
*************************************************************
Jenkins initial setup is required. An admin user has been created and a password generated.
Please use the following password to proceed to installation:
# 密码 -> `ea4d9c2d19e642f589df0b8279eb1d24` <- # 密码
This may also be found at: /var/jenkins_home/secrets/initialAdminPassword
*************************************************************
*************************************************************
*************************************************************
2021-08-23 07:09:41.546+0000 [id=60] INFO jenkins.InitReactorRunner$1#onAttained: Completed initialization
2021-08-23 07:09:41.569+0000 [id=30] INFO hudson.WebAppMain$3#run: Jenkins is fully up and running
Maven项目接入Jenkins
pom.xml
pom文件中添加maven docker的打包插件
<!-- Docker maven plugin -->
<build>
<plugins>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.0.0</version>
<configuration>
<imageName>${project.artifactId}</imageName>
<dockerDirectory>src/main/docker</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>1.4.13</version>
<configuration>
<repository>${project.artifactId}</repository>
</configuration>
</plugin>
</plugins>
</build>
dockerfile
供maven 插件编译docker镜像使用
FROM java:openjdk-8-jre-alpine
# 定义环境变量
ENV APP_NAME foreign-api-1.0-SNAPSHOT # 项目名
ENV BUILD_ENV ashsh_dev # 启动环境
ENV LOCAL_PATH /opt/${APP_NAME} # 镜像工作目录
# 制作镜像运行命令
RUN mkdir -p ${LOCAL_PATH} /opt/logs/foreign-api/test # 创建所需的目录
RUN apk --no-cache add curl # 添加curl 工具, 健康检查用(openjdk-8 镜像默认不带curl)
RUN echo 'Asia/Shanghai' >/etc/timezone # 修改镜像时区
ADD target/${APP_NAME}.jar ${LOCAL_PATH} # 启动jar文件
# 设置工作目录
WORKDIR ${LOCAL_PATH}
# 启动命令
CMD java -Xms800m -Xmx1662m -XX:PermSize=256m -XX:MaxPermSize=1024m -XX:MaxNewSize=1024m -jar ${APP_NAME}.jar --spring.cloud.nacos.config.namespace=${BUILD_ENV}
jenkinsfile
定义 jenkins 脚本执行流程
def project_url = "http://2587984bx6.zicp.vip/java-export/shenshen-export.git"
def project_name = "foreign-api"
def branch_name = "*/dev"
def gitlab_auth = "71e9f234-d159-4ac6-ab42-d031fb02d0a9"
def tag = "latest"
def harbor_address = "172.19.166.202/library"
def image_name = "${harbor_address}/${project_name}:${tag}"
def webhook_url = "http://139.196.179.174:9000/api/webhooks/dc3ac6a1-3c6c-4d39-af3c-be9a082a9651"
node {
def mvnHome
stage('拉取代码') { // for display purposes
checkout(
[
$class: 'GitSCM',
branches: [[name: "${branch_name}"]],
extensions: [],
userRemoteConfigs: [
[
credentialsId: "${gitlab_auth}",
url: "${project_url}"
]
]
]
)
}
stage('构建代码') {
sh "mvn clean package -DskipTests dockerfile:build"
sh "docker tag ${project_name} ${image_name}"
}
stage('推送镜像') {
sh "docker login -u admin -p Harbor12345 172.19.166.202"
sh "docker push ${image_name}"
sh "curl -X POST ${webhook_url}"
}
}