简体   繁体   English

詹金斯Maven管道

[英]Jenkins Maven Pipeline

I want to make a Jenkinsfile that will do tests and build my Spring boot Java application. 我想制作一个Jenkinsfile来进行测试并构建我的Spring boot Java应用程序。 The problem is that my tests require Postgres and RabbitMQ. 问题是我的测试需要Postgres和RabbitMQ。

What I'm trying to do: 我正在尝试做的是:

1) Setup Jenkins in docker 1)在Docker中设置Jenkins

## Run Jenkins Docker : 
sudo docker run -d -p 8080:8080 -p 50000:50000 -v /home/jenkins:/var/jenkins_home -v /var/run/docker.sock:/var/run/docker.sock -u root jenkins
Bash into docker container

## Bash into new docker container
docker exec -it {{ontainer_ID}} bash   

 ## Download an install docker as root
curl -sSL https://get.docker.com/ | sh
exit

2) Make pipeline to do it: 2)进行管道操作:

pipeline {
    agent {
        docker {
            image 'maven:3-alpine'
            args '-v /root/.m2:/root/.m2'
        }
    }
    stages {
        stage('Build') {
            steps {
                sh 'mvn -B -DskipTests clean package'
            }
        }
        stage('Test') {
            steps {
                    /* Run some tests which require PostgreSQL */
                    sh 'mvn test'
            }
            post {
                always {
                    junit 'target/surefire-reports/*.xml'
                }
            }
        }
    }
}

My goal to add postgres and rabbit to be launched on the phase right before tests. 我的目标是在测试前的阶段添加postgres和Rabbit。 I found this https://jenkins.io/doc/book/pipeline/docker/ There is an example how to run additional docker images: 我发现了这个https://jenkins.io/doc/book/pipeline/docker/有一个示例如何运行其他docker镜像:

checkout scm
/*
 * In order to communicate with the MySQL server, this Pipeline explicitly
 * maps the port (`3306`) to a known port on the host machine.
 */
docker.image('mysql:5').withRun('-e "MYSQL_ROOT_PASSWORD=my-secret-pw" -p 3306:3306') { c ->
    /* Wait until mysql service is up */
    sh 'while ! mysqladmin ping -h0.0.0.0 --silent; do sleep 1; done'
    /* Run some tests which require MySQL */
    sh 'make check'
}

Looking for some expirienced devops who can help with my setup. 寻找一些经验丰富的开发人员可以帮助我进行设置。 Thanks. 谢谢。

At the time of writing, declarative pipeline doesn't support such sidecar containers (as described in the docs . So what you found is correct for your problem. 在撰写本文时,声明性管道不支持此类sidecar容器(如docs中所述。因此,您发现的内容对您的问题是正确的。

The snippet you found is, however, for scripted pipeline . 但是,您发现的代码段是针对脚本管道的 To use this within your declarative pipeline, you need to wrap it in a script step: 要在声明式管道中使用此代码,需要将其包装在script步骤中:

stage('Test') {
  steps {
    docker.image('postgres:9').withRun('<whatever perameters you need>') { c ->
      sh 'mvn test'
    }
  }
}

Of course, replace this with the postgres 当然,用postgres代替

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM