-
Notifications
You must be signed in to change notification settings - Fork 25
/
add_environment_condition_to_step.go
68 lines (56 loc) · 1.62 KB
/
add_environment_condition_to_step.go
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
package examples
import (
"fmt"
"net/url"
"github.com/OctopusDeploy/go-octopusdeploy/octopusdeploy"
)
func AddEnvironmentConditionToStepExample() {
var (
// Declare working variables
octopusURL string = "https://your_octopus_url"
apiKey string = "API-YOUR_API_KEY"
spaceID string = "space-id"
projectName string = "project-name"
environmentNames []string = []string{"Development", "Test"}
stepName string = "Run a script"
)
apiURL, err := url.Parse(octopusURL)
if err != nil {
_ = fmt.Errorf("error parsing URL for Octopus API: %v", err)
return
}
client, err := octopusdeploy.NewClient(nil, apiURL, apiKey, spaceID)
if err != nil {
_ = fmt.Errorf("error creating API client: %v", err)
return
}
environmentIDs := []string{}
for _, environmentName := range environmentNames {
environments, err := client.Environments.GetByName(environmentName)
if err != nil {
// TODO: handle error
}
environmentIDs = append(environmentIDs, environments[0].GetID())
}
project, err := client.Projects.GetByName(projectName)
if err != nil {
// TODO: handle error
}
deploymentProcess, err := client.DeploymentProcesses.GetByID(project.DeploymentProcessID)
if err != nil {
// TODO: handle error
}
for _, step := range deploymentProcess.Steps {
if step.Name == stepName {
for _, action := range step.Actions {
for _, environmentID := range environmentIDs {
action.Environments = append(action.Environments, environmentID)
}
}
}
}
_, err = client.DeploymentProcesses.Update(deploymentProcess)
if err != nil {
// TODO: handle error
}
}