I have requirement to invoke RunBooks(under Operation) which are associated with Project using API by giving a parameter Environment and Tenant (i.e it should invoke project which are connected to Environment and Tenant).
This is a basic script that will run a specific runbook and runbook snapshot with the ability to specify the desired environment and tenant. It is very barebones and requires the specific IDs for each resource, this could be enhanced by adding in some lookups on the names to retrieve the required IDs.
# https://www.nuget.org/packages/Octopus.Client/
Add-Type -Path 'Octopus.Client.dll'
$apikey = 'API-xxxxx' # Get this from your profile
$octopusURI = 'http://xxxxx' # Your Octopus Server address
$runbookId = "Runbooks-1" # Get this from /api/runbooks
$runbookSnapshotId = "RunbookSnapshots-1" # Get this from /api/runbookSnapshots
$environmentId = "Environments-1" # Get this from /api/environments
$tenantId = "Tenant-1" # Get this from /api/tenants
$endpoint = New-Object Octopus.Client.OctopusServerEndpoint $octopusURI,$apikey
$repository = New-Object Octopus.Client.OctopusRepository $endpoint
$runbook = $repository.RunbookSnapshots.Get($runbookSnapshotId)
$deployment = New-Object Octopus.Client.Model.RunbookRunResource
$deployment.RunbookId = $runbookId
$deployment.RunbookSnapshotId = $runbookSnapshotId
$deployment.EnvironmentId = $environmentId
$deployment.TenantId = $tenantId
$repository.RunbookRuns.Create($deployment)
This method is using the API. The Octopus.client is a library that makes it easier to script and manipulate the REST API.
If you would prefer to run a script that uses the REST API without this library that is definitely doable. You would just need to use the Swagger documentation to convert the sample script above to the pure REST API calls.
The easiest way to determine which API endpoint is needed is to perform the task you want to do within the UI and inspect which API endpoints are being used.
e.g. when running a runbook snapshot, I can see runbookRuns being used