diff --git a/Jenkinsfile b/Jenkinsfile index 58c734b2b..402fd6676 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,11 +1,17 @@ +agentName = "server-slave" + pipeline { - agent any + agent none environment { //be sure to replace "willbla" with your own Docker Hub username - DOCKER_IMAGE_NAME = "willbla/train-schedule" + MYIMAGE = "$DOCKER_IMAGE_NAME:$BUILD_NUMBER" + DOCKER_IMAGE_NAME = "shdh/train-schedule" + VERSION = "2" + } stages { stage('Build') { + agent { label agentName } steps { echo 'Running build automation' sh './gradlew build --no-daemon' @@ -14,8 +20,9 @@ pipeline { } stage('Build Docker Image') { when { - branch 'master' + branch 'development' } + agent { label agentName } steps { script { app = docker.build(DOCKER_IMAGE_NAME) @@ -27,8 +34,9 @@ pipeline { } stage('Push Docker Image') { when { - branch 'master' + branch 'development' } + agent { label agentName } steps { script { docker.withRegistry('https://registry.hub.docker.com', 'docker_hub_login') { @@ -38,15 +46,62 @@ pipeline { } } } + stage('DeployToDev') { + when { + branch 'development' + } + agent { label agentName } + environment { + NAMESPACE = "dev" + NODEPORT = "30050" + } + steps { + kubernetesDeploy( + kubeconfigId: 'kube', + configs: 'train-schedule-kube.yml', + enableConfigSubstitution: true + ) + } + } + stage('DeployToCT') { + when { + branch 'development' + } + agent { label agentName } + environment { + NAMESPACE = "ct" + NODEPORT = "30060" + MYIMAGE = "$DOCKER_IMAGE_NAME:$VERSION" + } + steps { + input 'Deploy to CT?' + milestone(1) + kubernetesDeploy( + kubeconfigId: 'kube', + configs: 'train-schedule-kube.yml', + enableConfigSubstitution: true + ) + } + } stage('DeployToProduction') { when { branch 'master' } + agent { label agentName } + environment { + NAMESPACE = "prod" + NODEPORT = "30070" + MYIMAGE = "$DOCKER_IMAGE_NAME:$VERSION" + } steps { input 'Deploy to Production?' milestone(1) - //implement Kubernetes deployment here + kubernetesDeploy( + kubeconfigId: 'kube', + configs: 'train-schedule-kube.yml', + enableConfigSubstitution: true + ) } } } -} \ No newline at end of file +} diff --git a/README.md b/README.md index 2da1e91c9..12ae8781b 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # cicd-pipeline-train-schedule-kubernetes +# DEV, CT, PROD + This is a simple train schedule app written using nodejs. It is intended to be used as a sample application for a series of hands-on learning activities. ## Running the app diff --git a/train-schedule-kube.yml b/train-schedule-kube.yml new file mode 100644 index 000000000..78fd6dc4c --- /dev/null +++ b/train-schedule-kube.yml @@ -0,0 +1,58 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: mynamespace + namespace: $NAMESPACE +data: + namespace: $NAMESPACE + +--- + +kind: Service +apiVersion: v1 +metadata: + name: train-schedule-service + namespace: $NAMESPACE +spec: + type: NodePort + selector: + app: train-schedule + ports: + - protocol: TCP + port: 8080 + targetPort: 8080 + nodePort: $NODEPORT + +--- + +apiVersion: extensions/v1beta1 +kind: Deployment +metadata: + name: train-schedule-deployment + namespace: $NAMESPACE + labels: + app: train-schedule +spec: + replicas: 2 + selector: + matchLabels: + app: train-schedule + template: + metadata: + labels: + app: train-schedule + spec: + containers: + - name: train-schedule + image: $MYIMAGE + env: + # Define the environment variable + - name: NAMESPACE + valueFrom: + configMapKeyRef: + # The ConfigMap containing the value you want to assign to SPECIAL_LEVEL_KEY + name: mynamespace + # Specify the key associated with the value + key: namespace + ports: + - containerPort: 8080