Last active
July 7, 2020 07:27
-
-
Save ralvares/b7748eadfdb1611d46bc28d4d856d3f1 to your computer and use it in GitHub Desktop.
dotnet-cicd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
oc new-app jenkins-ephemeral | |
or | |
oc new-app jenkins-persistent | |
Create a file dotnet-jenkins-slave.yaml with the below content: | |
kind: ConfigMap | |
apiVersion: v1 | |
metadata: | |
name: jenkins-agent-dotnet31 | |
labels: | |
role: jenkins-slave | |
data: | |
dotnet31: |- | |
<org.csanchez.jenkins.plugins.kubernetes.PodTemplate> | |
<inheritFrom></inheritFrom> | |
<name>dotnet31</name> | |
<instanceCap>2247483647</instanceCap> | |
<idleMinutes>0</idleMinutes> | |
<label>dotnet31</label> | |
<serviceAccount>jenkins</serviceAccount> | |
<nodeSelector></nodeSelector> | |
<volumes/> | |
<containers> | |
<org.csanchez.jenkins.plugins.kubernetes.ContainerTemplate> | |
<name>jnlp</name> | |
<image>registry.redhat.io/dotnet/dotnet-31-jenkins-slave-rhel7</image> | |
<privileged>false</privileged> | |
<alwaysPullImage>true</alwaysPullImage> | |
<workingDir>/tmp</workingDir> | |
<command></command> | |
<args>${computer.jnlpmac} ${computer.name}</args> | |
<ttyEnabled>false</ttyEnabled> | |
<resourceRequestCpu></resourceRequestCpu> | |
<resourceRequestMemory></resourceRequestMemory> | |
<resourceLimitCpu></resourceLimitCpu> | |
<resourceLimitMemory></resourceLimitMemory> | |
<envVars/> | |
</org.csanchez.jenkins.plugins.kubernetes.ContainerTemplate> | |
</containers> | |
<envVars/> | |
<annotations/> | |
<imagePullSecrets/> | |
<nodeProperties/> | |
</org.csanchez.jenkins.plugins.kubernetes.PodTemplate> | |
oc apply -f dotnet-jenkins-slave.yaml | |
Create a file dotnet-jenkins-pipeline.yaml with the below content: | |
apiVersion: build.openshift.io/v1 | |
kind: BuildConfig | |
metadata: | |
name: dotnet-demo-pipeline | |
spec: | |
strategy: | |
jenkinsPipelineStrategy: | |
jenkinsfile: |- | |
// Don't forget to run the commands to create the dev project, and grant the needed roles to the service accounts | |
def BUILD_NAME = APP_NAME | |
pipeline { | |
agent { | |
label 'dotnet31' | |
} | |
stages { | |
stage('Checkout') { | |
steps { | |
git url: "${GIT_URL}", branch: "${GIT_REF}" | |
} | |
} | |
stage('Build') { | |
steps { | |
dir("${CONTEXT_DIR}") { | |
sh "dotnet publish -c Release" | |
} | |
} | |
} | |
stage('Test') { | |
steps { | |
dir("${CONTEXT_DIR}") { | |
sh "dotnet test" | |
} | |
} | |
} | |
stage('Create Image Builder') { | |
when { | |
expression { | |
openshift.withCluster() { | |
return !openshift.selector("bc", "${BUILD_NAME}").exists(); | |
} | |
} | |
} | |
steps { | |
script { | |
openshift.withCluster() { | |
openshift.newBuild("--name=${BUILD_NAME}", "--image-stream=${BUILD_IMAGE_STREAM}", "--binary") | |
} | |
} | |
} | |
} | |
stage('Build Image') { | |
steps { | |
script { | |
dir("${CONTEXT_DIR}") { | |
openshift.withCluster() { | |
openshift.selector("bc", "${BUILD_NAME}").startBuild("--from-dir=bin/Release/netcoreapp3.1/publish", "--wait") | |
} | |
} | |
} | |
} | |
} | |
stage('Deploy') { | |
when { | |
expression { | |
openshift.withCluster() { | |
return !openshift.selector('dc', "${APP_NAME}").exists() | |
} | |
} | |
} | |
steps { | |
script { | |
openshift.withCluster() { | |
openshift.newApp("${BUILD_NAME}:latest", "--name=${APP_NAME}").narrow('svc').expose() | |
def partOfLabel = openshift.raw( "label dc/${APP_NAME} app.kubernetes.io/part-of=${APP_NAME}-app" ) | |
echo "After label part-of partOfLabel: ${partOfLabel.out}" | |
} | |
} | |
} | |
} | |
} | |
} | |
env: | |
- name: APP_NAME | |
value: "dotnet-service" | |
- name: APP_VERSION | |
value: "1.0.0" | |
- name: GIT_URL | |
value: "https://github.com/redhat-developer/s2i-dotnetcore-ex" | |
- name: GIT_REF | |
value: "dotnetcore-3.1" | |
- name: CONTEXT_DIR | |
value: "app" | |
- name: BUILD_IMAGE_STREAM | |
value: "dotnet:3.1" | |
type: JenkinsPipeline | |
oc apply -f dotnet-jenkins-pipeline.yaml | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment