Script example to update the deployed version and other deployment attributes of an environment.

Script

package com.apwide.golive.example

import com.apwide.env.deployment.Deployment
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
@WithPlugin("com.holydev.env.plugin.jira-holydev-env-plugin")

import com.apwide.env.environment.Environment
import com.apwide.golive.deployment.DeploymentDescriptor
import com.apwide.golive.deployment.DeploymentHelper
import com.apwide.golive.helper.EnvironmentHelper

EnvironmentHelper helper = new EnvironmentHelper()
DeploymentHelper deploymentHelper = new DeploymentHelper()

Environment environment = helper.getEnvironment("eCommerce", "Staging")

String versionName = "My new version name"
String versionId = "10001" //optionally: put the id of the Jira version if available
String buildNumber = "#345"
String releaseNotesUrl = "https://my-release-notes.example.com/${versionName}"
String description = """Release Notes:

  - bugs fixed: BUG-123, BUG-344
  - new features: FEAT-34, FEAT-56
  
  Full release notes: <a href="${releaseNotesUrl}">${versionName}</a>
  
"""

DeploymentDescriptor deploymentDescriptor = new DeploymentDescriptor()
deploymentDescriptor.setVersionName(versionName)
deploymentDescriptor.setVersionId(versionId)
deploymentDescriptor.setBuildNumber(buildNumber)
deploymentDescriptor.setDescription(description)

// Uncomment following lines if you have both "Components" and "Team" deployment attributes defined at application level:
/*String components = "frontend, backend, api"
String teamEmail = "red-team@example.com"
deploymentDescriptor.setAttributes(["Components": components, "Team": teamEmail])*/


Deployment deployment = deploymentHelper.setDeployment(environment, deploymentDescriptor)

return """
Deployed version name=${deployment.versionName},
Deployed version id=${deployment.versionId},
Deployed build number=${deployment.buildNumber},
Deployed on=${deployment.deployed},
Deployment description=${deployment.description},
Deployment attributes=${deployment.attributes?.flatten({"${it.attribute.name}=${it.value}"})}}
"""

GROOVY

This script depends on

  • com.apwide.golive.accessor.EnvironmentServicesAccessor

  • com.apwide.golive.deployment.DeploymentDescriptor

  • com.apwide.golive.deployment.DeploymentHelper

  • com.apwide.golive.helper.EnvironmentHelper

(you can copy code below).

Files must be structured that way in your script root:

DeploymentHelper

package com.apwide.golive.deployment

import com.apwide.env.deployment.Deployment
import com.apwide.env.deployment.DeploymentSearcher
import com.apwide.golive.accessor.EnvironmentServicesAccessor
import com.apwide.env.deployment.DeploymentManager
import com.apwide.env.environment.Environment
import com.apwide.env.service.EnvironmentServices
import groovy.util.logging.Log4j

@Log4j
class DeploymentHelper {

    EnvironmentServices environmentServices = EnvironmentServicesAccessor.get()
    DeploymentManager deploymentManager = environmentServices.getDeploymentMgr()
    DeploymentSearcher deploymentSearcher = environmentServices.getDeploymentSearcher()


    Deployment setDeployment(Environment environment, DeploymentDescriptor deploymentDescriptor){
        Deployment newDeployment = deploymentManager.deployVersion(deploymentDescriptor.toInputParameters(environment)).entity.entity
        // Need to reload the deployment to get attribute values (bug fixed in Golive 9)
        return deploymentSearcher.getById(newDeployment.getId()).entity
    }

}
GROOVY

DeploymentDescriptor

package com.apwide.golive.deployment

import com.apwide.env.attribute.Attribute
import com.apwide.env.attribute.value.AttributeValue
import com.apwide.env.deployment.DeploymentInputParameters
import com.apwide.env.environment.Environment
import groovy.util.logging.Log4j

@Log4j
class DeploymentDescriptor {
     String versionId;
     String versionName;
     String buildNumber;
     Date deploymentDate;
     String description;
     Map<String, String> attributes;

     DeploymentInputParameters toInputParameters(Environment environment){

         DeploymentInputParameters params = new DeploymentInputParameters()

          params.setEnvironmentId(environment.id)

          params.setVersionId(this.versionId)
          params.setBuildNumber(this.buildNumber)
          params.setVersionName(this.versionName)
          params.setBuildNumber(this.buildNumber)
          params.setDeploymentDate(this.deploymentDate)
          params.setDescription(this.description)
          params.setAttributes(this.getAttributeValues())

          return params
     }

