Skip to content

Instantly share code, notes, and snippets.

@mousetree
Last active April 16, 2018 19:10
Show Gist options
  • Save mousetree/6bb4b12bb7ef034c4f8a844350318a39 to your computer and use it in GitHub Desktop.
Save mousetree/6bb4b12bb7ef034c4f8a844350318a39 to your computer and use it in GitHub Desktop.
Kubernetes deployment scripts
#!/usr/bin/env bash
set -e
# Get the version number from package.json
VERSION=$(node -e "console.log(require('./package.json').version);")
# Get the build number from Jenkins
BUILD=${BUILD_NUMBER:-local}
TAG="v${VERSION}-${BUILD}"
echo "Building my-app ${TAG}..."
# Save the tag for later use
echo $TAG > build_id.txt
# Create production minified version of the code
export REACT_APP_BUILD_ID=$TAG
rm -rf build/
npm run build
# Build docker and copy in code
docker build -t my-app .
# Tag the docker build with the correct version
docker login -u username -p password mycompany.registry.com
docker tag my-app mycompany.registry.com/my-app:${TAG}
docker push mycompany.registry.com/my-app:${TAG}
echo "Build complete!"
#!/usr/bin/env bash
POD_NAME=my-app
NAMESPACE=staging
echo "Checking deploy status for ${POD_NAME}..."
COUNTER=0
while [ $COUNTER -lt 30 ]; do
let COUNTER=COUNTER+1
TOTALPODS=`~/.kube/kubectl get pods -n ${NAMESPACE} | grep ${POD_NAME} | wc -l`
RUNNINGPODS=`~/.kube/kubectl get pods -n ${NAMESPACE} | grep ${POD_NAME} | grep Running | wc -l`
echo "Check #${COUNTER}: ${TOTALPODS} Total Pods — ${RUNNINGPODS} Running Pods"
if [ "$TOTALPODS" -eq "$RUNNINGPODS" ]; then
echo "${POD_NAME} was successfully updated."
break
else
sleep 10
false
fi
done
#!/usr/bin/env bash
set -e
TAG=`cat build_id.txt`
echo "Deploying ${TAG}..."
sed -i.bak "s/__BUILD_NUM__/$TAG/g" deploy.yml
# Deploy to Staging
~/.kube/kubectl apply -n staging -f deploy.yml
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
/bin/bash $DIR/check_deploy.sh
echo "Deploy complete!"
apiVersion: v1
kind: Service
metadata:
name: my-app
spec:
ports:
- port: 80
selector:
app: my-app
type: ClusterIP
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 2
revisionHistoryLimit: 5
template:
metadata:
labels:
app: my-app
spec:
containers:
- image: mycompany.registry.com/my-app:__BUILD_NUM__
name: my-app
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
livenessProbe:
httpGet:
path: /
port: 80
initialDelaySeconds: 3
periodSeconds: 30
readinessProbe:
httpGet:
path: /
port: 80
initialDelaySeconds: 3
periodSeconds: 30
imagePullSecrets:
- name: registrykey
pipeline {
agent any
stages {
stage('Install') {
steps {
sh 'npm install'
}
}
stage('Test') {
steps {
sh 'npm run test -- --coverage'
}
}
stage('Build') {
when { expression { env.BRANCH_NAME == 'master' } }
steps {
sh './scripts/build.sh'
}
}
stage('Deploy') {
when { expression { env.BRANCH_NAME == 'master' } }
steps {
sh './scripts/deploy.sh'
}
post {
success {
slackSend channel: '#builds',
color: 'good',
message: "${env.JOB_NAME} build #${env.BUILD_ID} was successfully deployed to staging"
}
}
}
}
post {
failure {
slackSend channel: '#builds',
color: 'bad',
message: "${env.JOB_NAME} build #${env.BUILD_ID} failed! See ${env.BUILD_URL}"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment