-
Notifications
You must be signed in to change notification settings - Fork 25
/
create_script_step.go
67 lines (54 loc) · 1.67 KB
/
create_script_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
package examples
import (
"fmt"
"net/url"
"github.com/OctopusDeploy/go-octopusdeploy/octopusdeploy"
)
func CreateScriptStepExample() {
var (
// Declare working variables
octopusURL string = "https://your_octopus_url"
apiKey string = "API-YOUR_API_KEY"
projectName string = "project-name"
roleName string = "role-name"
scriptBody string = "Write-Host \"Hello world\""
spaceID string = "space-id"
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
}
// Get project
project, err := client.Projects.GetByName(projectName)
if err != nil {
// TODO: handle error
}
// Get the deployment process
deploymentProcess, err := client.DeploymentProcesses.GetByID(project.DeploymentProcessID)
if err != nil {
// TODO: handle error
}
// Create new step object
newStep := octopusdeploy.NewDeploymentStep(stepName)
newStep.Condition = "Success"
newStep.Properties["Octopus.Action.TargetRoles"] = roleName
// Create new script action
stepAction := octopusdeploy.NewDeploymentAction(stepName)
stepAction.ActionType = "Octopus.Script"
stepAction.Properties["Octopus.Action.Script.ScriptBody"] = scriptBody
// Add step action and step to process
newStep.Actions = append(newStep.Actions, *stepAction)
deploymentProcess.Steps = append(deploymentProcess.Steps, *newStep)
// Update process
_, err = client.DeploymentProcesses.Update(deploymentProcess)
if err != nil {
// TODO: handle error
}
}