Jenkins Maven Pipeline



我想制作一个将进行测试并构建我的Spring Java应用程序的Jenkinsfile。问题在于我的测试需要postgres和兔子。

我要做的事情:

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(进行管道来做:

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'
                }
            }
        }
    }
}

我的目标是在测试之前在阶段启动邮政和兔子。我发现此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'
}

寻找一些可以帮助我的设置的已故devops。谢谢。

在撰写本文时,声明的管道不支持此类边缘容器(如文档中所述。因此,您发现的是正确的问题。

但是,您发现的片段是脚本管道。要在声明的管道中使用它,您需要将其包装在script步骤中:

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

当然,将其替换为Postgres

最新更新