MeWrite Docs

Jenkins: Pipeline failed

Jenkinsパイプラインが失敗した場合のエラー

概要

Jenkinsパイプラインの実行中にエラーが発生し、ビルドが失敗した状態です。Groovyスクリプトのエラーや環境問題など様々な原因があります。

エラーメッセージ

ERROR: script returned exit code 1

または

java.lang.NoSuchMethodError: No such DSL method 'sh' found

原因

  1. スクリプトエラー: Jenkinsfile内のGroovyエラー
  2. ビルドツールの失敗: Maven、Gradle、npmなどの失敗
  3. 権限問題: ファイルやリソースへのアクセス権限
  4. プラグイン不足: 必要なプラグインがインストールされていない

解決策

1. エラーログの確認

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                script {
                    try {
                        sh 'npm run build'
                    } catch (Exception e) {
                        echo "Build failed: ${e.message}"
                        throw e
                    }
                }
            }
        }
    }
}

2. シェルコマンドのデバッグ

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
pipeline {
    agent any
    stages {
        stage('Debug') {
            steps {
                // コマンドを表示
                sh '''
                    set -x
                    echo "Current directory: $(pwd)"
                    echo "Files:"
                    ls -la
                '''
            }
        }
    }
}

3. 環境変数の設定

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
pipeline {
    agent any
    environment {
        NODE_ENV = 'production'
        AWS_REGION = 'ap-northeast-1'
    }
    stages {
        stage('Build') {
            steps {
                withCredentials([string(credentialsId: 'aws-key', variable: 'AWS_KEY')]) {
                    sh 'echo $AWS_KEY | aws configure set aws_access_key_id'
                }
            }
        }
    }
}

4. ツールの指定

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
pipeline {
    agent any
    tools {
        nodejs 'NodeJS-18'
        maven 'Maven-3.9'
        jdk 'JDK-17'
    }
    stages {
        stage('Build') {
            steps {
                sh 'node --version'
                sh 'mvn --version'
            }
        }
    }
}

5. ステージの失敗処理

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
pipeline {
    agent any
    stages {
        stage('Test') {
            steps {
                sh 'npm test'
            }
            post {
                failure {
                    archiveArtifacts artifacts: 'test-reports/**'
                    slackSend channel: '#alerts', message: 'Tests failed!'
                }
            }
        }
    }
}

6. 並列実行

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
pipeline {
    agent any
    stages {
        stage('Parallel Tests') {
            parallel {
                stage('Unit Tests') {
                    steps {
                        sh 'npm run test:unit'
                    }
                }
                stage('Integration Tests') {
                    steps {
                        sh 'npm run test:integration'
                    }
                }
            }
        }
    }
}

7. Docker内でビルド

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
pipeline {
    agent {
        docker {
            image 'node:18-alpine'
            args '-v /var/run/docker.sock:/var/run/docker.sock'
        }
    }
    stages {
        stage('Build') {
            steps {
                sh 'npm ci'
                sh 'npm run build'
            }
        }
    }
}

8. タイムアウト設定

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
pipeline {
    agent any
    options {
        timeout(time: 30, unit: 'MINUTES')
    }
    stages {
        stage('Long Build') {
            options {
                timeout(time: 10, unit: 'MINUTES')
            }
            steps {
                sh 'npm run build'
            }
        }
    }
}

9. リトライ

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
pipeline {
    agent any
    stages {
        stage('Flaky Test') {
            steps {
                retry(3) {
                    sh 'npm run test:e2e'
                }
            }
        }
    }
}

10. 成果物のアーカイブ

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                sh 'npm run build'
            }
        }
    }
    post {
        always {
            archiveArtifacts artifacts: 'dist/**', fingerprint: true
            junit 'test-results/*.xml'
        }
        cleanup {
            cleanWs()
        }
    }
}

よくある間違い

  • Declarative Pipeline と Scripted Pipeline の構文混同
  • sh ステップで Windows ランナーを使用
  • credentialsId のタイポ
  • agent none で直接ステップを実行

最終更新: 2025-12-09