您可以在我的网站上打印 ${CI_COMMIT_SHA} 吗?

huangapple go评论68阅读模式
英文:

Docker, Gitlab: How can I print ${CI_COMMIT_SHA} on my website?

问题

I want to display the value of Gitlab's variable CI_COMMIT_SHA on my website to know which website version is displayed at the moment.

To do this, I have this HTML snippet on my website:

<p>Hashcode2: current_hash_code2</p>

In my Dockerfile, I want to replace current_hash_code2 with the value of CI_COMMIT_SHA using this command:

RUN sed -i "s/current_hash_code2/${CI_COMMIT_SHA}/g" ./src/components/WelcomeMessage.vue

However, this is not working; I can only see this:

您可以在我的网站上打印 ${CI_COMMIT_SHA} 吗?

To get some more information, I ran some tests.

Website:

<p>Hashcode1: current_hash_code1</p>
<p>Hashcode2: current_hash_code2</p>
<p>original text</p>

Dockerfile:

RUN sed -i "s/original text/new text/g" ./src/components/WelcomeMessage.vue

ARG myvalue=15.8
RUN echo $myvalue
RUN sed -i "s/current_hash_code1/${myvalue}/g" ./src/components/WelcomeMessage.vue

RUN echo $CI_COMMIT_SHA
RUN sed -i "s/current_hash_code2/${CI_COMMIT_SHA}/g" ./src/components/WelcomeMessage.vue

Logging of Gitlab:

#11 [ 7/12] RUN sed -i "s/original text/new text/g" ./src/components/WelcomeMessage.vue
#11 DONE 0.4s
#12 [ 8/12] RUN echo 15.8
#12 0.496 15.8
#12 DONE 0.5s
#13 [ 9/12] RUN sed -i "s/current_hash_code1/15.8/g" ./src/components/WelcomeMessage.vue
#13 DONE 0.5s
#14 [10/12] RUN echo $CI_COMMIT_SHA
#14 0.396
#14 DONE 0.5s
#15 [11/12] RUN sed -i "s/current_hash_code2/${CI_COMMIT_SHA}/g" ./src/components/WelcomeMessage.vue
#15 DONE 0.4s

Website output:

您可以在我的网站上打印 ${CI_COMMIT_SHA} 吗?

As you can see, I am able to replace data via the Dockerfile, even when using variables. However, I cannot get the value of $CI_COMMIT_SHA to the website.

Do you know why?

英文:

I want to display the value of Gitlab's variable CI_COMMIT_SHA on my website to know which website version is displayed at the moment.

To do this, I have this HTML snippet on my website:

&lt;p&gt;Hashcode2: current_hash_code2&lt;/p&gt;

In my Dockerfile I want to replace current_hash_code2 with the value of CI_COMMIT_SHA with

RUN sed -i &quot;s/current_hash_code2/${CI_COMMIT_SHA}/g&quot; ./src/components/WelcomeMessage.vue

However, this is not working; I can only see this:

您可以在我的网站上打印 ${CI_COMMIT_SHA} 吗?

To get some more information I ran some tests.

Website:

&lt;p&gt;Hashcode1: current_hash_code1&lt;/p&gt;
&lt;p&gt;Hashcode2: current_hash_code2&lt;/p&gt;
&lt;p&gt;original text&lt;/p&gt;

Dockerfile:

RUN sed -i &quot;s/original text/new text/g&quot; ./src/components/WelcomeMessage.vue

ARG myvalue=15.8
RUN echo $myvalue
RUN sed -i &quot;s/current_hash_code1/${myvalue}/g&quot; ./src/components/WelcomeMessage.vue

RUN echo $CI_COMMIT_SHA
RUN sed -i &quot;s/current_hash_code2/${CI_COMMIT_SHA}/g&quot; ./src/components/WelcomeMessage.vue

Logging of Gitlab:

#11 [ 7/12] RUN sed -i &quot;s/original text/new text/g&quot; ./src/components/WelcomeMessage.vue
#11 DONE 0.4s
#12 [ 8/12] RUN echo 15.8
#12 0.496 15.8
#12 DONE 0.5s
#13 [ 9/12] RUN sed -i &quot;s/current_hash_code1/15.8/g&quot; ./src/components/WelcomeMessage.vue
#13 DONE 0.5s
#14 [10/12] RUN echo $CI_COMMIT_SHA
#14 0.396 
#14 DONE 0.5s
#15 [11/12] RUN sed -i &quot;s/current_hash_code2/${CI_COMMIT_SHA}/g&quot; ./src/components/WelcomeMessage.vue
#15 DONE 0.4s

Website output:

您可以在我的网站上打印 ${CI_COMMIT_SHA} 吗?

As you can see, I am able to replace data via the Dockerfile, even when using variables. However, I cannot get the value of $CI_COMMIT_SHA to the website.

Do you know why?

答案1

得分: 1

那么变量 CI_COMMIT_SHA 在你的 Dockerfile 中是空的,这意味着它可能在 docker build 过程中没有被赋值。

你需要:

docker build --build-arg CI_COMMIT_SHA=${CI_COMMIT_SHA}

而且你的 Dockerfile 应该在 FROM 之后以以下方式开始:

ARG CI_COMMIT_SHA
ENV CI_COMMIT_SHA $CI_COMMIT_SHA
英文:

Then variable CI_COMMIT_SHA is empty inside your Dockerfile, which means it might not have been valued during the docker build

You would need:

docker build --build-arg CI_COMMIT_SHA=${CI_COMMIT_SHA}

And your Dockerfile should start with (after the FROM)

ARG CI_COMMIT_SHA
ENV CI_COMMIT_SHA $CI_COMMIT_SHA

huangapple
  • 本文由 发表于 2023年3月3日 21:10:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75627487.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定