---
# System prepended metadata

title: '**盤點系統 - Jenkins Pipeline(獨立伺服器架構)**'
tags: [CICD, Micro-service, 盤點系統, Jenkins, 自動化佈署]

---

# **盤點系統 - Jenkins Pipeline(獨立伺服器架構)**

## **1. 登入 Jenkins**

**以瀏覽器鍵入 http://10.10.0.51:8080**

> **新增作業**

選單路徑 : Dashboard -> 新增作業
![](https://i.imgur.com/EjqqDTG.png)
<br>




## **2. AssetsAPI Pipeline**
### **Build Triggers**
![](https://i.imgur.com/6mnij6Z.png)

### **Pipeline script**
```bash=
pipeline {
    agent { label 'AssetsAPI' }
    
    stages { 
    
        //1. 拉取代碼        
        stage('1. Pull') {            
            steps {                
                echo "Pull ...."
                checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[credentialsId: '9251bb32-16b6-40f3-9d31-d9e389ceba4c', url: 'http://10.10.0.52/root/assetsapi.git']]])	
                }        
	    }              
	  
        // 2. 代碼掃瞄
        stage('scan code') {
            steps {
                sh "/root/.dotnet/tools/dotnet-sonarscanner  begin /key:AssetsAPI_Pipeline /d:sonar.host.url='http://10.10.0.61:9000' /d:sonar.login='e9fe284dee21de1344abfa90fd6c5ed8f8a71b9f'"
                sh "dotnet build"
                sh "/root/.dotnet/tools/dotnet-sonarscanner  end  /d:sonar.login='e9fe284dee21de1344abfa90fd6c5ed8f8a71b9f'"
            }
        } 
		
        //3. 編譯代碼        
        stage('3. Build') {  
            steps {                
                echo "Build ...."   
                sh """
                   cd ./AssetsAPI
                   dotnet publish -c Release -r linux-x64 /p:PublishSingleFile=true
                """
            }      
        }
		
        //4. 佈署
        stage('4. Deploy') {  
            steps {                
                echo "Deploy ...."   
                sh """
                    cp -rp -f ./AssetsAPI/bin/Release/netcoreapp3.1/linux-x64/publish /opt/Assets

                    sed -i -e 's/192.168.50.47/10.10.0.209/' -e 's/sa/ewill/' -e 's/!QAZ2wsx/ewill1qaz@WSX/' -e 's/8030/5011/' /opt/Assets/appsettings.json

                   systemctl stop assetsapi.service
                   systemctl start assetsapi.service
                """
            }      
        }
    }
}
```

> **Pipeline - 原碼管理**
```bash=6
        //1. 拉取代碼        
        stage('1. Pull') {            
            steps {                
                echo "Pull ...."
                checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[credentialsId: '9251bb32-16b6-40f3-9d31-d9e389ceba4c', url: 'http://10.10.0.52/root/assetsapi.git']]])	
                }        
	    }        
```
> **Pipeline - 代碼掃瞄**
```bash=14
        // 2. 代碼掃瞄
        stage('scan code') {
            steps {
                sh "/root/.dotnet/tools/dotnet-sonarscanner  begin /key:AssetsAPI_Pipeline /d:sonar.host.url='http://10.10.0.61:9000' /d:sonar.login='e9fe284dee21de1344abfa90fd6c5ed8f8a71b9f'"
                sh "dotnet build"
                sh "/root/.dotnet/tools/dotnet-sonarscanner  end  /d:sonar.login='e9fe284dee21de1344abfa90fd6c5ed8f8a71b9f'"
            }
        } 
```

> **Pipeline - 編譯代碼**
```bash=23
        //3. 編譯代碼
        stage('3. Build') {  
            steps {                
                echo "Build ...."   
                sh """
                   cd ./AssetsAPI
                   dotnet publish -c Release -r linux-x64 /p:PublishSingleFile=true
                """
            }      
        }
```

> **Pipeline - 佈署**
```bash=34
        //4. 佈署
        stage('4. Deploy') {  
            steps {                
                echo "Deploy ...."   
                sh """
                    cp -rp -f ./AssetsAPI/bin/Release/netcoreapp3.1/linux-x64/publish /opt/Assets

                    sed -i -e 's/192.168.50.47/10.10.0.209/' -e 's/sa/ewill/' -e 's/!QAZ2wsx/ewill1qaz@WSX/' -e 's/8030/5011/' /opt/Assets/appsettings.json

                   systemctl stop assetsapi.service
                   systemctl start assetsapi.service
                """
            }      
        }
```

## **3. Inventory Pipeline**
### **Build Triggers**
![](https://i.imgur.com/6mnij6Z.png)

### **Pipeline script**
```bash=
pipeline {
    agent { label 'inventory' }
    
    stages {                
	  
        //1. 拉取代碼        
        stage('Pull') {            
            steps {                
                echo "Pull ...."
                checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[credentialsId: '9251bb32-16b6-40f3-9d31-d9e389ceba4c', url: 'http://10.10.0.52/root/inventory.git']]])
            }        
        }              
	  
        //2. 佈署
        stage('Deploy') {  
            steps {                
                echo "Deploy ...."   
                sh """
                    cp -rp Inventory_2 /opt/Inventory

                    sed -i -e 's/192.168.50.47/10.10.0.209/' /opt/Inventory/js/pages/ip.js
                """
            }      
        }
    }
}


```


###### tags: `盤點系統` `自動化佈署` `Jenkins` `CICD` `Micro-service`