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

相关推荐
JavaAlpha1 小时前
Jenkins 配置信息导出 的详细说明(中英对照)
运维·servlet·jenkins
遇见火星1 天前
Jenkins 配置gitlab的 pipeline流水线任务
servlet·gitlab·jenkins
代码写到35岁2 天前
Jenkins自动发布C# EXE执行程序
运维·c#·jenkins
程序员阿斌哥2 天前
记录一次jenkins slave因为本地安装多个java版本导致的问题
java·jenkins
LI JS@你猜啊3 天前
liunx版本的
运维·jenkins
水银嘻嘻3 天前
Jenkins持续集成CI,持续部署CD,Allure报告集成以及发送电子 邮件
ci/cd·gitee·jenkins
网安INF4 天前
CVE-2024-23897源码分析与漏洞复现(Jenkins 任意文件读取)
java·web安全·网络安全·jenkins·漏洞
测试开发Kevin5 天前
详解Jenkins Pipeline 中git 命令的使用方法
运维·jenkins
leblancAndSherry5 天前
Gitlab + Jenkins 实现 CICD
linux·运维·docker·kubernetes·gitlab·jenkins