Add missing actions and data to Environments and Deployments APIs
Problem
Review apps are short lived by definition. Unfortunately, stale branches are ubiquitous (gitlab-ce has ~1,600). Keeping all as review apps would is already a burden for us. It's not possible to write a script because
Proposal
We want to write a script that periodically deletes old environments. However, it's not possible to implement it because Environments and Deployments APIs don't provide enough information.
environments = get "/projects/#{project_id.urlencode}/environments"
environments.each do |environment|
next unless environment.available?
deployment = get "/projects/#{project_id.urlencode}/deployments/#{environment.deployment_id.urlencode}"
next if deployment.created_at >= 2.weeks.ago
next if deployment.deployable.name != 'reviewapp-deploy' # VERY IMPORTANT! We only want to delete review apps, right?!
post "/projects/#{project_id.urlencode}/environments/#{environment.id.urlencode}/stop"
end
Missing API functionality to be added
Status: available or stopped.
- Endpoint
/projects/:id/environments
- Access the
state
field from the API - When the model is instantiated, state machine helper methods are available
available?
,stopped?
Deployment ID of the current deployment
- Endpoint
/projects/:id/environments
- field
last_deployment
exposed https://gitlab.com/gitlab-org/gitlab/blob/master/app/models/environment.rb#L11 -
last_deployment
is anEntities::Deployment
object, so this call is not necessary:deployment = get "/projects/#{project_id.urlencode}/deployments/#{environment.deployment_id.urlencode}"
def show
)
Add missing API end-point (Rails: - This endpoint exists - no changes needed
GET /projects/:id/environments/:environment_id
- https://docs.gitlab.com/ee/api/environments.html#get-a-specific-environment - added in April 2019 724f19ba
Links / references
~"feature proposal" ~"CI/CD" ~kubernetes
@ayufan @markglenfletcher @markpundsack @bikebilly @victorwu
Edited by Damian Nowak