diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..931511d --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,45 @@ +node { + def project = 'snsumner75' + def appName = 'python_api' + def feSvcName = "${appName}" + def namespace = 'production' + def imageTag = "quay.io/${project}/${appName}:${env.BRANCH_NAME}.v${env.BUILD_NUMBER}" + checkout scm + + stage 'Printenv' + sh("printenv") + + stage 'Login to Quay.io' + sh("docker login -u=\"${env.quay_username}\" -p=\"${env.quay_password}\" quay.io") + + stage 'Build image' + sh("docker build -t ${imageTag} .") + +// stage 'Run Go tests ' +// sh("docker run ${imageTag} go test") + + stage 'Push image to Quay.io registry' + sh("docker push ${imageTag}") + + stage "Deploy Application" + switch (env.BRANCH_NAME) { + case "canary": + // Roll out to canary environment + // Change deployed image in canary to the one we just built + sh("sed -i.bak 's#quay.io/${project}/${appName}:.*\$#${imageTag}#' ./k8s/canary/*.yaml") + sh("kubectl --namespace=${namespace} apply -f k8s/services/") + sh("kubectl --namespace=${namespace} apply -f k8s/canary/") + break + + case "production": + // Roll out to production environment + // Change deployed image in canary to the one we just built + sh("sed -i.bak 's#quay.io/${project}/${appName}:.*\$#${imageTag}#' ./k8s/production/*.yaml") + sh("kubectl --namespace=${namespace} apply -f k8s/services/") + sh("kubectl --namespace=${namespace} apply -f k8s/production/") + break + + default: + break + } +} diff --git a/Jenkinsfile-canary b/Jenkinsfile-canary new file mode 100644 index 0000000..e74c17a --- /dev/null +++ b/Jenkinsfile-canary @@ -0,0 +1,32 @@ +node { + def project = 'chris_ricci' + def appName = 'python_api' + def feSvcName = "${appName}" + def namespace = 'production' + def imageTag = "quay.io/${project}/${appName}:canary.v${env.BUILD_NUMBER}" + checkout scm + + stage 'Printenv' + sh("printenv") + + stage 'Login to Quay.io' + sh("docker login -u=\"${env.quay_username}\" -p=\"${env.quay_password}\" quay.io") + + stage 'Build image' + sh("docker build -t ${imageTag} .") + +// stage 'Run Go tests ' +// sh("docker run ${imageTag} go test") + + stage 'Push image to Quay.io registry' + sh("docker push ${imageTag}") + + stage "Deploy Application" + // Roll out to canary environment + // Change deployed image in canary to the one we just built + sh("sed -i.bak 's#quay.io/${project}/${appName}:.*\$#${imageTag}#' ./k8s/canary/*.yaml") + sh("kubectl --namespace=${namespace} apply -f k8s/services/") + sh("kubectl --namespace=${namespace} apply -f k8s/canary/") + //sh("echo http://`kubectl --namespace=${namespace} get service/${feSvcName} --output=json | jq -r '.status.loadBalancer.ingress[0].ip'` > ${feSvcName}") + //break +} diff --git a/Jenkinsfile-production b/Jenkinsfile-production new file mode 100644 index 0000000..f864a8d --- /dev/null +++ b/Jenkinsfile-production @@ -0,0 +1,32 @@ +node { + def project = 'chris_ricci' + def appName = 'python_api' + def feSvcName = "${appName}" + def namespace = 'production' + def imageTag = "quay.io/${project}/${appName}:production.v${env.BUILD_NUMBER}" + checkout scm + + stage 'Printenv' + sh("printenv") + + stage 'Login to Quay.io' + sh("docker login -u=\"${env.quay_username}\" -p=\"${env.quay_password}\" quay.io") + + stage 'Build image' + sh("docker build -t ${imageTag} .") + +// stage 'Run Go tests ' +// sh("docker run ${imageTag} go test") + + stage 'Push image to Quay.io registry' + sh("docker push ${imageTag}") + + stage "Deploy Application" + // Roll out to canary environment + // Change deployed image in production to the one we just built + sh("sed -i.bak 's#quay.io/${project}/${appName}:.*\$#${imageTag}#' ./k8s/production/*.yaml") + sh("kubectl --namespace=${namespace} apply -f k8s/services/") + sh("kubectl --namespace=${namespace} apply -f k8s/production/") + //sh("echo http://`kubectl --namespace=${namespace} get service/${feSvcName} --output=json | jq -r '.status.loadBalancer.ingress[0].ip'` > ${feSvcName}") + //break +} diff --git a/app/views.py b/app/views.py index 5603900..6063b5c 100644 --- a/app/views.py +++ b/app/views.py @@ -5,4 +5,4 @@ @app.route('/index') def index(): timestamp = str(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')) - return timestamp + " Hello, World!\n" + return timestamp + " Hello, World! - Version 1.0\n" diff --git a/k8s/canary/python-api.yaml b/k8s/canary/python-api.yaml new file mode 100644 index 0000000..137a941 --- /dev/null +++ b/k8s/canary/python-api.yaml @@ -0,0 +1,28 @@ +apiVersion: extensions/v1beta1 +kind: Deployment +metadata: + labels: + app: python-api + env: canary + name: python-api-canary +spec: + replicas: 1 + template: + metadata: + labels: + app: python-api + env: canary + spec: + containers: + - image: quay.io/snsumner75/python_api:v1 + imagePullPolicy: IfNotPresent + name: python-api + ports: + - containerPort: 5000 + protocol: TCP + resources: {} + terminationMessagePath: /dev/termination-log + dnsPolicy: ClusterFirst + restartPolicy: Always + securityContext: {} + terminationGracePeriodSeconds: 30 diff --git a/k8s/production/python-api.yaml b/k8s/production/python-api.yaml new file mode 100644 index 0000000..07b2483 --- /dev/null +++ b/k8s/production/python-api.yaml @@ -0,0 +1,28 @@ +apiVersion: extensions/v1beta1 +kind: Deployment +metadata: + labels: + app: python-api + env: production + name: python-api-production +spec: + replicas: 4 + template: + metadata: + labels: + app: python-api + env: production + spec: + containers: + - image: quay.io/snsumner75/python_api:v1 + imagePullPolicy: IfNotPresent + name: python-api + ports: + - containerPort: 5000 + protocol: TCP + resources: {} + terminationMessagePath: /dev/termination-log + dnsPolicy: ClusterFirst + restartPolicy: Always + securityContext: {} + terminationGracePeriodSeconds: 30 diff --git a/k8s/production/samplewebapp.yaml.old b/k8s/production/samplewebapp.yaml.old new file mode 100644 index 0000000..2466a56 --- /dev/null +++ b/k8s/production/samplewebapp.yaml.old @@ -0,0 +1,32 @@ +apiVersion: extensions/v1beta1 +kind: Deployment +metadata: + labels: + app: simplewebapp + env: production + name: simplewebapp +spec: + replicas: 3 + selector: + matchLabels: + app: simplewebapp + env: production + strategy: + rollingUpdate: + maxSurge: 1 + maxUnavailable: 1 + type: RollingUpdate + spec: + containers: + - image: quay.io/chris_ricci/simplewebapp:v21 + imagePullPolicy: IfNotPresent + name: simplewebapp + ports: + - containerPort: 80 + protocol: TCP + resources: {} + terminationMessagePath: /dev/termination-log + dnsPolicy: ClusterFirst + restartPolicy: Always + securityContext: {} + terminationGracePeriodSeconds: 30 diff --git a/k8s/services/python-api-svc.yaml b/k8s/services/python-api-svc.yaml new file mode 100644 index 0000000..be34e67 --- /dev/null +++ b/k8s/services/python-api-svc.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Service +metadata: + name: python-api +spec: + ports: + - port: 80 + protocol: TCP + targetPort: 5000 + selector: + app: python-api + type: LoadBalancer