1
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?

More than 1 year has passed since last update.

JenkinsでMavenビルド

Last updated at Posted at 2024-03-04

1.フォルダの作成

makejob\jobdsl\folder
folder.groovy

def folderPath = "${FOLDER_PATH}"
folder(folderPath) {
//    displayName('Project A')
    description("Folder for " + "${FOLDER_PATH}")
}

2.job登録

jobdsl\pipelineJob
pipeline_01_hello_world.groovy
//参考
//https://jenkinsci.github.io/job-dsl-plugin/#path/pipelineJob

//参考
//https://jenkinsci.github.io/job-dsl-plugin/#path/pipelineJob

def folderPath = "${FOLDER_PATH}"
def jobName = "${FOLDER_PATH}" + "/" + "01_hello_world"

// checkout a project into the workspace directory
pipelineJob(jobName) {
  // Sets a description for the item.
  description("ビルド")

//  keepDependencies(false)
  logRotator {
    numToKeep(5)
    artifactNumToKeep(5)
  }
  triggers {
    // Polls source control for changes at regular intervals.
    scm('H/5 * * * 0')
    // Starts a build on completion of an upstream job, i.e. adds the "Build after other projects are built" trigger.
    upstream('makejob', 'SUCCESS')
  }
  // Adds a workflow definition.
  definition {
    // Loads a pipeline script from SCM.
    cpsScm {
      scm {
      // Adds a Subversion SCM source.
        svn{
          // At least one location must be specified.
          location('http://192.168.40.206:18080/svn/jenkins/trunk/terasoluna-tourreservation-5.7.0.RELEASE') {
            // Sets credentials for authentication with the remote server.
            credentials('49d7bee5-1182-4c96-932a-c5a01e109c2b')
            // If set, disables externals definition processing.
            ignoreExternals(true)
          }
        }
      }
    scriptPath('Jenkinsfile')
    }
  }
}

//queue(jobName)


3.ジョブ登録とフォルダ作成

makejob\pipeline
pipeline_00_seed_pipeline.groovy


pipeline {

    agent any

    stages {

        stage('create decrative-pipeline jobs') {
            environment {
                FOLDER_PATH = "test-pipeline"
            }
            steps {
                jobDsl(
                    targets: [
                        'jobdsl/folder/folder.groovy',
                        'jobdsl/pipelineJob/pipeline_01_hello_world.groovy'
          'jobdsl/pipelineJob/pipeline_24_download_beautiful_landscape_video.groovy',
//                        'jobdsl/pipelineJob/pipeline_25_upload_artifact_to_dropbox.groovy',
                    ].join('\n'),
                    additionalParameters: [
                        FOLDER_PATH: "${FOLDER_PATH}"
//                        ,
//                        GIT_HUB_OWNER_AND_PROJECT: "sakamaki-y123/jenkins-continuous-delivery",
//                        GIT_HUB_PROTOCOL:"https",
//                        GIT_HUB_BRANCH: "*/master"
                    ]
                )
            }
        }
    }
}

4.buildジョブ

Jenkinsfile

pipeline {
    agent any
    stages {
        stage('build') {
            agent {
                docker {
                    label 'master'
                    image 'maven:3.8.3-amazoncorretto-8'
                    // マウント
//                    args '-v /root/Docker/Jenkins/mavenCache:/var/maven'
                    // cpu制限 30%
//                    args '-v /root/Docker/Jenkins/mavenCache:/root/.m2:z -u root --cpus 0.3'
                    // マウント + 実行cpu指定
                    args '-v /root/Docker/Jenkins/mavenCache:/root/.m2:z -u root --cpuset-cpus 1'
                    // 既存のワークスペースを使い続ける
                    reuseNode true
                }
            }
            steps {
                // Run Maven on a Unix agent.
                // DB初期化
                //sh "mvn -f terasoluna-tourreservation-initdb/pom.xml sql:execute"
                // 一部だけ削除
//                sh "rm -rf /root/.m2/repository/org/terasoluna"
                // build war作成
                sh "mvn clean package -DskipTests=true"
            }
            post {
                success { 
                archiveArtifacts 'terasoluna-tourreservation-web/target/*.war'
                }
            }
        }
        stage('docker build') {
            steps {
                sh "ls -la ./terasoluna-tourreservation-web/target"
//                sh 'docker build -t test-tomcatinterasoluna:0.1 .'
            }
        }
        stage('triggerChildJob') {
            steps {
//                build job: "test-pipeline/01_hello_world", wait: true
                // 他のジョブを非同期(wait: false)で実行
                build job: "test-pipeline/01_hello_world", wait: false
            }
        }
    }
}
1
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
1
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?