CICD pipeline(Jenkins) for angular 8 projects on Linux

Posted by Kuo on June 8, 2019
  • Enviroment: Ubuntu 18.04.2 LTS

1. create a new user

  • Login into root
    $ ssh root@host_ip
    
  • Use the adduser command to add a new user
    $ adduser kuo 
    
  • Set and confirm the new user’s password at the prompt.
    Set password prompts:
    Enter new UNIX password:
    Retype new UNIX password:
    passwd: password updated successfully
    
  • Follow the prompts to set the new user’s information. It is fine to accept the defaults to leave all of this information blank.
    User information prompts:
    Changing the user information for username
    Enter the new value, or press ENTER for the default
      Full Name []:
      Room Number []:
      Work Phone []:
      Home Phone []:
      Other []:
    Is the information correct? [Y/n]
    
  • Use the usermod command to add the user to the sudo group.
    $ usermod -aG sudo kuo
    
  • Login into new user
    $ su kuo
    

    2. Install Docker

    $ curl -fsSL https://get.docker.com -o get-docker.sh
    $ sudo sh get-docker.sh
    
  • run docker as non-root user
    $ sudo usermod -aG docker kuo
    
  • logout

3. Install Jenkins by docker

$ sudo mkdir -p /var/jenkins
$ sudo chown kuo /var/jenkins  
$ sudo chmod 775 /var/jenkins
$ docker run -d --rm \
  -u root \
  -p 8080:8080 \
  -v /var/jenkins:/var/jenkins_home \
  -v /var/run/docker.sock:/var/run/docker.sock \
  -v "$HOME":/home jenkinsci/blueocean
  • after jenkins gets started, then login into jenkins server you will see this ```bash

    get container id

    $ docker ps -a output CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d69b9ba9acfc jenkins “/bin/tini – /usr/l…” 18 minutes ago Up 18 minutes 0.0.0.0:8080->8080/tcp, 0.0.0.0:50000->50000/tcp epic_pare

$ docker exec -it d69b9ba9acfc bash $ cd /var/jenkins_home/secrets

* Install plugin by default
* Set admin user/password

### 4. Create Pipeline
create Jenkinsfile

since ng is not installed globally,  we can use our local @angular/cli, so in package.json, we can use the npm commands.
```javascript
  "scripts": {
    "ng": "ng",
    "build": "node ./node_modules/@angular/cli/bin/ng build --prod --aot",
    "test":  "node ./node_modules/@angular/cli/bin/ng test"
pipeline {
    agent {
        docker { image 'node:10-alpine' }
    }
    stages {
        stage('Restore') {
            steps {
                sh 'npm install'
            }
        }
        stage('Build') {
            steps {
                sh 'npm run-script build'
            }
        }
        stage('Test') {
            steps {
                sh 'ng run-script test'
            }
        }        
        stage('Deploy') {
            steps {
                sh 'rm ../../apps/*'
                sh 'cp ./dist/apps/* ../../apps/'
            }
        }             
    }
}

5. Set up Blue Ocean Pipeline

  • Choose Platform by open blue ocean, by default the url is localhost:8080/blue you will see this

you will see this

Enjoy