---
# System prepended metadata

title: '**Jenkins 作業管理**'
tags: [CICD, Micro-service, Jenkins, 自動化佈署]

---

# **Jenkins 作業管理**

## **1. 新增作業**

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

:::success
### **新增作業**
選單路徑 : Dashboard -> 新增作業
![](https://i.imgur.com/EjqqDTG.png)
<br>
### **選擇作業類型**
![](https://i.imgur.com/39bLge0.png)
:::
## **2. 建置 Free-Style 軟體專案**


### **Slave節點**
:::success
![](https://i.imgur.com/uQsP0Tl.png)
:::
### **原始碼管理**
:::success
輸入 Repository URL
![](https://i.imgur.com/rAdRK4q.png)
<br>
建立授權帳號
![](https://i.imgur.com/PZ3znRk.png)
<br>
以使用者/密帳為認證機制
![](https://i.imgur.com/60LV8v9.png)
<br>
選擇認證帳戶
![](https://i.imgur.com/8ButmjK.png)
<br>
輸入 Branch
![](https://i.imgur.com/ypy0SIM.png)
:::
### **建置觸發程序**
:::success
輸入排程資訊
![](https://i.imgur.com/wuqHrGu.png)

| 欄位  | 定義 | 範圍    |範例|範例說明|
| ----- | ---- | ------- |--|--|
| 欄位1 | 分  | 0-59 分 | H  | 0-59隨機值|
| 欄位2 | 時  | 0-23 時 | H(1-6)/2|1:00 - 6:00 每隔2小時執行一次 |
| 欄位3 | 日  | 1–31 日 | * |每天|
| 欄位4 | 月  | 1–12 月 | * |每月|
| 欄位4 | 星期 | 0–7     | 1-5   | 星期一至星期五|

各個欄位可以代入H當做隨機值，H後面可以接上隨機值的範圍，無特別指定的值可代入*做為任何合理值
:::

### **建置**
:::success
以呼叫 Shell 方式建置為例
![](https://i.imgur.com/OooDofT.png)
<br>
依各專案調整建置指令
![](https://i.imgur.com/BBJ6Acl.png)
:::
### **馬上建置**
:::success
執行馬上建置
![](https://i.imgur.com/XlJKQY4.png)
<br>
點擊作業項目 -> Console Output
![](https://i.imgur.com/xTp2oHs.png)
:::



## **3. Pipeline**
### **Build Triggers**
:::success
![](https://i.imgur.com/6mnij6Z.png)
:::
### **Pipeline - agent**
:::success
```
pipeline {
agent { label 'seal_uat' }      //slave-name

options {
        timestamps()
        //20分鐘無法執行完畢就timeout
        timeout(time: 30, unit: 'MINUTES')
    }
    

environment {
        BRANCH="*/master" 
        ENV=“uat”              //專案環境
        WAR=“demoseal”         //專案名稱
        CI_REPO_URL="http://gitlab.example.com/root/demoseal.gitt"           //git 數據庫
        CI_CREDENTIAL_ID="7ae20df4-f04c-439f-abe4-e3906d932bed"           //git 憑證
    }


stages {
    }
}
```

:::
### **Pipeline - 原碼管理**
:::success
```
    stage('check out source') {
      steps {
        echo "Check-out" 
        checkout([$class: 'GitSCM', branches: [[name: "${BRANCH}"]], doGenerateSubmoduleConfigurations: false, extensions: [], gitTool: 'Git', submoduleCfg: [], userRemoteConfigs: [[credentialsId: "${CI_CREDENTIAL_ID}", url: "${CI_REPO_URL}"]]])
  }
}
```
:::

### **Pipeline - 編譯**
:::success
```
    stage('Build') {
            steps {
                echo "Start Build" 
	            sh'''
	                export JAVA_HOME=/usr/java/openjdk/java-1.8
	                /opt/apache-maven-3.6.3/bin/mvn clean package -Dmaven.test.skip=true -P "${ENV}"
	            '''
            }
    }
```
:::

### **Pipeline - 佈署**
:::success
```
	stage('Deploy') { 
	    steps {
	        sh'''
                export JAVA_HOME=/usr/java/openjdk/java-1.8
                export JBOSS_HOME=/opt/jboss-eap/jboss-eap-7.4
        
                echo "Undeploy ${WAR} ..."
                output=`$JBOSS_HOME/bin/jboss-cli.sh -c --command="deployment-info --name=${WAR}" || true`
                if [[ $output == *OK* ]]
                then
                    $JBOSS_HOME/bin/jboss-cli.sh --connect --command="undeploy ${WAR}" 
                fi
                
                echo "Deploy demoseal.war ..." 
                $JBOSS_HOME/bin/jboss-cli.sh --connect --command="deploy target/${WAR}"
            '''           
	    }
    }
```
:::


###### tags: `自動化佈署` `Jenkins` `CICD` `Micro-service`