# 【Jenkins】Jenkinsfile

[Using a Jenkinsfile](https://www.jenkins.io/doc/book/pipeline/jenkinsfile/)

```JavaScript
pipeline {
    agent any
    stages{
        stage('Init'){
            steps {
                echo "Testing......"
            }
        }
 	stage('Build'){
            steps {
                echo "Building......"
            }
        }
 	stage('Deploy'){
            steps {
                echo "Code Deployed."
            }
        }
    }
}
```

```JavaScript
pipeline {
    agent any

    tools{
        maven 'local maven'
    }

    parameters{
        string(name: 'tomcat_dev', defaultValue: '1.1.1.1', description: 'Staging Server')
        string(name: 'tomcat_prod', defaultValue: '2.2.2.2', description: 'Production Server')
    }

    triggers {
         pollSCM('* * * * *')
     }

     stages{
        stage('Build'){
            steps {
                sh 'mvn clean package'
            }
            post {
                success {
                    echo '开始存档...'
                    archiveArtifacts artifacts: '**/target/*.war'
                }
            }
        }

     stage ('Deployments'){
            parallel{
                stage ('Deploy to Staging'){
                    steps {
                        sh "scp -i /Users/gaoyan/Documents/SunnyDemo/dev/tomcat-demo.pem.txt **/target/*.war ec2-user@${params.tomcat_dev}:/var/lib/tomcat8/webapps"
                    }
                }

                stage ("Deploy to Production"){
                    steps {
                        sh "scp -i /Users/gaoyan/Documents/SunnyDemo/dev/tomcat-demo.pem.txt **/target/*.war ec2-user@${params.tomcat_prod}:/var/lib/tomcat8/webapps"
                    }
                }
            }
        }
    }
}
```

- [\[Day 25\] Pipeline 專案 (上) - iT 邦幫忙::一起幫忙解決難題，拯救 IT 人的一天 (ithome.com.tw)](https://ithelp.ithome.com.tw/articles/10195957)
- [\[Day 26\] Pipeline 專案 (中) - iT 邦幫忙::一起幫忙解決難題，拯救 IT 人的一天 (ithome.com.tw)](https://ithelp.ithome.com.tw/articles/10196116)
- [\[Day 27\] Pipeline 專案 (下) - iT 邦幫忙::一起幫忙解決難題，拯救 IT 人的一天 (ithome.com.tw)](https://ithelp.ithome.com.tw/articles/10196252)

---

**問題1**：\*\*\\target\\\*.war: No such file or directory

解決辦法：

Jenkins無法根據正則匹配找到war文件，我們需要在Jenkinsfile中指定到war文件的完整路徑，例如/var/jenkins\_home/workspace/package\_pipelpine/webapp/target/\*.war(更改成你機器上的實際路徑)。而不是\*\*\\target\\\*.war

---

<div id="bkmrk-%E5%A6%82%E6%9E%9C%E5%87%BA%E7%8F%BE%E9%9D%A2%E4%B8%8B%E9%8C%AF%E8%AA%A4%E6%8F%90%E7%A4%BA%EF%BC%9A-%5Bdeploy-"><div><div><div><div><div><div><div><div><div data-purpose="curriculum-item-viewer-content"><section aria-label="第 7 章節：Jenkins Pipeline As Code (Jenkinsfile)，第 58 堂講座：問題解決：完全自動化Jenkins Pipeline-代碼執行" class="lecture-view--container--pL22J">如果出現面下錯誤提示：

\[Deploy to Production\] Host key verification failed.  
\[Deploy to Production\] lost connection

解決思路如下：

請改用如下命令

1\.  
scp -o StrictHostKeyChecking=no -i /Users/gaoyan/Documents/SunnyDemo/dev/tomcat-demo.pem.txt webapp/target/webapp.war ec2-user@52.15.183.253:/var/lib/tomcat8/webapps

加了-o StrictHostKeyChecking=no參數， 這樣主機密鑰就會在連接時自動加入到known\_hosts中去

2\.

命令行也用加了-o參數的命令手動登錄試試

ssh -o StrictHostKeyChecking=no ec2-user@13.58.179.83 -i tomcat-demo.pem.txt

這樣主機密鑰就會在連接時自動加入到known\_hosts中去

3.保證用jenkins執行的時候 tomcat-demo.pem.txt這個文件其他用戶對他有操作權，但是注意執行上面第二個命令時，會提示其他人不能對這個文件有訪問權，所以用sudo chmod 700 tomcat-demo.pem.txt， 執行完這個命令以後，再把權限改成 644， sudo chmod 644 tomcat-demo.pem.txt， 然後去jenkins執行第一條命令

4.還不成功刪除known\_hosts試試， cd~/.ssh

rm known\_hosts

如果出現：

Permissions 0644 for 'jenkinskey.pem' are too open.  
It is required that your private key files are NOT accessible by others.  
This private key will be ignored.  
Load key "jenkinskey.pem": bad permissions

解決辦法：

chmod 400 pem\_filename.pem

</section></div></div></div></div></div></div></div></div></div></div>