Retry deploy job doesn't create environment
Summary
If a deployment job is retried and the environment has been deleted after the first deployment, the environment is not created.
While running the job, the job header shows This job is creating a deployment to .
, which references a blank environment.
Possible related issue: #34004
Proposal
All the jobs with environment.action != stop
should ensure that they have access to their environment (and related variables). If the environment does not exist it should be recreated. Stopped environments should be restarted.
Jobs with environment.action = stop
should fail if the environment does not exist to notify the user of unexpected situations (from GitLab's point of view unexpected) where manual intervention might be needed.
The above logic should work for job re-runs as well as first runs.
Bug description
Steps to reproduce
- Create a new project with CI/CD pipeline as below
stages:
- deploy
set-env:
stage: deploy
script:
- echo "deploy job"
environment:
name: my_env
- Run pipeline
- Stop environment and delete environment
- Retry deploy job
Analysis
The Ci::CreatePipelineService
has Gitlab::Ci::Pipeline::Chain::EnsureEnvironments
in the SEQUENCE
chain:
This seems to call the Environments::CreateForBuildService
for each stage in the pipeline:
By comparison, the Projects::JobsController#retry
calls the Ci::RetryJobService
:
The Ci::RetryJobService
calls retry_job
:
This calls clone!
which seems to clone the job:
Which then calls clone
on the job:
It seems to call Deployments::CreateForBuildService
, but it does not have a similar step to check for environments:
And the Deployments::CreateForBuildService
seems to return if there is no environment for the build:
Example Project
What is the current bug behavior?
Job runs successfully, no environment or deployment is created
What is the expected correct behavior?
Job runs successfully, environment and deployment are created
Relevant logs and/or screenshots
Output of checks
Results of GitLab environment info
GitLab SaaS and self-managed v16.0.1
Expand for output related to GitLab environment info
(For installations with omnibus-gitlab package run and paste the output of: \`sudo gitlab-rake gitlab:env:info\`) (For installations from source run and paste the output of: \`sudo -u git -H bundle exec rake gitlab:env:info RAILS_ENV=production\`)
Results of GitLab application Check
Expand for output related to the GitLab application check
(For installations with omnibus-gitlab package run and paste the output of: `sudo gitlab-rake gitlab:check SANITIZE=true`) (For installations from source run and paste the output of: `sudo -u git -H bundle exec rake gitlab:check RAILS_ENV=production SANITIZE=true`) (we will only investigate if the tests are passing)