application deployment by jenkinsfile and docker compose

发布时间 2023-06-04 22:18:09作者: lightsong

Using Jenkins CI/CD for your NodeJS app

https://blog.harveydelaney.com/jenkins-build-test-deploy-node-app/

 

Jenkinsfile

https://github.com/internetarchive/openlibrary/blob/9b7fda26cd154239ac498e06ee513a239c233ee8/scripts/solr_builder/Jenkinsfile#L3

    stage('2: Populate solr') {
      stages {
        stage('Restart Postgres') {
          // Have to restart to because the previous workspace was destroyed on cleanup
          when { expression { return params.REUSE_POSTGRES } }
          steps {
            dir(env.HOST_SOLR_BUILDER_DIR) {
              sh(label: 'Stop the db containers',
                script: 'docker compose stop db adminer')

              sh(label: 'Start the db containers',
                script: 'docker compose up -d --no-deps db adminer')
            }
          }
        }

 

 

https://github.com/grv231/Jenkins-Docker-demo/blob/master/misc/Jenkinsfile

node {
   def git_commit_id
   def to = emailextrecipients([
        [$class: 'CulpritsRecipientProvider'],
        [$class: 'DevelopersRecipientProvider'],
        [$class: 'RequesterRecipientProvider']
  ])
   
   
   stage('Prepare') {
     checkout scm
     sh "git rev-parse --short HEAD > .git/commit-id"
     git_commit_id = readFile('.git/commit-id').trim()
   }
   
   stage('test') {
     def testContainer = docker.image('node:4.6')
     testContainer.pull()
     testContainer.inside {
       sh 'npm install --only=dev'
       sh 'npm test'
     }
   }
   
   try {
   stage('Running Tests with a Database') {
     def mysql = docker.image('mysql').run("-e MYSQL_ALLOW_EMPTY_PASSWORD=yes") 
     def testContainer = docker.image('node:4.6')
     testContainer.pull()
     testContainer.inside("--link ${mysql.id}:mysql") {
          sh 'npm install --only=dev' 
          sh 'npm test'                     
     }     
     currentBuild.result = "SUCCESS";
     mysql.stop()
    }  
   } catch (Exception err) {
     currentBuild.result = "FAILURE";
     def subject = "${env.JOB_NAME} - Build #${env.BUILD_NUMBER} ${currentBuild.result}"
     def content = '${JELLY_SCRIPT,template="html"}'
     
     if(to != null && !to.isEmpty()) {
      emailext(body: content, mimeType: 'text/html',
         replyTo: '$DEFAULT_REPLYTO', subject: subject,
         to: to, attachLog: true )
    }
   }
   
   echo "RESULT_MYSQL_JOB: ${currentBuild.result}"

   
   stage('Pushing builds in Docker hub') {            
     docker.withRegistry('https://index.docker.io/v1/', 'dockerhub') {
       def app = docker.build("grv231/nodejs-docker-jenkins-demo:${git_commit_id}", '.').push()
     }                                     
   }                                       
}                                          

 

Docker Compose Detached: Modes explained

https://bobcares.com/blog/docker-compose-detached/

A Docker Compose command, like a docker run command, launches the services in the foreground by default.

docker compose detached

 

To execute this in the background, use the vom compose-up command with the -d or we can also use the —detach option. Now let us move ahead to the main topic.

 

Docker Compose Detached

In the detachment mode or in the background, we have to  run the docker-compose up command as shown below:

 

docker-compose up -d
- or -
docker-compose up --detach

 

The preceding command launches the containers in the background and prints the new container names.

 

Run Docker Container in Background (Detached Mode)

Before starting a Docker container, you must, first of all, decide if you want to run it in the default foreground mode or in the background in a detached mode.

In the foreground mode, Docker can start the process in the container and attach the console to the process’s standard input, standard output, and standard error.

 

The disadvantage of running a container in the foreground is that you can not access the command prompt anymore, as you can see from the screenshot above. Which means you can not run any other commands while the container is running.

To run a Docker container in the background, use the use -d=true or just -d option. First, stop it from the foreground mode by pressing [Ctrl+C], then run it in a detached mode as shown:

# docker run -d --rm -p 8000:80 -p 8443:443 --name pandorafms pandorafms/pandorafms:latest

In addition, to reattach to a detached container, use docker attach command.

# docker attach --name pandorafms
OR
# docker attach 301aef99c1f3

https://docs.docker.com/engine/reference/commandline/run/

 

https://docs.docker.com/engine/reference/commandline/compose_up/

The docker compose up command aggregates the output of each container (like docker compose logs --follow does). One can optionally select a subset of services to attach to using --attach flag, or exclude some services using --no-attach to prevent output to be flooded by some verbose services.

When the command exits, all containers are stopped. Running docker compose up --detach starts the containers in the background and leaves them running.

 

nohup &

https://www.gnu.org/software/libc/manual/html_node/Termination-Signals.html#:~:text=The%20SIGHUP%28%E2%80%9Chang-up%E2%80%9D%29%20signal%20is%20used%20to%20report%20that,terminal%20to%20jobs%20associated%20with%20that%20session%3B%20this

Macro: int SIGHUP

The SIGHUP (“hang-up”) signal is used to report that the user’s terminal is disconnected, perhaps because a network or telephone connection was broken. For more information about this, see Control Modes.

This signal is also used to report the termination of the controlling process on a terminal to jobs associated with that session; this termination effectively disconnects all processes in the session from the controlling terminal. For more information, see Termination Internals.

 

https://www.cnblogs.com/yunwangjun-python-520/p/10713564.html#_label0_4

&:是指在后台运行,当用户退出(挂起)的时候,命令自动跟着结束


nohup:不挂断的运行,注意并没有后台运行的功能,就是指用nohup运行命令可以使命令永久的执行下去,和用户终端没有关系,例如我们断开SSH连接都不会影响他的运行,注意了nohup没有后台运行的意思;&才是后台运行

因此将nohup和&结合使用,就可以实现使命令永久地在后台执行的功能

 

https://zhuanlan.zhihu.com/p/380607264

后台运行转前台

既然任务可以从前台转后台,那反过来从后台转前台是否可行呢?答案是可以的 首先以后台运行run.sh

$ nohup bash  run.sh 2>&1 & 
[1] 482
nohup: ignoring input and appending output to 'nohup.out'

查看后台任务

$ jobs
[1]  + running    nohup bash run.sh 2>&1

运行fg命令,将任务从后台转到前台

$ fg %1                    
[1]  + 482 running    nohup bash run.sh 2>&1