Skip to content
Open
45 changes: 45 additions & 0 deletions Jenkinsfile
Original file line number Diff line number Diff line change
@@ -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
}
}
32 changes: 32 additions & 0 deletions Jenkinsfile-canary
Original file line number Diff line number Diff line change
@@ -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
}
32 changes: 32 additions & 0 deletions Jenkinsfile-production
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
28 changes: 28 additions & 0 deletions k8s/canary/python-api.yaml
Original file line number Diff line number Diff line change
@@ -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
28 changes: 28 additions & 0 deletions k8s/production/python-api.yaml
Original file line number Diff line number Diff line change
@@ -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
32 changes: 32 additions & 0 deletions k8s/production/samplewebapp.yaml.old
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions k8s/services/python-api-svc.yaml
Original file line number Diff line number Diff line change
@@ -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