diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..13cd16c --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +.terraform/ +outputs/ +terraform.tfstate.d/ +*.out +*.plan +.DS_Store \ No newline at end of file diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..d2c1341 --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,56 @@ +library( + identifier:'pipeline-lib@4.3.1', + retriever:modernSCM([$class:'GitSCMSource', + remote:'https://github.com/SmartColumbusOS/pipeline-lib', + credentialsId:'jenkins-github-user']) +) + +properties([ + pipelineTriggers([scos.dailyBuildTrigger()]), + parameters([ + booleanParam(defaultValue: false, description: 'Deploy to development environment?', name: 'DEV_DEPLOYMENT'), + string(defaultValue: 'development', description: 'Image tag to deploy to dev environment', name: 'DEV_IMAGE_TAG') + ]) +]) + +def image, imageName +def doStageIf = scos.&doStageIf +def doStageIfDeployingToDev = doStageIf.curry(env.DEV_DEPLOYMENT == "true") +def doStageIfMergedToMaster = doStageIf.curry(scos.changeset.isMaster && env.DEV_DEPLOYMENT == "false") +def doStageIfRelease = doStageIf.curry(scos.changeset.isRelease) + +node ('infrastructure') { + ansiColor('xterm') { + scos.doCheckoutStage() + + doStageIfDeployingToDev('Deploy to Dev') { + deployTo('dev', "--set image.tag=${env.DEV_IMAGE_TAG} --recreate-pods") + } + + doStageIfMergedToMaster('Process Dev job') { + scos.devDeployTrigger('micro-service-watchinator') + } + + doStageIfMergedToMaster('Deploy to Staging') { + deployTo('staging') + scos.applyAndPushGitHubTag('staging') + } + + doStageIfRelease('Deploy to Production') { + deployTo('prod') + scos.applyAndPushGitHubTag('prod') + } + } +} + + +def deployTo(environment, extraHelmCommandArgs = '') { + def extraVars = [ + 'extraHelmCommandArgs': extraHelmCommandArgs + ] + + def terraform = scos.terraform(environment) + sh "terraform init && terraform workspace new ${environment}" + terraform.plan(terraform.defaultVarFile, extraVars) + terraform.apply() +} diff --git a/README.md b/README.md index 676c423..249ba5c 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,12 @@ # micro_service_watchinator-deploy Deploy repository for micro_service_watchinator + + +# Deploying Via Terraform +``` +env=dev +terraform init -backend-config=../common/backends/alm.conf +terraform workspace new $env +terraform plan --var-file=variables/$env.tfvars -var 'watchinator_image_name=199837183662.dkr.ecr.us-east-2.amazonaws.com/scos/micro-service-watchinator:' --out=my.out +terraform apply my.out +``` \ No newline at end of file diff --git a/main.tf b/main.tf new file mode 100644 index 0000000..24b7577 --- /dev/null +++ b/main.tf @@ -0,0 +1,69 @@ +data "terraform_remote_state" "env_remote_state" { + backend = "s3" + workspace = "${terraform.workspace}" + + config { + bucket = "${var.alm_state_bucket_name}" + key = "operating-system" + region = "us-east-2" + role_arn = "${var.alm_role_arn}" + } +} + +resource "local_file" "kubeconfig" { + filename = "${path.module}/outputs/kubeconfig" + content = "${data.terraform_remote_state.env_remote_state.eks_cluster_kubeconfig}" +} + +resource "local_file" "helm_vars" { + filename = "${path.module}/outputs/${terraform.workspace}.yaml" + + content = <