-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
113 lines (104 loc) · 8.42 KB
/
Jenkinsfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* Jenkinsfile for the IC OKPy fork
Jenkins instance at: http://ok-jenkins.uksouth.cloudapp.azure.com/
*/
// Run in declarative pipeline
pipeline {
// Agent will be specified per-stage
agent none
// Force testing environment
environment {
OK_ENV = 'test'
}
stages {
stage('Testing') {
// Docker and native builds are independent of each other
parallel {
stage('OK docker') {
agent { label 'azure-linux' }
steps {
// docker methods need to drop into scripted pipeline
script {
// generate version, it's important to remove the trailing new line in git describe output
def version = sh script: 'git rev-parse --short HEAD | tr -d "\n"', returnStdout: true
def imageName = "icokpy"
def imageTag = "${env.BUILD_NUMBER}_${version}"
def acrHost = 'icokpy.azurecr.io'
def acrUrl = "https://${acrHost}"
def location = 'westeurope'
def webAppName = 'icokpy'
def webAppResourceGroup = "icokpy-dev-${location}"
def okImage = docker.build("${imageName}:${imageTag}")
okImage.inside() {
sh 'py.test tests/ --ignore=tests/test_job.py'
}
if ( env.BRANCH_NAME == 'master' ) {
// Push the new image to acr
docker.withRegistry("${acrUrl}", 'icokpy-registry-credentials') {
okImage.push()
okImage.push("latest")
}
// Install azure-cli tools on the build slave
sh 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/azure-cli/ xenial main" | sudo tee /etc/apt/sources.list.d/azure-cli.list'
sh 'sudo apt-key adv --keyserver packages.microsoft.com --recv-keys 52E16F86FEE04B979B07E28DB02C46DF417A0893'
sh 'curl -L https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -'
sh 'sudo apt-get -y install apt-transport-https'
sh 'sudo apt-get update && sudo apt-get -y install azure-cli'
// Deploy
withCredentials([azureServicePrincipal('jenkins-service-principal')]) {
sh "az login --service-principal -u $AZURE_CLIENT_ID -p $AZURE_CLIENT_SECRET -t $AZURE_TENANT_ID"
sh "az account set -s $AZURE_SUBSCRIPTION_ID"
sh "az group create --name ${webAppResourceGroup} --location ${location}"
withCredentials([usernamePassword(credentialsId: 'icokpy-mysql-credentials', usernameVariable: 'mysqlUser', passwordVariable: 'mysqlPass')]) {
withCredentials([usernamePassword(credentialsId: 'icokpy-sendgrid', usernameVariable: 'sendgridUser', passwordVariable: 'sendgridPass')]) {
withCredentials([usernamePassword(credentialsId: 'icokpy-registry-credentials', usernameVariable: 'acrUser', passwordVariable: 'acrPass')]) {
withCredentials([usernamePassword(credentialsId: 'icokpy-azure-app-id', usernameVariable: 'azureAdAppID', passwordVariable: 'azureAdAppSecret')]) {
// Horrible hack - the first deployment is expected to fail with resources not yet available. so we need to
// re-run if the first deployment fails.
sh """
az group deployment create --resource-group ${webAppResourceGroup} --template-file azure/paas/azure.deploy.json \
--parameters @azure/paas/azure.deploy.parameters.json --parameters dockerImageName=${imageName} \
--parameters dockerImageTag=${imageTag} --parameters mySqlUsername=${mysqlUser} \
--parameters mySqlAdminPassword=${mysqlPass} --parameters sendgridPassword=${sendgridPass} \
--parameters dockerRegistryUsername=${acrUser} --parameters dockerRegistryPassword=${acrPass} \
--parameters dockerRegistryUrl=${acrHost} --parameter uniqueAppName='icokpy-dev' --parameter OkPyEnvironment='dev' \
--parameters templateBaseURL=https://raw.githubusercontent.com/icokpy/ok/master/azure/paas/ \
--parameters azureAdAppID=${azureAdAppID} --parameters azureAdAppSecret=${azureAdAppSecret} \
--parameters azureAdTenantID='ImperialLondon.onmicrosoft.com' || \
az group deployment create --resource-group ${webAppResourceGroup} --template-file azure/paas/azure.deploy.json \
--parameters @azure/paas/azure.deploy.parameters.json --parameters dockerImageName=${imageName} \
--parameters dockerImageTag=${imageTag} --parameters mySqlUsername=${mysqlUser} \
--parameters mySqlAdminPassword=${mysqlPass} --parameters sendgridPassword=${sendgridPass} \
--parameters dockerRegistryUsername=${acrUser} --parameters dockerRegistryPassword=${acrPass} \
--parameters dockerRegistryUrl=${acrHost} --parameter uniqueAppName='icokpy-dev' --parameter OkPyEnvironment='dev' \
--parameters templateBaseURL=https://raw.githubusercontent.com/icokpy/ok/master/azure/paas/ \
--parameters azureAdAppID=${azureAdAppID} --parameters azureAdAppSecret=${azureAdAppSecret} \
--parameters azureAdTenantID='ImperialLondon.onmicrosoft.com'
"""
}
}
}
}
sh 'az logout'
}
}
}
}
}
stage('OK native') {
agent { label 'azure-linux' }
steps {
sh 'sudo apt-get update'
// Required for pip and later installs
sh 'sudo apt-get -y install curl patch python3'
// Ubuntu pip package gives version problems
// with system python-six being too old
sh 'curl https://bootstrap.pypa.io/get-pip.py | sudo python3'
sh 'sudo pip install -r requirements.txt'
sh 'sudo pip install pytest-timeout python-coveralls'
sh 'py.test --cov-report term-missing --cov=server tests/ --ignore tests/test_web.py --timeout=30'
}
}
}
}
}
}