buildscript { ext { springBootVersion = '1.5.9.RELEASE' } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") classpath "com.moowork.gradle:gradle-node-plugin:1.2.0" classpath 'org.hidetake:gradle-ssh-plugin:2.9.0' } repositories { mavenLocal() maven { url "https://plugins.gradle.org/m2/" } } } enum Environment{ localdev, localtest, dev , test , prod } ext.version_base = '1.0.5' ext.environment = project.hasProperty('env') ? env : Environment.dev.toString() version = version_base + "-$ext.environment" group = 'uk.ac.ebi.ena.webin-portal' ext.sshKeyFile = file(hasProperty('sshKeyFile') ? sshKeyFile : "${System.properties['user.home']}/.ssh/id_rsa.ppk") ext.gitlab_project_id ='977' ext.envAngularBuildScriptMap = [ 'test' : 'buildTest', 'dev' : 'buildDev', 'prod' : 'buildProd', 'default': 'build' ] printSetup() def props = new Properties() file('src/main/resources/application.properties').withInputStream { props.load(it) } ext.serverContextPath = removeLeadingSlash(props['server.contextPath']) ext.serverPort = props['server.port'] apply plugin: 'java' apply plugin: 'eclipse' apply plugin: 'org.springframework.boot' apply plugin: 'com.moowork.node' apply plugin: 'org.hidetake.ssh' apply plugin: 'maven-publish' sourceCompatibility = 1.8 sourceSets.main.resources.includes = [ "**/*.*" ] repositories { mavenLocal() maven { url "https://gitlab.ebi.ac.uk/api/v4/groups/enasequence/-/packages/maven" } mavenCentral() } dependencies { compile('org.springframework.boot:spring-boot-starter-web') compile 'org.springframework.boot:spring-boot-starter-actuator' testCompile('org.springframework.boot:spring-boot-starter-test') } node { version = "10.9.0" download = true nodeModulesDir = file("frontend") } processResources { filesMatching('application*.properties') { expand(project.properties) } } jar { manifest { attributes( 'Implementation-Title': project.name, 'Implementation-Version': project.version, 'Built-By': System.getProperty('user.name'), 'Built-Date': new Date(), 'Source-Compatibility': project.sourceCompatibility) } } task sourceJar(type: Jar) { classifier = 'sources' from sourceSets.main.allSource } remotes { dev { host = 'ves-ebi-5b.ebi.ac.uk' user = 'ena_adm' identity = sshKeyFile knownHosts = allowAnyHosts } test { host = 'ves-ebi-5a' user = 'ena_adm' identity = sshKeyFile knownHosts = allowAnyHosts } prodA { host = 'ves-hx-5a' user = 'ena_adm' identity = sshKeyFile knownHosts = allowAnyHosts } prodB { host = 'ves-hx-5b' user = 'ena_adm' identity = sshKeyFile knownHosts = allowAnyHosts } } publishing { publications { mavenJava(MavenPublication) { from components.java artifact sourceJar } } repositories { maven { // Project specific maven repository in Gitlab. url "https://gitlab.ebi.ac.uk/api/v4/projects/${gitlab_project_id}/packages/maven" // Developer token in Gitlab. credentials(HttpHeaderCredentials) { name = "Private-Token" value = project.ext.properties.gitlab_private_token } authentication { header( HttpHeaderAuthentication ) } } } } wrapper{ gradleVersion = '5.2.1' } task buildClientWatch(type: NpmTask, dependsOn: 'npmInstall'){ group = 'application' description = "Build and watches the client side assets for rebuilding" args = ['run','buildWatch'] } task buildClient(type:NpmTask, dependsOn:'npmInstall'){ println 'buildClient script:' + getAngularBuildScript() group = 'build' description = "Compile client side folder for production" args = ['run', getAngularBuildScript()] } def getEnvRemotes() { def hosts = [] switch (environment){ case "dev": hosts.add(remotes.dev) ; break case "test": hosts.add(remotes.test) ; break case "prod": hosts.add(remotes.prodA) hosts.add(remotes.prodB) break } return hosts } def getAngularBuildScript(){ def script = envAngularBuildScriptMap[environment.toString()] if(script) return script else return envAngularBuildScriptMap['default'] } task deploy() { doLast{ ssh.run { println("execute" + "~/ena/generic-deploy.sh $project.group $project.name $version $environment $serverPort $serverContextPath") for (remote in getEnvRemotes()){ println("Deploying into: " + remote) session(remote) { execute "~/ena/generic-deploy.sh $project.group $project.name $version $environment $serverPort $serverContextPath" } } } } } task restartDev(){ doLast{ restart(remotes.dev) } } task restartTest(){ doLast{ restart(remotes.test) } } task restartProd(){ doLast{ restart(remotes.prodA) restart(remotes.prodB) } } def restart(remote){ ssh.run { session(remote) { println "Restarting app" def result = execute "monit restart $project.name" println result } } } task displayInfo(){ doLast{ displayInfo(remotes.dev) displayInfo(remotes.test) displayInfo(remotes.prodA) displayInfo(remotes.prodB) } } def displayInfo(remote){ println "" println "Remote:$remote, host:$remote.host" println "Info and health status" doGet(getInfoURL(remote)) doGet(getHealthURL(remote)) println "" } def doGet(String url){ try { println url println(new URL(url).text) } catch(all){ print all.getMessage() } } def getInfoURL(remote){ return getBaseURL(remote) + "/info" } def getHealthURL(remote){ return getBaseURL(remote) + "/health" } def getBaseURL(remote){ return "http://${remote.host}:${serverPort}/${serverContextPath}" } static def removeLeadingSlash(url){ if(url.substring(0,1) == "/" )return url.substring(1) return url } // javascript unit testing task testUnit(type: NpmTask) { args = ['run' , 'testHeadLess'] } //include js unit tests into project build lifecycle def printSetup(){ println() println("ENVIRONMENT:" + ext.environment) println("VERSION:" + version) println("VERSION BASE:" + version_base) println("GROUP:" + group) println("KEY FILE::" + ext.sshKeyFile) println() } deploy.dependsOn(publish) publish.dependsOn(assemble) bootRepackage.dependsOn(buildClient) bootRun.dependsOn(bootRepackage) jar.dependsOn(buildClient) compileJava.dependsOn(clean) test.dependsOn testUnit