This commit is contained in:
gavinfish 2019-10-17 14:01:43 +08:00
parent ae1f4a804c
commit 507549cac3
16 changed files with 291 additions and 67 deletions

6
Dockerfile Normal file
View file

@ -0,0 +1,6 @@
FROM java:8-jre
ADD ./target/pet-clinic.jar /app/
CMD ["java", "-Xmx200m", "-jar", "/app/pet-clinic.jar"]
EXPOSE 8080

72
Jenkinsfile vendored
View file

@ -1,22 +1,56 @@
node('master') { pipeline {
stage('init') { agent any
checkout scm
}
stage('image build') { stages {
sh ''' stage('init') {
./mvnw clean package steps {
cd target git url: "git@github.com:azure-devops/spring-petclinic.git",
mv *.jar petclinic.jar credentialsId: "github_ssh_key",
cp ../web.config web.config branch: "ignite"
zip petclinic.zip web.config petclinic.jar }
''' }
}
stage('deploy') { stage('build') {
azureWebAppPublish appName: env.APP_NAME, steps {
azureCredentialsId: env.CRED_ID, sh '''
resourceGroup: env.RESOURCE_GROUP, ./mvnw clean package
filePath: 'target/*.zip' 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
'''
}
}
}
} }
} }

View file

@ -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
}
}

53
infra/Jenkinsfile vendored Normal file
View file

@ -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'
}
}
}
}
}

12
infra/ansible/main.yaml Normal file
View file

@ -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

14
infra/ansible/setenv.yaml Normal file
View file

@ -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

View file

@ -0,0 +1,4 @@
apiVersion: v1
kind: Namespace
metadata:
name: production

View file

@ -0,0 +1,4 @@
apiVersion: v1
kind: Namespace
metadata:
name: staging

View file

@ -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"

View file

@ -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

44
infra/terraform/k8s.tf Normal file
View file

@ -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"
}
}

6
infra/terraform/main.tf Normal file
View file

@ -0,0 +1,6 @@
provider "azurerm" {
version = "~>1.5"
}
terraform {
}

27
infra/terraform/output.tf Normal file
View file

@ -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}"
}

View file

@ -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"
}