add working Jenkinsfile with prod deploys

This commit is contained in:
Kegan Myers 2020-04-15 01:23:59 -05:00
parent 624962b106
commit a6e83683d5

87
Jenkinsfile vendored
View file

@ -3,62 +3,79 @@ pipeline {
stages {
stage('build') {
steps {
git credentialsId: 'fetch-dht-pull', url: 'git@git.keganmyers.com:terribleplan/fetch-dht.git'
checkout scm
script {
sh 'git rev-parse HEAD > commit'
def gitCommit = readFile('commit').trim()
def kubeCa = "LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUN5RENDQWJDZ0F3SUJBZ0lCQURBTkJna3Foa2lHOXcwQkFRc0ZBREFWTVJNd0VRWURWUVFERXdwcmRXSmwKY201bGRHVnpNQjRYRFRJd01ETXlNVEU0TkRrd05Wb1hEVE13TURNeE9URTRORGt3TlZvd0ZURVRNQkVHQTFVRQpBeE1LYTNWaVpYSnVaWFJsY3pDQ0FTSXdEUVlKS29aSWh2Y05BUUVCQlFBRGdnRVBBRENDQVFvQ2dnRUJBTklWClJoaDMzVVd2Yk4vS0VzZTFSY2tkM0R0VitmR251RC9yOVhmdU1BRThzaW9vT1gwQjVzcjdOVi9TWFA2dDB0ak8KQkRTRnJpODczejd0SWtMcktvNmFWNGpOcDdNUXNDbllydk9ob0EwWnJIdmpmbUljbFlXRnhwclNWWkFRZ1JpbgpiU2VRMkY2d3MwdnlTSjluMy8zTmV2RlRHT0V5WVBjWko0cGZCNjJnWkk5OFR6ZU9NWTV0QWJRQmJsRW0wdGx6ClVJSWJYTmhCVXNnWnZxU3dwTG5LMHorUVBsdkRRd0FLRCt6dmNmdlZaQk1tNGZ1bFprYkxHdGNUbUtBcVhQMWQKbGtkMUxYN205cEVoUDhKVGUvVW9WWWh5ZTRRNG0xbWQvTHgrNXJUTEg0TXBjYkV0dTR0MklPZjZTZ0l2WkJ0dwpIbnRuVG1HMTdQdjJUUzJ4WWEwQ0F3RUFBYU1qTUNFd0RnWURWUjBQQVFIL0JBUURBZ0trTUE4R0ExVWRFd0VCCi93UUZNQU1CQWY4d0RRWUpLb1pJaHZjTkFRRUxCUUFEZ2dFQkFBYkRpOXZNT085akN0WGU5WCtHNDZGdkpnVEgKMkVQb2NOUDdIcHZGNFYzQVd4eWRSaGZSK2o1eHRldG1sem5uNktjVE41WUVoenE2VXVGMlpPS1NjVUp0Yi8ydgpnQVhCTjF3SVdSZ0p2bWdVTTRLVVVwc1FpWjRsY0o1TS9IT1ByZmJrL21pdERzd01XZDlIVTZ3ZVN4NVJ6eVFGCmEvNVhtSGlSUTZRc1Q2SnovMGNIQmNCbjMwRkZCTzVudVdRMjRnVjNBTlE2NXUvUFhkalBwRExBT3d6bmV4QmYKRUZLMk1aNC9Nd0xNUjlDcGk3TWovWHUwVkxLTWhUWVVCSWQwQ0EwMllKems1RzVSbmpZVlRxWnp3L2QvaUVXQQpta1k3d1Y2d0tUV25jRDM1MVlmWjJtWGtGa293WGVDL1lHVVpEQmUza0hlQTJRVFFkMmEzMEt6dnpvOD0KLS0tLS1FTkQgQ0VSVElGSUNBVEUtLS0tLQo="
def kubeUrl = "https://45.79.61.115:443"
def appName = "fetch-dht"
def imagePullSecret = "registry-terrible-netowrk-pull-secret" // typo is purposeful
def kubectl = docker.image("bitnami/kubectl:1.18")
def registryHost = "registry.terrible.network"
def imageName = "${registryHost}/terribleplan/${appName}"
def betaImageName = "${imageName}:beta"
// build
def app = docker.build("${imageName}:${gitCommit}")
def betaTag = "beta"
def betaImageName = "${imageName}:${betaTag}"
def betaAppName = "${appName}-beta"
def betaHost = "${betaAppName}.terrible.network"
def beta
// beta push
docker.withRegistry("https://${registryHost}", "registry-terrible-network") {
beta = docker.build(betaImageName)
beta.inside {
sh "cp package.json /app/package.json"
sh "cp yarn.lock /app/yarn.lock"
sh "cd /app && yarn"
sh "cd /app && ls"
sh "cd /app/lib && ls"
sh "cd /app/lib/binaryQuerystring && ls"
sh "cd /app && node lib/binaryQuerystring/index.test.js"
}
beta.push()
app.push(betaTag)
}
// beta deploy
kubectl.inside("--entrypoint=''") {
env.NAMESPACE = betaAppName
env.HOST = betaHost
env.APP_NAME = betaAppName
env.IMAGE =
env.IMAGE_PULL_SECRET = "registry-terrible-network-pull-secret"
sh "set -eu"
sh "install_packages gettext"
sh "set -euo pipefail"
sh "set -eu"
sh "mkdir -p ~/.kube"
withCredentials([string(credentialsId: 'fetch-dht-beta-token', variable: 'token')]) {
sh "cat deploy/kubeconfig.envsubst.yml | CA='${kubeCa}' URL='${kubeUrl}' TOKEN='${token}' envsubst '\$CA,\$URL,\$TOKEN,\$NAMESPACE' > ~/.kube/config"
}
sh "cat deploy/manifest.envsubst.yml | IMAGE='${beta.imageName()}' APP_NAME='${betaAppName}' REVISION='${env.GIT_COMMIT}' envsubst '\${NAMESPACE}' | kubectl apply -f -"
sh "cat deploy/manifest.envsubst.yml | HOST='${betaHost}' IMAGE_PULL_SECRET='${imagePullSecret}' IMAGE='${betaImageName}' APP_NAME='${betaAppName}' REVISION='${gitCommit}' envsubst '\$NAMESPACE,\$HOST,\$IMAGE_PULL_SECRET,\$IMAGE,\$APP_NAME,\$REVISION' | kubectl apply -f -"
sh "kubectl rollout status 'deployments/${betaAppName}'"
}
// beta integration
app.inside {
sh "set -eu"
sh "cp package.json /app/package.json"
sh "cp yarn.lock /app/yarn.lock"
sh "cd /app && yarn"
sh "cd /app && ENDPOINT='https://${betaHost}' node integration.js"
}
// def prodImageName = "${imageName}:latest"
// beta.push(prodImageName)
// beta.run("-e ENDPOINT=https://${betaHost}", "yarn && yarn integration")
// latest = beta.tag("latest")
// latest.push()
// kubectl.inside {
// env.NAMESPACE = appName
// env.HOST = "dht.terrible.network"
// env.APP_NAME = appName
// env.IMAGE = latest.imageName()
// env.IMAGE_PULL_SECRET = "registry-terrible-network-pull-secret"
// sh "kubectl rollout restart 'deployments/${env.APP_NAME}'"
// sh "kubectl -n '${env.NAMESPACE}' rollout status 'deployments/${env.APP_NAME}'"
// }
def prodTag = "latest"
def prodImageName = "${imageName}:${prodTag}"
def prodHost = "dht.terrible.network"
// push
docker.withRegistry("https://${registryHost}", "registry-terrible-network") {
app.push(prodTag)
}
// deploy
kubectl.inside("--entrypoint=''") {
env.NAMESPACE = appName
sh "set -eu"
sh "install_packages gettext"
sh "set -eu"
sh "mkdir -p ~/.kube"
withCredentials([string(credentialsId: 'fetch-dht-prod-token', variable: 'token')]) {
sh "cat deploy/kubeconfig.envsubst.yml | CA='${kubeCa}' URL='${kubeUrl}' TOKEN='${token}' envsubst '\$CA,\$URL,\$TOKEN,\$NAMESPACE' > ~/.kube/config"
}
sh "cat deploy/manifest.envsubst.yml | HOST='${prodHost}' IMAGE_PULL_SECRET='${imagePullSecret}' IMAGE='${prodImageName}' APP_NAME='${appName}' REVISION='${gitCommit}' envsubst '\$NAMESPACE,\$HOST,\$IMAGE_PULL_SECRET,\$IMAGE,\$APP_NAME,\$REVISION' | kubectl apply -f -"
sh "kubectl rollout status 'deployments/${appName}'"
}
// integration
app.inside {
sh "set -eu"
sh "cp package.json /app/package.json"
sh "cp yarn.lock /app/yarn.lock"
sh "cd /app && yarn"
sh "cd /app && ENDPOINT='https://${prodHost}' node integration.js"
}
}
}
}