<p>Am trying to create a new namespace once I run the pipeline in Gitlab, and this namespace will have the name of the branch I push to it!</p>

<p>So how I can pass a variable that namespace manifest file can read it?
Is there any way to pass it with gitlab-ci.yml?</p>

<p>example of what i'm thinking about:</p>

<pre><code>apiVersion: v1
kind: Namespace
metadata:
  name: $CI_COMMIT_BRANCH
</code></pre>

Noraa
<p>So the best answer here would be to recommend <a href="https://helm.sh/" rel="nofollow noreferrer">Helm</a> since you are effectively asking for dynamic yaml templates. <a href="https://helm.sh/docs/faq/#changes-since-helm-2" rel="nofollow noreferrer">Since version 3</a> of Helm it does not require any server-side components like Tiller so it is much easier to use.</p>

<p>There are however, two more pragmatic approaches if you don't want to convert all your files to be Helm templates</p>

<hr>

<p><strong>sed</strong></p>

<p>By using <a href="https://www.gnu.org/software/sed/" rel="nofollow noreferrer">sed</a> you could find/replace the env variable in your (assumption) <code>namespace.yml</code> like this:</p>

<pre><code>script:
  - sed -i "s/\$CI_COMMIT_BRANCH/$CI_COMMIT_BRANCH/g" namespace.yml
  - kubectl apply -f namespace.yml
</code></pre>

<p>The <code>-i</code> stands for 'in-place', leave it if you want to save to a new file or STDOUT.</p>

<hr>

<p><strong>envsubst</strong></p>

<p>If you have multiple yaml files and need to mass find/replace, using <a href="https://www.gnu.org/software/gettext/manual/gettext.html#envsubst-Invocation" rel="nofollow noreferrer">envsubst</a> (part of the gnu <a href="https://www.gnu.org/software/gettext/" rel="nofollow noreferrer">gettext</a> utilities) is another option.</p>

<p>It finds all environment variables in a file and replaces them with their actual values as present in the current environment. This looks like this (<a href="https://stackoverflow.com/a/14157575/2078207">original answer</a>):</p>

<pre><code>script:
  - envsubst &lt; "namespace.yml" &gt; "namespace-replaced.yml"
  - kubectl apply -f namespace-replaced.yml
</code></pre>

<p>Note, as stated in the related answer: If you want to use the same file for both, you'll have to use something like moreutil's <code>sponge</code>: <code>envsubst &lt; "namespace.yml" | sponge "namespace.yml"</code>
. (Because the shell redirect will otherwise empty the file before its read.</p>

<hr>

<p>Final tips:</p>

<ul>
<li>Please don't define namespaces in your yaml files (except for namespace.yml) as it is not a best-practice. Use kubectl to set the target namespace instead: <code>kubectl apply -f deployment.yml --namespace=$CI_COMMIT_BRANCH</code></li>
<li>Don't use the <code>$CI_COMMIT_BRANCH</code> in relation to k8s. It's the full branch name and can contain special chars and spaces. Instead use <code>$CI_COMMIT_REF_SLUG</code> as <a href="https://docs.gitlab.com/ee/ci/variables/predefined_variables.html" rel="nofollow noreferrer">predefined CI variable</a> since this is the branch or tag name for which project is built, lowercased, shortened to 63 bytes, and with everything except 0-9 and a-z replaced with -. No leading / trailing -. Use in URLs, host names and domain names.</li>
</ul>
