From 507549cac33cc5dd7cc09c0e5bb4b180d708cd59 Mon Sep 17 00:00:00 2001 From: gavinfish Date: Thu, 17 Oct 2019 14:01:43 +0800 Subject: [PATCH] Temp --- Dockerfile | 6 ++ Jenkinsfile | 72 ++++++++++++++----- Jenkinsfile-bluegreen | 48 ------------- infra/Jenkinsfile | 53 ++++++++++++++ infra/ansible/main.yaml | 12 ++++ infra/ansible/setenv.yaml | 14 ++++ infra/kube/namespaces/production.yaml | 4 ++ infra/kube/namespaces/staging.yaml | 4 ++ .../kube/workloads/production/deployment.yaml | 0 infra/kube/workloads/production/service.yaml | 0 infra/kube/workloads/staging/deployment.yaml | 24 +++++++ infra/kube/workloads/staging/service.yaml | 14 ++++ infra/terraform/k8s.tf | 44 ++++++++++++ infra/terraform/main.tf | 6 ++ infra/terraform/output.tf | 27 +++++++ infra/terraform/variables.tf | 30 ++++++++ 16 files changed, 291 insertions(+), 67 deletions(-) create mode 100644 Dockerfile delete mode 100644 Jenkinsfile-bluegreen create mode 100644 infra/Jenkinsfile create mode 100644 infra/ansible/main.yaml create mode 100644 infra/ansible/setenv.yaml create mode 100644 infra/kube/namespaces/production.yaml create mode 100644 infra/kube/namespaces/staging.yaml create mode 100644 infra/kube/workloads/production/deployment.yaml create mode 100644 infra/kube/workloads/production/service.yaml create mode 100644 infra/kube/workloads/staging/deployment.yaml create mode 100644 infra/kube/workloads/staging/service.yaml create mode 100644 infra/terraform/k8s.tf create mode 100644 infra/terraform/main.tf create mode 100644 infra/terraform/output.tf create mode 100644 infra/terraform/variables.tf diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 000000000..1dda1f441 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,6 @@ +FROM java:8-jre + +ADD ./target/pet-clinic.jar /app/ +CMD ["java", "-Xmx200m", "-jar", "/app/pet-clinic.jar"] + +EXPOSE 8080 diff --git a/Jenkinsfile b/Jenkinsfile index e786f9191..4687b7bce 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,22 +1,56 @@ -node('master') { - stage('init') { - checkout scm - } +pipeline { + agent any - stage('image build') { - sh ''' - ./mvnw clean package - cd target - mv *.jar petclinic.jar - cp ../web.config web.config - zip petclinic.zip web.config petclinic.jar - ''' - } + stages { + stage('init') { + steps { + git url: "git@github.com:azure-devops/spring-petclinic.git", + credentialsId: "github_ssh_key", + branch: "ignite" + } + } - stage('deploy') { - azureWebAppPublish appName: env.APP_NAME, - azureCredentialsId: env.CRED_ID, - resourceGroup: env.RESOURCE_GROUP, - filePath: 'target/*.zip' + stage('build') { + steps { + sh ''' + ./mvnw clean package + mv target/*.jar target/pet-clinic.jar + ''' + } + } + + +// stage('image build') { +// environment { +// sha = sh(script: 'git rev-parse --short HEAD', returnStdout: true) +// } +// +// steps { +// acrQuickTask azureCredentialsId: "jenkins-sp", +// registryName: "jenkinsdemosacr", +// resourceGroupName: "demo-aks", +// local: "", +// dockerfile: "Dockerfile", +// imageNames: [[image: "jenkinsdemosacr.azurecr.io/pet-clinic:master-${sha}"]] +// } +// } + + stage('update staging config') { + environment { + sha = sh(script: 'git rev-parse --short HEAD', returnStdout: true).trim() + } + + steps { + dir('infra/kube/workloads/staging') { + sh ''' + sed -i -e "s/master-......./master-\${sha}/" deployment.yaml + sed -i -e "s/master-......./master-\${sha}/" service.yaml + git add * + git commit -m "Update staging file with \${sha} commit" + git push origin ignite + ''' + } + } + } } -} \ No newline at end of file +} diff --git a/Jenkinsfile-bluegreen b/Jenkinsfile-bluegreen deleted file mode 100644 index 6bab99a25..000000000 --- a/Jenkinsfile-bluegreen +++ /dev/null @@ -1,48 +0,0 @@ -def userInput - -node('master') { - stage('init') { - checkout scm - } - - stage('image build') { - sh ''' - ./mvnw clean package - cd target - mv *.jar petclinic.jar - cp ../web.config web.config - zip petclinic.zip web.config petclinic.jar - ''' - } - - stage('preview') { - azureWebAppPublish appName: env.APP_NAME, - azureCredentialsId: env.CRED_ID, - resourceGroup: env.RESOURCE_GROUP, - filePath: 'target/*.zip', - slotName: 'preview' - } - - stage('confirm swap slots') { - try { - userInput = input( - id: 'Proceed1', message: 'Do you want to swap slots?', parameters: [ - [$class: 'BooleanParameterDefinition', defaultValue: true, description: '', name: 'Please confirm you want to swap the slots'] - ]) - } catch(err) { // input false - echo "Aborted" - } - } - - if (userInput == true) { - stage('swap slots') { - azureWebAppSwapSlots appName: env.APP_NAME, - azureCredentialsId: env.CRED_ID, - resourceGroup: env.RESOURCE_GROUP, - sourceSlotName: 'production', - targetSlotName: 'preview' - } - } else { - // Send a notification - } -} \ No newline at end of file diff --git a/infra/Jenkinsfile b/infra/Jenkinsfile new file mode 100644 index 000000000..d45448900 --- /dev/null +++ b/infra/Jenkinsfile @@ -0,0 +1,53 @@ +pipeline { + agent any + + stages { + stage('init') { + steps { + checkout scm + } + } + + stage('init_and_plan') { + steps { + dir('infra/terraform') { + sh "terraform init" + withCredentials([azureServicePrincipal(credentialsId: 'jenkins-sp', + subscriptionIdVariable: 'ARM_SUBSCRIPTION_ID', + clientIdVariable: 'ARM_CLIENT_ID', + clientSecretVariable: 'ARM_CLIENT_SECRET', + tenantIdVariable: 'ARM_TENANT_ID')]) { + sh "terraform plan -out=plan" + } + } + } + } + + stage('apply_changes') { + steps { + dir('infra/terraform') { + withCredentials([azureServicePrincipal(credentialsId: 'jenkins-sp', + subscriptionIdVariable: 'ARM_SUBSCRIPTION_ID', + clientIdVariable: 'ARM_CLIENT_ID', + clientSecretVariable: 'ARM_CLIENT_SECRET', + tenantIdVariable: 'ARM_TENANT_ID')]) { + sh 'terraform apply plan' + } + } + } + } + + stage('setup_aks') { + steps { + withCredentials([azureServicePrincipal(credentialsId: 'jenkins-sp', + subscriptionIdVariable: 'AZURE_SUBSCRIPTION_ID', + clientIdVariable: 'AZURE_CLIENT_ID', + clientSecretVariable: 'AZURE_SECRET', + tenantIdVariable: 'AZURE_TENANT')]) { + ansiblePlaybook installation: 'ansible', + playbook: 'infra/ansible/setenv.yaml' + } + } + } + } +} diff --git a/infra/ansible/main.yaml b/infra/ansible/main.yaml new file mode 100644 index 000000000..2ebc0060b --- /dev/null +++ b/infra/ansible/main.yaml @@ -0,0 +1,12 @@ +- name: manage aks + hosts: localhost + connection: local + vars: + resource_group: jieshe-collection-test + tasks: + - name: Create a k8s namespace + k8s: + name: testing + api_version: v1 + kind: Namespace + state: present diff --git a/infra/ansible/setenv.yaml b/infra/ansible/setenv.yaml new file mode 100644 index 000000000..be16eef5c --- /dev/null +++ b/infra/ansible/setenv.yaml @@ -0,0 +1,14 @@ +- name: manage aks + hosts: localhost + connection: local + vars: + resource_group: azure-k8stest + tasks: + - name: Create staging namespaces + k8s: + state: present + src: ../kube/namespaces/staging.yaml + - name: Create production namespaces + k8s: + state: present + src: ../kube/namespaces/production.yaml diff --git a/infra/kube/namespaces/production.yaml b/infra/kube/namespaces/production.yaml new file mode 100644 index 000000000..67d15e121 --- /dev/null +++ b/infra/kube/namespaces/production.yaml @@ -0,0 +1,4 @@ +apiVersion: v1 +kind: Namespace +metadata: + name: production diff --git a/infra/kube/namespaces/staging.yaml b/infra/kube/namespaces/staging.yaml new file mode 100644 index 000000000..ee38adfbd --- /dev/null +++ b/infra/kube/namespaces/staging.yaml @@ -0,0 +1,4 @@ +apiVersion: v1 +kind: Namespace +metadata: + name: staging diff --git a/infra/kube/workloads/production/deployment.yaml b/infra/kube/workloads/production/deployment.yaml new file mode 100644 index 000000000..e69de29bb diff --git a/infra/kube/workloads/production/service.yaml b/infra/kube/workloads/production/service.yaml new file mode 100644 index 000000000..e69de29bb diff --git a/infra/kube/workloads/staging/deployment.yaml b/infra/kube/workloads/staging/deployment.yaml new file mode 100644 index 000000000..8c13c547b --- /dev/null +++ b/infra/kube/workloads/staging/deployment.yaml @@ -0,0 +1,24 @@ +apiVersion: extensions/v1beta1 +kind: Deployment +metadata: + name: pet-clinic + namespace: staging +spec: + selector: + matchLabels: + app: pet-clinic + + template: + metadata: + labels: + app: pet-clinic + version: master-xxxxxxx + + spec: + containers: + - name: pet-clinic + image: "jenkinsdemosacr.azurecr.io/pet-clinic:master-xxxxxxx" + ports: + - containerPort: 8080 + imagePullSecrets: + - name: "acr-secret" diff --git a/infra/kube/workloads/staging/service.yaml b/infra/kube/workloads/staging/service.yaml new file mode 100644 index 000000000..e9562c685 --- /dev/null +++ b/infra/kube/workloads/staging/service.yaml @@ -0,0 +1,14 @@ +kind: Service +apiVersion: v1 +metadata: + name: pet-clinic + namespace: staging + labels: + version: master-xxxxxxx +spec: + selector: + app: auth-service + version: master-xxxxxxx + ports: + - port: 80 + targetPort: 8080 diff --git a/infra/terraform/k8s.tf b/infra/terraform/k8s.tf new file mode 100644 index 000000000..cbb22a681 --- /dev/null +++ b/infra/terraform/k8s.tf @@ -0,0 +1,44 @@ +resource "azurerm_resource_group" "k8s" { + name = var.resource_group_name + location = var.location +} + +resource "azurerm_storage_account" "test" { + name = "tfstoragesfejsf" + resource_group_name = azurerm_resource_group.k8s.name + location = azurerm_resource_group.k8s.location + account_replication_type = "LRS" + account_tier = "Standard" +} + +resource "azurerm_kubernetes_cluster" "k8s" { + name = var.cluster_name + location = azurerm_resource_group.k8s.location + resource_group_name = azurerm_resource_group.k8s.name + dns_prefix = var.dns_prefix + + linux_profile { + admin_username = "ubuntu" + + ssh_key { + key_data = file(var.ssh_public_key) + } + } + + agent_pool_profile { + name = "agentpool" + count = var.agent_count + vm_size = "Standard_DS1_v2" + os_type = "Linux" + os_disk_size_gb = 30 + } + + service_principal { + client_id = var.client_id + client_secret = var.client_secret + } + + tags = { + Environment = "Development" + } +} diff --git a/infra/terraform/main.tf b/infra/terraform/main.tf new file mode 100644 index 000000000..798ff5497 --- /dev/null +++ b/infra/terraform/main.tf @@ -0,0 +1,6 @@ +provider "azurerm" { + version = "~>1.5" +} + +terraform { +} diff --git a/infra/terraform/output.tf b/infra/terraform/output.tf new file mode 100644 index 000000000..3aa3b154f --- /dev/null +++ b/infra/terraform/output.tf @@ -0,0 +1,27 @@ +output "client_key" { + value = "${azurerm_kubernetes_cluster.k8s.kube_config.0.client_key}" +} + +output "client_certificate" { + value = "${azurerm_kubernetes_cluster.k8s.kube_config.0.client_certificate}" +} + +output "cluster_ca_certificate" { + value = "${azurerm_kubernetes_cluster.k8s.kube_config.0.cluster_ca_certificate}" +} + +output "cluster_username" { + value = "${azurerm_kubernetes_cluster.k8s.kube_config.0.username}" +} + +output "cluster_password" { + value = "${azurerm_kubernetes_cluster.k8s.kube_config.0.password}" +} + +output "kube_config" { + value = "${azurerm_kubernetes_cluster.k8s.kube_config_raw}" +} + +output "host" { + value = "${azurerm_kubernetes_cluster.k8s.kube_config.0.host}" +} diff --git a/infra/terraform/variables.tf b/infra/terraform/variables.tf new file mode 100644 index 000000000..69932ea6c --- /dev/null +++ b/infra/terraform/variables.tf @@ -0,0 +1,30 @@ +variable "client_id" { + default = "764b3430-7053-4520-bbba-d74b8df5270c" +} +variable "client_secret" { + default = "Z.4tiyJc/l]ahAi:XKI2Ox3xg5UXWXdC" +} + +variable "agent_count" { + default = 3 +} + +variable "ssh_public_key" { + default = "~/.ssh/id_rsa.pub" +} + +variable "dns_prefix" { + default = "k8stest" +} + +variable cluster_name { + default = "k8stest" +} + +variable resource_group_name { + default = "azure-k8stest" +} + +variable location { + default = "East US" +}