Creating an Angular Pipeline with Jenkins

If you are not familiar with Angular pipelines it is used to transform data on a template without writing boilerplate code in a component. Jenkins is an open source server which is written only using Java. It facilitates continuous integration and delivery in software projects by automating parts related to build, test and deployment. We will discuss how to set up Jenkins, create a Jenkinsfile, and create a pipeline. First you need to create a network for Jenkins.

docker network create jenkins

Then add two volumes to share Docker client TLS certificates and persist Jenkins data. This step is to avoid losing data when you shut down your PC. 

docker volume create jenkins-docker-certs
docker volume create jenkins-data

The following command should be executed.

docker image pull docker:dind
docker container run --name jenkins-docker \
  --restart unless-stopped \
  --detach \
  --privileged --network jenkins \
  --network-alias docker \
  --env DOCKER_TLS_CERTDIR=/certs \
  --volume jenkins-docker-certs:/certs/client \
  --volume jenkins-data:/var/jenkins_home \
  --publish 2376:2376\
  docker:dind
docker image pull jenkinsci/blueocean
docker container run --name jenkins-blueocean \
  --restart unless-stopped \
  --detach \
  --network jenkins \
  --env DOCKER_HOST=tcp://docker:2376 \
  --env DOCKER_CERT_PATH=/certs/client \
  --env DOCKER_TLS_VERIFY=1 \
  --volume jenkins-data:/var/jenkins_home \
  --volume jenkins-docker-certs:/certs/client:ro \
  --publish 8080:8080 \
  --publish 50000:50000 \
jenkinsci/blueocean

Then unlock Jenkins using the initial password located in the CI blue ocean Docker container. 

docker container ps
4ec29afb280f        jenkinsci/blueocean
1ce11b131265        docker:dind
docker container exec 4ec29afb280f cat /var/jenkins_home/secrets/initialAdminPassword
Bd95aa5131a142b690795fa9427287a8

Next step is to configure the plug-ins you want to install. Finally set a Jenkins URL.

Now we are moving to the second part of our blog, creating a Jenkins file. Specify a base DOcker image to run our commands. Then we specify the stages to go over ie Install, Testing, Build. You can add additional steps if you need. Now you can choose to deploy automatically or publish a Docker image. The given below is our pipeline. The pipeline should be put inside a jenkins file inside the root folder. 

  --volume jenkins-docker-certs:/certs/client:ro \
  --publish 8080:8080 \
  --publish 50000:50000 \
jenkinsci/blueocean

Then unlock Jenkins using the initial password located in the CI blue ocean Docker container. 

docker container ps
4ec29afb280f        jenkinsci/blueocean
1ce11b131265        docker:dind
docker container exec 4ec29afb280f cat /var/jenkins_home/secrets/initialAdminPassword
Bd95aa5131a142b690795fa9427287a8


pipeline {
  agent {
    docker { image 'node:latest' }
  }
  stages {
    stage('Install') {
      steps { sh 'npm install' }
    }
 
    stage('Test') {
      parallel {
        stage('Static code analysis') {
            steps { sh 'npm run-script lint' }
        }
        stage('Unit tests') {
            steps { sh 'npm run-script test' }
        }
      }
    }
 
    stage('Build') {
      steps { sh 'npm run-script build' }
    }
  }
}

Open jenkins URL on https://localhost:8080 and create a new item. Then a new menu will open. You can add a description. We are creating a multi branch so look for branch sources and select Git. Then pass your project repository and credentials.Enabling “Scan Multibranch Pipeline Triggers ” will trigger a build only when there are source code changes.