<p>I have three K8s clusters; staging, sandbox, and production. I would like to:</p>
<ol>
<li>Trigger a pipeline to build and deploy an image to staging, if a merge request to <code>master</code> is created</li>
<li>Upon a successful deploy of staging, I would like the branch to be merged into <code>master</code></li>
<li>I would like to use the <em>same</em> image I already built in the build job before the staging deploy, to be used to deploy to sandbox and production</li>
</ol>
<p>Something like this:</p>
<pre><code>build:
  ... (stuff that builds and pushes &quot;$CI_REGISTRY_IMAGE:$IMAGE_TAG&quot;)
  rules:
    - if: $CI_PIPELINE_SOURCE == 'merge_request_event'

staging:
  ...
  rules:
    - if: $CI_PIPELINE_SOURCE == 'merge_request_event'

sandbox:
  ...
  ?

production:
  ...
  ?
</code></pre>
<p>What I can't figure out is how to both have a successful MR at the end of the <code>staging</code> job and thereby have the pipeline merge the branch into master, and also then pass down whatever <code>$CI_REGISTRY_IMAGE:$IMAGE_TAG</code> was to continue with the jobs for the sandbox and production deploys.</p>

<blockquote>
<p>Trigger a pipeline to build and deploy an image to staging, if a merge
request to master is created</p>
</blockquote>
<p>For first you can create rules like</p>
<pre><code>only:
     - merge_requests
   except:
     variables:
       - $CI_MERGE_REQUEST_TARGET_BRANCH_NAME != &quot;master&quot;
</code></pre>
<p>You can run the curl command or hit API to approve the MR
<a href="https://gitlab.example.com/api/v4/projects/:id/merge_requests/:merge_request_iid/approve" rel="nofollow noreferrer">https://gitlab.example.com/api/v4/projects/:id/merge_requests/:merge_request_iid/approve</a></p>
<p>Reference : <a href="https://stackoverflow.com/a/58036578/5525824">https://stackoverflow.com/a/58036578/5525824</a></p>
<p>Document: <a href="https://docs.gitlab.com/ee/api/merge_requests.html#accept-mr" rel="nofollow noreferrer">https://docs.gitlab.com/ee/api/merge_requests.html#accept-mr</a></p>
<blockquote>
<p>I would like to use the same image I already built in the build job
before the staging deploy, to be used to deploy to sandbox and
production</p>
</blockquote>
<p>You can use the <code>TAG_NAME: $CI_COMMIT_REF_NAME</code> passing across the stages as Environment variable</p>
<p>You are making it really complicated ideally you can use the TAG and make it easy to manage and deploy using the CI.</p>
<p>Merge when MR gets merged and create TAG and build docker images with TAG name, deploy that same TAG across environment simple.</p>