    private List<AttributeValue> getAttributeValues(){
        if (attributes == null || attributes.isEmpty())
            return new ArrayList<AttributeValue>()

       List<AttributeValue> attributeValues = attributes?.entrySet()?.stream()?.map({ entry ->
            Attribute attribute = Attribute.fromName(entry.key)
            return new AttributeValue.AttributeValueBuilder().attribute(attribute).value(entry.value).build()
        })?.toArray()
        log.debug "AttributeValues: ${attributeValues}"
        return attributeValues
    }
}
GROOVY

EnvironmentHelper

package com.apwide.golive.helper

import com.apwide.env.application.ApplicationSearcher
import com.apwide.env.attribute.Attribute
import com.apwide.env.attribute.value.AttributeValue
import com.apwide.env.common.BaseResult
import com.apwide.env.common.ErrorCollectionHelper
import com.apwide.env.envcategory.EnvironmentCategory
import com.apwide.env.envcategory.EnvironmentCategorySearcher
import com.apwide.env.environment.Environment
import com.apwide.env.environment.admin.EnvironmentInputParameters
import com.apwide.env.environment.admin.EnvironmentManager
import com.apwide.env.environment.search.EnvironmentSearcher
import com.apwide.env.service.EnvironmentServices

import com.apwide.golive.accessor.EnvironmentServicesAccessor
import com.atlassian.crowd.model.application.Application
import groovy.util.logging.Log4j

@Log4j
class EnvironmentHelper {

    EnvironmentServices environmentServices = EnvironmentServicesAccessor.get()
    EnvironmentSearcher environmentSearcher = environmentServices.getEnvSearcher()
    EnvironmentManager environmentManager = environmentServices.getEnvMgr()
    EnvironmentCategorySearcher categorySearcher = environmentServices.getEnvCatSearcher()
    ApplicationSearcher applicationSearcher = environmentServices.getAppSearcher()

    Environment getEnvironment(String applicationName, String categoryName) {
        BaseResult<Environment> result = environmentSearcher.getByUnique(getApplication(applicationName), getEnvironmentCategory(categoryName))
        if (result.hasAnyErrors()) {
            log.error(ErrorCollectionHelper.displayErrors(result.getErrorCollection()))
            return null
        }
        return result.getEntity()
    }

    Environment setAttributes(Environment environment, Map<String, String> attributes) {
        List<AttributeValue> attributeValues = attributes.entrySet().stream().map({ entry ->
            Attribute attribute = Attribute.fromName(entry.key)
            return new AttributeValue.AttributeValueBuilder().attribute(attribute).value(entry.value).build()
        }).toArray()

        EnvironmentInputParameters params = new EnvironmentInputParameters()
        params.setAttributes(attributeValues)

        BaseResult<Environment> result = environmentManager.update(environment.getID(), params)
        if (result.hasAnyErrors()) {
            log.error(ErrorCollectionHelper.displayErrors(result.getErrorCollection()))
            return null
        }
        return result.getEntity()
    }

    String getAttributeValue(Environment environment, String attributeName){
        if (!environment.getAttributes())
            return null
        AttributeValue attributeValue = environment.getAttributes().find({AttributeValue attributeValue -> attributeValue.attribute.name == attributeName})
        return attributeValue?.value
    }

    Application getApplication(String applicationName) {
        BaseResult<Application> result = applicationSearcher.getByName(applicationName)
        if (result.hasAnyErrors()) {
            log.error(ErrorCollectionHelper.displayErrors(result.getErrorCollection()))
            return null
        }
        return result.getEntity()
    }

    EnvironmentCategory getEnvironmentCategory(String categoryName) {
        BaseResult<EnvironmentCategory> result = categorySearcher.getByName(categoryName)
        if (result.hasAnyErrors()) {
            log.error(ErrorCollectionHelper.displayErrors(result.getErrorCollection()))
            return null
        }
        return result.getEntity()
    }


}
GROOVY

EnvironmentServicesAccessor

package com.apwide.golive.accessor

import com.onresolve.scriptrunner.runner.customisers.WithPlugin
@WithPlugin("com.holydev.env.plugin.jira-holydev-env-plugin")

import com.atlassian.jira.component.ComponentAccessor
import com.apwide.env.service.GoliveEnvironmentServices

class EnvironmentServicesAccessor {
    static GoliveEnvironmentServices get(){
        final Class serviceClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.apwide.env.service.GoliveEnvironmentServices")
        return ComponentAccessor.getOSGiComponentInstanceOfType(serviceClass)
    }
}
GROOVY