jenkins 对于具有两个容器的NodeJS,CI/CD管道的步骤是什么

wtlkbnrh  于 2023-03-17  发布在  Jenkins
关注(0)|答案(1)|浏览(161)

我正在阅读一本关于Jenkins的书,作者使用Sping Boot ,他使用的技术与我的不同,我可以说这也与部署有关(例如,我不使用Kubernetes),但我想根据我的实际情况使用一些知识。我的堆栈和部署策略如下。
我有一个使用PostgreSQL的NodeJS项目。NodeJS项目使用express和handlebar,并且在一个docker图像中。postgres在另一个图像中。
为了运行我的系统,我使用docker-compose运行这两个图像
更正:如果在我的情况下部署CI/CD是错误的,我必须让Jenkins执行以下操作:

-see if the NodeJS project repository changed
-copy the code
-run npm install
-build an image copying all the project source code into the image
-run the containers on the remote server using the docker -h option.

obs. here I am no longer using docker-compose to run the containers.
eagi6jfj

eagi6jfj1#

这里是Jenkin工作的文件

pipeline {
  environment {
    imagename = "harshmanvar/node-web-app"
    registryCredential = 'docker'
    dockerImage = ''
  }
  agent any
  stages {
    stage('Cloning Git') {
      steps {
        git([url: 'https://github.com/harsh4870/node-js-aws-cloudbuild-basic-ci-cd.git', branch: 'main', credentialsId: 'github'])

      }
    }
    stage('Building image') {
      steps{
        script {
          dockerImage = docker.build imagename
        }
      }
    }
    stage('Deploy Image') {
      steps{
        script {
          docker.withRegistry( '', registryCredential ) {
            dockerImage.push("$BUILD_NUMBER")
             dockerImage.push('latest')

          }
        }
      }
    }
    stage('Deploy to K8s') {
      steps{
        script {
          sh "sed -i 's,TEST_IMAGE_NAME,harshmanvar/node-web-app:$BUILD_NUMBER,' deployment.yaml"
          sh "cat deployment.yaml"
          sh "kubectl --kubeconfig=/home/ec2-user/config get pods"
          sh "kubectl --kubeconfig=/home/ec2-user/config apply -f deployment.yaml"
        }
      }
    }
    
    
    stage('Remove Unused docker image') {
      steps{
        sh "docker rmi $imagename:$BUILD_NUMBER"
         sh "docker rmi $imagename:latest"

      }
    }
  }
}

它包含使用DockerfileCreds构建Docker映像的步骤,以提取Git代码。
您可以参考我的repo,其中包含一个Node js****express示例,以使用Jenkin配置构建Docker映像,还包含buildspec.yml,供类似于JenkinAWS代码构建服务使用。
https://github.com/harsh4870/node-js-aws-codebuild-basic-ci-cd
Jenkin作业由Github在发生任何提交事件或根据您的配置设置创建MR/PR触发,您也可以定期轮询Jenkin检查更改。

相关问题