> For the complete documentation index, see [llms.txt](https://jose-antonio-henriquez-chavarria.gitbook.io/ci-cd-jenkins/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jose-antonio-henriquez-chavarria.gitbook.io/ci-cd-jenkins/jenkins-pipeline.md).

# jenkins Pipeline

Creamos una tarea nueva

![](/files/-Lf5DCcP2ZyuGyjawlyj)

para este caso se usa la opcion de pipeline

![](/files/-Lf5DCcTkkzJgo_URQEy)

Configuramos los parametros necesarios para poder ejecutar el pipeline, para el escenario de ejemplo configuramos

* Definition: "Pipeline script from SCM"
* SCM: "Git"
* Repositories: url del repositorio
* Script path: para este caso en el repositorio de ejemplo el script se encuentra en la carpeta "misc" entonces la ruta se define como "misc/Jenkinsfile"

![](/files/-Lf5DCcVT2-NkuJXABi9)

El codigo del archivo Jenkinsfile es el siguiente:

```groovy
node {
   def commit_id
   stage('Preparation') {
     checkout scm
     sh "git rev-parse --short HEAD > .git/commit-id"                        
     commit_id = readFile('.git/commit-id').trim()
   }
   stage('test') {
     nodejs(nodeJSInstallationName: 'nodejs') {
       sh 'npm install --only=dev'
       sh 'npm test'
     }
   }
   stage('docker build/push') {
     docker.withRegistry('https://index.docker.io/v1/', 'dockerhub') {
       def app = docker.build("alphyon/docker-node-example:${commit_id}", '.').push()
     }
   }
}
```

en este script se ha definido un nodo con 3 escenarios para ejecutarse en nuestro pipeline, para el ejempo se hace un escenario de preparacion, el cual revisa el repositorio para hacer un push de los archivos que se necesitan en la ejecucion de la prueba, un escenario de test el cual instala las dependencias necesarias y corre los unit test almacenados en el proyecto y por ultimo un stage de construccion del contendor y su publicacion en el repositorio de imagenes de docker, en la imagen siguiente se observa el resultado de ejecuciones del pipeline

![](/files/-Lf5DCcX2OelmU0HAoue)

## Agregando un nuevo stage

podemos agregar los escenarios que sean posibles a nuestos jobs para ello solo debemos crear una nueva seccion dentro del script que desamos ejecutar, para este caso se agrega un nuevo script con un escenario extra el cual es hacer unas pruebas con un contenedor que corra una base de datos

```
node {
   def commit_id
   stage('Preparation') {
     checkout scm
     sh "git rev-parse --short HEAD > .git/commit-id"
     commit_id = readFile('.git/commit-id').trim()
   }
   stage('test') {
     def myTestContainer = docker.image('node:4.6')
     myTestContainer.pull()
     myTestContainer.inside {
       sh 'npm install --only=dev'
       sh 'npm test'
     }
   }
   stage('test with a DB') {
     def mysql = docker.image('mysql').run("-e MYSQL_ALLOW_EMPTY_PASSWORD=yes") 

def myTestContainer = docker.image('node:4.6')
     myTestContainer.pull()
     myTestContainer.inside("--link ${mysql.id}:mysql") { // using linking, mysql will be available at host: mysql, port: 3306
          sh 'HOME=$PWD npm install --only=dev' 
          sh 'npm test'                     
     }                                   
     mysql.stop()
   }                                     
   stage('docker build/push') {            
     docker.withRegistry('https://index.docker.io/v1/', 'dockerhub') {
       def app = docker.build("alphyon/docker-node-example:${commit_id}", '.').push()
     }                                     
   }                                       
}
```

Creamos una nueva tarea del tipo pipeline con la referencia del archivo que contiene nuestro nuevo script y lo ejecutamos, como se observa en la imagen se ejecutaron 4 escenarios

![](/files/-Lf5DCcZ8N_6EytBZgG0)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://jose-antonio-henriquez-chavarria.gitbook.io/ci-cd-jenkins/jenkins-pipeline.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
