如何使用上次创建的git标签触发管道(Terraform -AWS CodeCommit)

发布于 2025-02-05 08:40:11 字数 584 浏览 4 评论 0原文

我想从创建的最后一个git标签(gitlab)中触发AWS中的管道,在我的Terraform模块中,我告诉它从特定的标签中启动管道,但这是否有效,但是是否可以传递动态值?这个想法是用最后一个创建的git标签触发管道。

{
    "source"      = ["aws.codecommit"]
    "detail-type" = ["CodeCommit Repository State Change"]
    "resources"   = [data.aws_codecommit_xxxxxxxxx]
    "detail" = {
      "event"         = ["referenceCreated", "referenceUpdated"]
      "referenceType" = ["tag"]
      "referenceName" = [var.repository_tag_version]
    }
  }

var.repository_tag_version应该是最新的git标签,在Terraform中有一种方法:git describ descript -tags -tags -abbrev = 0以获取最新的?

I want to trigger pipelines in AWS from the last git tag created (Gitlab), in my terraform module I am telling it to fire the pipeline from a specific tag and that is working, but is it possible to pass a dynamic value? The idea is to trigger the pipeline with the last created git tag.

{
    "source"      = ["aws.codecommit"]
    "detail-type" = ["CodeCommit Repository State Change"]
    "resources"   = [data.aws_codecommit_xxxxxxxxx]
    "detail" = {
      "event"         = ["referenceCreated", "referenceUpdated"]
      "referenceType" = ["tag"]
      "referenceName" = [var.repository_tag_version]
    }
  }

var.repository_tag_version should be the latest git tag, there is a way to do this in terraform: git describe --tags --abbrev=0 to get the latest one?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鹤舞 2025-02-12 08:40:11

这就是我能够修复它的方式。

1:创建一个bash脚本,在脚本中,您需要执行逻辑以克隆存储库并获得想要获得的标签。 IE:
MAIN.TF中的external_script.sh

while getopts a: flag
do
  case "${flag}" in
    a) git_repository=${OPTARG};;
  esac
done
# Check out the repository to a temporary directory
temp_folder="temp"
mkdir "$temp_folder"
git clone "${git_repository}" "$temp_folder"
cd "$temp_folder"
git fetch --tags
last_git_tag=$(git describe --tags --abbrev=0)
# Export last_git_tag value to txt file
cd ..
echo "$last_git_tag" > "latest-git-tag-value.txt"

resource "null_resource" "get_last_git_tag_created" {
  provisioner "local-exec" {
    command = "bash ./external_script.sh -a $repository_link"
    interpreter = ["bash", "-c"]
    environment = {
      repository_link = "your_repo_url"
    }
  }
}

读取最后一个git标签的值:

data "local_file" "read_file_content" {
    filename = "./latest-git-tag-value.txt"
    depends_on = [null_resource.get_last_git_tag_created]
}

现在,该值在data.local_file.read_file_content中,因此事件触发器应该看起来像这样:

“ referenceType” = [referenceType'= [“ tag”]

referencenCename”。 = [{“ prefix”:data.local_file.read_file_content.content}]]

This is how I was able to fix it.

1: Create a bash script, inside the script you need to do logic to clone the repository and get the tag you want to get. i.e:
external_script.sh

while getopts a: flag
do
  case "${flag}" in
    a) git_repository=${OPTARG};;
  esac
done
# Check out the repository to a temporary directory
temp_folder="temp"
mkdir "$temp_folder"
git clone "${git_repository}" "$temp_folder"
cd "$temp_folder"
git fetch --tags
last_git_tag=$(git describe --tags --abbrev=0)
# Export last_git_tag value to txt file
cd ..
echo "$last_git_tag" > "latest-git-tag-value.txt"

In main.tf:

resource "null_resource" "get_last_git_tag_created" {
  provisioner "local-exec" {
    command = "bash ./external_script.sh -a $repository_link"
    interpreter = ["bash", "-c"]
    environment = {
      repository_link = "your_repo_url"
    }
  }
}

Read the value of the last git tag:

data "local_file" "read_file_content" {
    filename = "./latest-git-tag-value.txt"
    depends_on = [null_resource.get_last_git_tag_created]
}

Now, the value is in data.local_file.read_file_content, So the event trigger should looks like this:

"referenceType" = ["tag"]

"referenceName" = [{ "prefix": data.local_file.read_file_content.content }]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文