简体   繁体   English

GitLab CICD 部署与 GitLab 环境分支流程

[英]GitLab CICD deployment with GitLab Environment branches flow

I'm trying to setup a gitlab cicd pipeline flow for my docker-based web app projects .我正在尝试为基于 docker 的 web 应用程序项目设置 gitlab cicd 管道流。 The idea is to use the gitlab environment branches flow to make developed code bubble towards the prod environment, which is publicly exposed on the internet.这个想法是使用gitlab 环境分支流,使开发的代码向 prod 环境冒泡,并在互联网上公开。

From the master branch, I develop a feature on a dedicated branch, then I open a merge request into master .master分支,我在专用分支上开发了一个特性,然后我打开一个合并请求master If tests pass and the merge request is accepted, a particular job creates a build tagged docker image for my app, which is stored on the project container registry.如果测试通过并且合并请求被接受,则特定作业会为我的应用程序创建一个带有构建标记的 docker 映像,该映像存储在项目容器注册表中。 This is done through a job which uses kaniko to create an image tagged both with latest and the CI_COMMIT_SHORT_SHA , to ensure image reproducibility.这是通过使用kaniko创建一个带有latestCI_COMMIT_SHORT_SHA标记的图像的作业来完成的,以确保图像的可再现性。

Then, a merge request is opened into staging to deploy the latest tagged image into staging.然后,将合并请求打开到staging中,以将最新的标记图像部署到 staging 中。 If everything goes fine and I subsequently want to deploy into prod, I git tag the current commit with semantic versioning (ie vX.YZ).如果一切顺利并且我随后想部署到 prod 中,我git 用语义版本控制(即 vX.YZ)标记当前提交 The tagging procedure triggers a tag pipeline with which I create a new semantic versioned image tag out of the staging image version thanks to crane (which avoids me to rebuild a new image).标记过程触发了一个标记管道,我使用它从暂存图像版本中创建一个新的语义版本化图像标记,这要归功于起重机(这避免了我重建新图像)。

Now I open a merge request into prod , which is supposed to do the deployment in production.现在我向prod打开一个合并请求,它应该在生产中进行部署。 Here I have a problem.在这里我有一个问题。 Ideally, imho, it would be better to deploy the image with the semantic versioning tag.理想情况下,恕我直言,最好使用语义版本控制标签部署图像。 This is the best way to ensure reproducibility over time, and helps understanding exactly which image version is run in prod.这是随着时间的推移确保可重复性的最佳方式,并有助于准确了解在 prod 中运行的映像版本。 The problem is that gitlab CICD pipeline alternatively provides the tag name or the branch name.问题是 gitlab CICD 管道交替提供标签名称或分支名称。 Meaning: there is no way to access both the git tag name and the branch within a pipeline, independently of its type (branch/ tag).含义:无法同时访问 git 标记名称和管道中的分支,无论其类型(分支/标记)如何。 To me, both of them should be on the contrary available in both types of pipelines.对我来说,相反,它们都应该在两种类型的管道中都可用。

If I have the tag, I lose the possibility to know in which branch I am.如果我有标签,我就无法知道我在哪个分支。 This can cause to redeploy to prod from wherever I am tagging, which could lead to undesired deployments.这可能会导致从我标记的任何地方重新部署到 prod,这可能会导致不需要的部署。 Conversely, if I have the branch name, I'm sure I'm deploying when I'm merging into production, but I cannot deploy the image version I want through the git tag.相反,如果我有分支名称,我确定我在合并到生产时正在部署,但我无法通过 git 标签部署我想要的映像版本。 Yes, I could use the COMMIT_SHORT_SHA, but this makes it useless to produce a semantic versioned tagged image in the registry, which would be the best option to know exactly what is deployed into prod.是的,我可以使用 COMMIT_SHORT_SHA,但这使得在注册表中生成语义版本标记图像毫无用处,这将是准确了解部署到 prod 中的最佳选择。

Any idea on how to solve this problem?关于如何解决这个问题的任何想法? Thanks!谢谢!

If I understand your question properly, you want to be able to get both commit tag and branch name in the pipeline.如果我正确理解您的问题,您希望能够在管道中同时获得提交标签和分支名称。 You can use CI_COMMIT_TAG for the tag and execute the following command to get the branch name in the variable TAG_BRANCH TAG_BRANCH=$(git branch --contains tags/CI_COMMIT_TAG) .您可以使用CI_COMMIT_TAG作为标签并执行以下命令以获取变量 TAG_BRANCH TAG_BRANCH=$(git branch --contains tags/CI_COMMIT_TAG)中的分支名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM