0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

jenkins 設定されている すべての job から 情報取得

Last updated at Posted at 2024-11-17
import jenkins.model.Jenkins 
import hudson.model.* 
import groovy.json.JsonBuilder 
import hudson.plugins.git.GitSCM 
import com.cloudbees.hudson.plugins.folder.Folder 

// ログ出力ディレクトリの作成
def logDir = new File('/root/Jenkins_log')
if (!logDir.exists()) {
    logDir.mkdirs()
}

// 結果を保存するマップ
def jobConfigs = [:] 
def filteredJobConfigs = [:] 

// すべてのジョブを取得 
Jenkins.instance.getAllItems(AbstractItem.class).each { job -> 
    def config = [:] 
    try { 
        // 基本情報の取得 
        config.name = job.name 
        config.fullName = job.fullName 
        config.url = job.absoluteUrl 
        config.description = job.description 
        
        // ジョブタイプの判定 
        config.jobType = job.getClass().simpleName 
        
        // ビルドトリガーの取得 
        def triggers = [] 
        if (job.hasProperty('triggers')) { 
            job.triggers.each { trigger -> 
                triggers << [ 
                    type: trigger.key.getClass().simpleName, 
                    spec: trigger.value.spec 
                ] 
            } 
        } 
        config.triggers = triggers 
        
        // SCM情報の取得 
        if (job.hasProperty('scm')) { 
            def scm = [:] 
            if (job.scm instanceof GitSCM) { 
                scm.type = 'Git' 
                scm.repositories = job.scm.repositories.collect { repo -> 
                    [ 
                        url: repo.URIs[0].toString(), 
                        branches: repo.getBranches().collect { it.name } 
                    ] 
                } 
            } else { 
                scm.type = job.scm.getClass().simpleName 
            } 
            config.scm = scm 
        } 
        
        // ビルド後の動作の取得(フォルダの場合はスキップ) 
        if (!(job instanceof Folder) && job.hasProperty('publishers')) { 
            config.publishers = job.publishers.collect { publisher -> 
                [ type: publisher.getClass().simpleName ] 
            } 
        } 
        
        // ビルドステップの取得(フリースタイルプロジェクトの場合) 
        if (job instanceof FreeStyleProject) { 
            config.buildSteps = job.builders.collect { builder -> 
                [ 
                    type: builder.getClass().simpleName, 
                    command: builder.hasProperty('command') ? builder.command : null 
                ] 
            } 
        } 
        
        // パラメータの取得 
        if (job.hasProperty('properties')) { 
            def params = job.properties.findAll { it.class.simpleName == 'ParametersDefinitionProperty' } 
            config.parameters = params.collect { param -> 
                param.parameterDefinitions.collect { 
                    [ 
                        name: it.name, 
                        type: it.getClass().simpleName, 
                        defaultValue: it.defaultValue, 
                        description: it.description 
                    ] 
                } 
            }.flatten() 
        } 
        
        jobConfigs[job.fullName] = config 
        
        // 最新の成功ビルドの確認 
        def lastSuccessfulBuild = job.getLastSuccessfulBuild() 
        if (lastSuccessfulBuild != null && lastSuccessfulBuild.number != '-') { 
            filteredJobConfigs[job.fullName] = config 
        } 
    } catch (Exception e) { 
        println "Error processing job ${job.fullName}: ${e.message}" 
        // エラーログを別ファイルに出力
        new File("${logDir}/error_log.txt").append("Error processing job ${job.fullName}: ${e.message}\n")
    } 
} 

// 結果をJSON形式で出力 
def json = new JsonBuilder(jobConfigs).toPrettyString() 
new File("${logDir}/jenkins_job_configs.json").text = json 
println "設定情報の取得が完了しました。${logDir}/jenkins_job_configs.jsonに保存されました。" 

// フィルタリングされた結果をJSON形式で出力 
def filteredJson = new JsonBuilder(filteredJobConfigs).toPrettyString() 
new File("${logDir}/filtered_jenkins_job_configs.json").text = filteredJson 
println "フィルタリングされた設定情報の取得が完了しました。${logDir}/filtered_jenkins_job_configs.jsonに保存されました。"
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?