title: How is base container executed on Gitlab runner
body: <p>I am working on a CI/CD pipeline which uses a containerised environment. </p>
  
  <ul>
  <li>On my laptop, everything thing is run within a centos container which contains all of my tools, while the code and configuration reside on a shared volume.  </li>
  <li>Most of that shared volume gets pushed up to master when I submit.  
  
  <ul>
  <li>But, for example, I exclude any python binaries from the virtual environment.  </li>
  </ul></li>
  <li>That same container is used as my "base" container in Gitlab to execute some of my runner stages in order to  
  
  <ul>
  <li>execute static lint testing of both the code and the ansible config  </li>
  <li>build the final product as a new image with ansible-bender  </li>
  </ul></li>
  </ul>
  
  <p>In order to distinguish the environments, I use a basic shell in the <code>entrypoint.sh</code> which is executed when the container comes up and in that shell, I used an environment variable which is managed by Gitlab: <code>CI_JOB_STAGE</code>. But the shell script doesn't seem to perform in the same way when I run the container locally as opposed to on the runner. Which is crazy, because that the whole point of containers isn't it?</p>
  
  <ul>
  <li>it's unclear at what point Gitlab mounts the share between the runner host and the container</li>
  <li>conditions testing the value <code>CI_JOB_STAGE</code> don't seem to work even though the values are shown when it printed out with and <code>echo</code></li>
  <li>for some reason the entrypoint is executed both before <strong>AND</strong> after the stage executes</li>
  </ul>
  
  <p>I am attaching gists for </p>
  
  <ul>
  <li>the entrypoint.sh script</li>
  <li>the output when the container is run locally
  
  <ul>
  <li>in this case, it see that the virtual env already exist by check for the existence of the activate binary, so that environment is simply activated</li>
  <li>it then checks for the flask binary to see if it needs to install requirements</li>
  <li>i know it's primitive, but this is just POC</li>
  </ul></li>
  <li>the output for the first stage of the runner
  
  <ul>
  <li>based on the shell script, it should not install the requirements because that is part of the stage commands anyway</li>
  <li>I don't believe it actually does do so, because it goes by too quickly, but the message indicates that it went through the condition</li>
  <li><code>if [[ $CI_JOB_STAGE -eq "locally" ]] &amp;&amp; ! test -f "./env/bin/flask"</code>
  
  <ul>
  <li>if the stage is local (false: we clearly see that stage is <code>lint_code</code>) </li>
  <li><strong>AND</strong> </li>
  <li>flask is not present (well, it won't be, but it doesn't matter because the first half is already false)</li>
  </ul></li>
  </ul></li>
  </ul>
  
  <p>The stages still run correctly, but I find this all very confusing. Does anyone have detailed knowledge of how these runners execute?</p>
  
  <p>gists</p>
  
  <ul>
  <li><a href="https://gist.github.com/mchassy/8cb9542cbf2b45c096c8e3b04fcc76b2" rel="nofollow noreferrer">the entrypoint.sh for the container</a></li>
  <li><a href="https://gist.github.com/mchassy/b185fb838d92ccbe7bbed43f2ba81f16" rel="nofollow noreferrer">output of entrypoint when the container runs locally on my laptop</a></li>
  <li><a href="https://gist.github.com/mchassy/d8086605dc6f820199115c2c5ed70c5e" rel="nofollow noreferrer">output of entrypoint and then of the stage commands and then strangely of the entrypoint <strong>AGAIN</strong></a></li>
  </ul>