jenkinsfile小试牛刀

本文主要演示一下如何用jenkinsfile来编译java服务

安装jenkins

这里使用docker来安装jenkins

复制代码
docker run --name jenkins-docker \
--volume $HOME/jenkins_home:/var/jenkins_home \
-p 8080:8080 jenkins/jenkins:2.416

之后访问http://${yourip}:8080,然后输入admin密码继续安装

jenkinsfile

复制代码
pipeline {
    agent any

    tools {
        // Install the Maven version configured as "M3" and add it to the path.
        maven "M3"
    }

    stages {
        stage('Build') {
            steps {
                // Get some code from a GitHub repository
                git 'https://github.com/jglick/simple-maven-project-with-tests.git'

                // Run Maven on a Unix agent.
                sh "mvn -Dmaven.test.failure.ignore=true clean package"

                // To run Maven on a Windows agent, use
                // bat "mvn -Dmaven.test.failure.ignore=true clean package"
            }

            post {
                // If Maven was able to run the tests, even if some of the test
                // failed, record the test results and archive the jar file.
                success {
                    junit '**/target/surefire-reports/TEST-*.xml'
                    archiveArtifacts 'target/*.jar'
                }
            }
        }
    }
}

这里定义了Build,它先是git拉取代码,然后执行mvn进行编译,最后在post部分在状态为success时使用junit显示测试报告,并归档jar包

小结

jenkins2.x提供了pipeline as code的功能,可以通过jenkinsfile(类似gitlab ci yaml),使用dsl来定义流水线,这个相比于在界面上配置更为易用和清晰,也容易进行版本化管理。

doc

相关推荐
闲人编程17 小时前
Elasticsearch搜索引擎集成指南
python·elasticsearch·搜索引擎·jenkins·索引·副本·分片
人间打气筒(Ada)2 天前
jenkins基于Pipeline发布项目
java·pipeline·jenkins·流水线·ci·cd·cicd
狂野小青年3 天前
Jenkins如何添加全局凭证
运维·jenkins
only_Klein3 天前
jenkins流水线报错:Connection reset by peer
ci/cd·kubernetes·gitlab·jenkins·ssl
野猪佩挤4 天前
Jenkins动态Salve调度其他集群Docker in Docker
运维·docker·jenkins
软件派4 天前
Elasticsearch终极教程:从基础到进阶的技术指南
运维·jenkins
tianyuanwo4 天前
Jenkins节点编码环境深度解析:从配置到Java Web连接原理
java·jenkins·语言编码
小韩加油呀4 天前
jenkins声明式pipline和shell从环境变量配置到打包构建再到发布到k8s
运维·kubernetes·jenkins
看世界的小gui4 天前
Jenkins通过CAS接入Maxkey单点登陆
运维·jenkins
倔强菜鸟5 天前
2026.2.2--Jenkins的基本使用
java·运维·jenkins