jenkins pipline docker
时间: 2023-10-23 11:14:19 浏览: 105
Jenkins Pipeline is a tool that allows you to define and automate your CI/CD workflows as code. Docker is a platform that allows you to build, run, and manage containerized applications.
When using Jenkins Pipeline with Docker, you can define your build, test, and deployment steps within the pipeline code and use Docker containers to execute those steps. This allows for greater portability and consistency across different environments.
To use Docker within a Jenkins Pipeline, you will need to have the Docker plugin installed on your Jenkins instance. You can then define your pipeline stages to include steps such as building a Docker image, running tests within a Docker container, and deploying your application to a Docker registry.
Here is an example pipeline code that uses Docker:
```
pipeline {
agent {
docker {
image 'node:12-alpine'
}
}
stages {
stage('Build') {
steps {
sh 'npm install'
sh 'npm run build'
}
}
stage('Test') {
steps {
sh 'npm run test'
}
}
stage('Deploy') {
steps {
withCredentials([usernamePassword(credentialsId: 'dockerhub', usernameVariable: 'DOCKER_USERNAME', passwordVariable: 'DOCKER_PASSWORD')]) {
sh 'docker login -u $DOCKER_USERNAME -p $DOCKER_PASSWORD'
}
sh 'docker build -t myapp:latest .'
sh 'docker push myapp:latest'
}
}
}
}
```
This pipeline uses the `node:12-alpine` Docker image as the build agent, installs dependencies, runs tests, and finally deploys the application to a Docker registry using credentials stored in Jenkins.
阅读全文