OpenShift Plugin for Jenkins (jenkins-plugin): Difference between revisions
(38 intermediate revisions by the same user not shown) | |||
Line 2: | Line 2: | ||
* https://github.com/openshift/jenkins-plugin | * https://github.com/openshift/jenkins-plugin | ||
* https://github.com/jenkinsci/openshift-pipeline-plugin (associated repository) | |||
=Internal= | =Internal= | ||
* [[OpenShift_CI/CD_Concepts# | * [[OpenShift_CI/CD_Concepts#OpenShift_Plugin_for_Jenkins_.28jenkins-plugin.29|OpenShift CI/CD Concepts]] | ||
=Overview= | =Overview= | ||
This is the oldest Jenkins/OpenShift integration plugin, which executes inside Jenkins and implements a series of REST flows that interface with the OpenShift master server via its exposed API. This plugin '''does not require''' the [[oc]] binary to be present on the host that executes the plugin logic. Other plugin that may be used is [[OpenShift_Jenkins_Pipeline_DSL_Plugin_(jenkins-client-plugin)|OpenShift Jenkins Pipeline DSL Plugin (jenkins-client-plugin)]]. | This is the oldest Jenkins/OpenShift integration plugin, which executes inside Jenkins and implements a series of REST flows that interface with the OpenShift master server via its exposed API. This plugin '''does not require''' the [[oc]] binary to be present on the host that executes the plugin logic. Other plugin that may be used starting with OpenShift 3.7 is [[OpenShift_Jenkins_Pipeline_DSL_Plugin_(jenkins-client-plugin)|OpenShift Jenkins Pipeline DSL Plugin (jenkins-client-plugin)]]. The "OpenShift Plugin for Jenkins" is currently in maintenance mode. | ||
=Manual Build= | |||
{{External|https://github.com/NovaOrdis/playground/tree/master/openshift/jenkins-plugin/Jenkinsfile-manual-build.groovy}} | |||
Note that GIT_SOURCE_URL and GIT_SOURCE_REF must be defined as environment variables in the build configuration, as shown here [[OpenShift_CI/CD_Concepts#Jenkinsfile|here]] | |||
<syntaxhighlight lang='groovy'> | |||
try { | |||
timeout(time: 20, unit: 'MINUTES') { | |||
def project="" | |||
node { | |||
stage("initialize") { | |||
echo "initializing ..." | |||
project = env.PROJECT_NAME | |||
} | |||
} | |||
echo "project: ${project}" | |||
node("maven") { | |||
stage("checkout") { | |||
echo "checking out from ${GIT_SOURCE_URL} ..." | |||
git url: "${GIT_SOURCE_URL}", branch: "${GIT_SOURCE_REF}" | |||
echo "check out ok" | |||
} | |||
stage("build maven") { | |||
echo "building maven artifacts ..." | |||
sh "mvn clean package -Popenshift" | |||
echo "build ok" | |||
stash name:"war", includes:"target/ROOT.war" | |||
} | |||
} | |||
node { | |||
stage("build image") { | |||
echo "building image ..." | |||
unstash name:"war" | |||
sh "oc start-build ${appName}-docker --from-file=target/ROOT.war -n ${project}" | |||
openshiftVerifyBuild bldCfg: "${appName}-docker", namespace: project, waitTime: '20', waitUnit: 'min' | |||
} | |||
stage("Deploy") { | |||
openshiftDeploy deploymentConfig: appName, namespace: project | |||
} | |||
} | |||
} | |||
} | |||
catch (err) { | |||
echo "build failure: ${err}" | |||
currentBuild.result = 'FAILURE' | |||
throw err | |||
} | |||
</syntaxhighlight> | |||
==Jenkins Slave Pods== | |||
''Jenkins slave pods'' are configured in Jenkins system configuration as Kubernetes pods. They are invoked as follows: | |||
<syntaxhighlight lang='groovy'> | |||
node("maven") { | |||
// | |||
// this will execute into a Maven slave pod | |||
// | |||
... | |||
} | |||
</syntaxhighlight> | |||
where the available types are "maven" and "nodejs", and a base pod to build custom slave pods. The pods are instantiated by the [[OpenShift_CI/CD_Concepts#Kubernetes_Plugin|Jenkins Kubernetes plugin]] in the same project as the Jenkins pod that started them. Since they are completely separated pods, they do not use Jenkin's pod resources: | |||
oc get pods | |||
NAME READY STATUS RESTARTS AGE | |||
jenkins-1-5w13l 1/1 Running 0 1h | |||
maven-f45b0fd18d86 1/1 Running 0 21s | |||
==Parallel Stages== | |||
{{Internal|Jenkins Pipeline Syntax#Parallel_Stages|Parallel Stages}} | |||
=Pipeline DSL= | |||
The pipeline can be scripted or can be specified in a declarative format. A scripted DSL element looks like this: | |||
<syntaxhighlight lang='groovy'> | |||
openshiftBuild(buildConfig: 'something') | |||
</syntaxhighlight> | |||
A declared DSL element looks like this: | |||
<syntaxhighlight lang='groovy'> | |||
openshiftBuild bldCfg: 'something', verbose: 'true', ... | |||
</syntaxhighlight> | |||
Optional configuration parameter that apply to all DSL steps are: | |||
* 'apiURL' - the URL of the OpenShift API endpoint. If nothing is specified, the plugin will inspect KUBERNETES_SERVICE_HOST environment variable and if the variable is not set, the default is "https://openshift.default.svc.cluster.local" See [[OpenShift Concepts#Master_API|Master API]]. | |||
* 'namespace' - the name of the project the objects the DSL refers to are stored in. If nothing is specified, the plugin will inspect PROJECT_NAME environment variable. | |||
* 'authToken' | |||
* 'verbose' - for verbose logging. | |||
==Pipeline DSL Example== | |||
{{External|https://github.com/NovaOrdis/playground/blob/master/openshift/jenkins-plugin/Jenkinsfile-jenkins-plugin-DSL.groovy}} | |||
<syntaxhighlight lang='groovy'> | |||
node { | |||
stage("build") { | |||
echo "build starting ..." | |||
openshiftBuild bldCfg: 's2i' | |||
openshiftVerifyBuild bldCfg: 's2i' | |||
echo "build done" | |||
} | |||
stage ("deploy") { | |||
echo "deployment starting ..." | |||
openshiftDeploy depCfg: 'test-app' | |||
openshiftVerifyDeployment depCfg: 'test-app', replicaCount: '1' | |||
echo "deployment done" | |||
} | |||
stage ("verify") { | |||
echo "service verification starting ..." | |||
openshiftVerifyService svcName: 'test-app' | |||
echo "service verification done" | |||
} | |||
} | |||
</syntaxhighlight> | |||
==openshiftBuild== | |||
{{External|https://github.com/openshift/jenkins-plugin#trigger-openshift-build}} | |||
Trigger a build based on the specified build configuration. Equivalent with [[oc start-build]]. | |||
<syntaxhighlight lang='groovy'> | |||
openshiftBuild bldCfg: 'name_of_the_BuildConfig_to_start' | |||
</syntaxhighlight> | |||
==openshiftDeploy== | |||
{{External|https://github.com/openshift/jenkins-plugin#trigger-openshift-deployment}} | |||
Trigger a deployment based on the specified deployment configuration. | |||
<syntaxhighlight lang='groovy'> | |||
openshiftDeploy depCfg: 'name_of_the_DeployConfig_to_start' | |||
</syntaxhighlight> | |||
==openshiftExec== | |||
{{External|https://github.com/openshift/jenkins-plugin#run-openshift-exec}} | |||
Run a command in a pod. | |||
<syntaxhighlight lang='groovy'> | |||
openshiftExec pod: 'name_of_the_pod', command: '...' | |||
</syntaxhighlight> | |||
==openshiftCreateResource== | |||
{{External|https://github.com/openshift/jenkins-plugin#create-openshift-resources}} | |||
Run a command in a pod. | |||
<syntaxhighlight lang='groovy'> | |||
openshiftExec json|yaml|jsonyaml: '...' | |||
</syntaxhighlight> | |||
==openshiftDeleteResourceByJsonYaml== | |||
{{External|https://github.com/openshift/jenkins-plugin#delete-openshift-resources-from-jsonyaml}} | |||
==openshiftDeleteResourceByLabels== | |||
{{External|https://github.com/openshift/jenkins-plugin#delete-openshift-resources-using-labels}} | |||
==openshiftDeleteResourceByKey== | |||
{{External|https://github.com/openshift/jenkins-plugin#delete-openshift-resources-by-key}} | |||
==openshiftScale== | |||
{{External|https://github.com/openshift/jenkins-plugin#scale-openshift-deployment}} | |||
==openshiftTag== | |||
{{External|https://github.com/openshift/jenkins-plugin#tag-openshift-image}} | |||
==openshiftVerifyBuild== | |||
{{External|https://github.com/openshift/jenkins-plugin#verify-openshift-build}} | |||
==openshiftVerifyDeployment== | |||
{{External|https://github.com/openshift/jenkins-plugin#verify-openshift-deployment}} | |||
==openshiftVerifyService== | |||
{{External|https://github.com/openshift/jenkins-plugin#verify-openshift-service}} | |||
==openshiftImageStream== | |||
{{External|https://github.com/openshift/jenkins-plugin#imagestreams-scm}} |
Latest revision as of 05:24, 9 December 2017
External
- https://github.com/openshift/jenkins-plugin
- https://github.com/jenkinsci/openshift-pipeline-plugin (associated repository)
Internal
Overview
This is the oldest Jenkins/OpenShift integration plugin, which executes inside Jenkins and implements a series of REST flows that interface with the OpenShift master server via its exposed API. This plugin does not require the oc binary to be present on the host that executes the plugin logic. Other plugin that may be used starting with OpenShift 3.7 is OpenShift Jenkins Pipeline DSL Plugin (jenkins-client-plugin). The "OpenShift Plugin for Jenkins" is currently in maintenance mode.
Manual Build
Note that GIT_SOURCE_URL and GIT_SOURCE_REF must be defined as environment variables in the build configuration, as shown here here
try {
timeout(time: 20, unit: 'MINUTES') {
def project=""
node {
stage("initialize") {
echo "initializing ..."
project = env.PROJECT_NAME
}
}
echo "project: ${project}"
node("maven") {
stage("checkout") {
echo "checking out from ${GIT_SOURCE_URL} ..."
git url: "${GIT_SOURCE_URL}", branch: "${GIT_SOURCE_REF}"
echo "check out ok"
}
stage("build maven") {
echo "building maven artifacts ..."
sh "mvn clean package -Popenshift"
echo "build ok"
stash name:"war", includes:"target/ROOT.war"
}
}
node {
stage("build image") {
echo "building image ..."
unstash name:"war"
sh "oc start-build ${appName}-docker --from-file=target/ROOT.war -n ${project}"
openshiftVerifyBuild bldCfg: "${appName}-docker", namespace: project, waitTime: '20', waitUnit: 'min'
}
stage("Deploy") {
openshiftDeploy deploymentConfig: appName, namespace: project
}
}
}
}
catch (err) {
echo "build failure: ${err}"
currentBuild.result = 'FAILURE'
throw err
}
Jenkins Slave Pods
Jenkins slave pods are configured in Jenkins system configuration as Kubernetes pods. They are invoked as follows:
node("maven") {
//
// this will execute into a Maven slave pod
//
...
}
where the available types are "maven" and "nodejs", and a base pod to build custom slave pods. The pods are instantiated by the Jenkins Kubernetes plugin in the same project as the Jenkins pod that started them. Since they are completely separated pods, they do not use Jenkin's pod resources:
oc get pods NAME READY STATUS RESTARTS AGE jenkins-1-5w13l 1/1 Running 0 1h maven-f45b0fd18d86 1/1 Running 0 21s
Parallel Stages
Pipeline DSL
The pipeline can be scripted or can be specified in a declarative format. A scripted DSL element looks like this:
openshiftBuild(buildConfig: 'something')
A declared DSL element looks like this:
openshiftBuild bldCfg: 'something', verbose: 'true', ...
Optional configuration parameter that apply to all DSL steps are:
- 'apiURL' - the URL of the OpenShift API endpoint. If nothing is specified, the plugin will inspect KUBERNETES_SERVICE_HOST environment variable and if the variable is not set, the default is "https://openshift.default.svc.cluster.local" See Master API.
- 'namespace' - the name of the project the objects the DSL refers to are stored in. If nothing is specified, the plugin will inspect PROJECT_NAME environment variable.
- 'authToken'
- 'verbose' - for verbose logging.
Pipeline DSL Example
node {
stage("build") {
echo "build starting ..."
openshiftBuild bldCfg: 's2i'
openshiftVerifyBuild bldCfg: 's2i'
echo "build done"
}
stage ("deploy") {
echo "deployment starting ..."
openshiftDeploy depCfg: 'test-app'
openshiftVerifyDeployment depCfg: 'test-app', replicaCount: '1'
echo "deployment done"
}
stage ("verify") {
echo "service verification starting ..."
openshiftVerifyService svcName: 'test-app'
echo "service verification done"
}
}
openshiftBuild
Trigger a build based on the specified build configuration. Equivalent with oc start-build.
openshiftBuild bldCfg: 'name_of_the_BuildConfig_to_start'
openshiftDeploy
Trigger a deployment based on the specified deployment configuration.
openshiftDeploy depCfg: 'name_of_the_DeployConfig_to_start'
openshiftExec
Run a command in a pod.
openshiftExec pod: 'name_of_the_pod', command: '...'
openshiftCreateResource
Run a command in a pod.
openshiftExec json|yaml|jsonyaml: '...'