使用Terraform在GitHub v2源码操作中构建CodePipeline以获取代码

首先

在试图将GitHub和AWS CodePipeline进行集成时,我了解到GitHub v1源动作已被弃用,并且v2被推荐使用。
v1通过从GitHub获得OAuth令牌并在Webhook上进行集成。
v2现在要求使用CodeStar SourceConnection与GitHub进行集成。

我成功地在Terraform上成功地将GitHub v2源操作与其整合,因此我将记录下来。我将用这篇文章重点讲述如何将GitHub v2源操作配置到CodePipeline的源阶段中。

我已经在KensukeTakahara/terraform-codepipeline-github中发布了实现的代码。
在这个存储库中,我构建了包括构建和部署阶段的CodePipeline。

执行环境

使用的 Terraform 版本为1.0.5,AWS Provider 版本为3.58.0。

使用Terraform进行实现

我会对下面的资源进行实施。

    • S3 (アーティファクト格納用)

 

    • CodeStar Connection

 

    CodePipeline

S3(用于存储艺术品)

resource "aws_s3_bucket" "artifact" {
  bucket = "terraform-codepipeline-github-artifact"
}

CodePileline 需要一个 S3 存储桶来存储构件。
在 CodePileline 中,当将构件传递到下一个阶段时,会通过 S3 存储桶进行传递。
为了确保存储桶在区域内是唯一的,需要设置一个唯一的值作为存储桶的名称。

代码星连接

resource "aws_codestarconnections_connection" "github" {
  name          = "github-connection"
  provider_type = "GitHub"
}

请在 provide_type 中指定 “GitHub”。

代码管道

resource "aws_codepipeline" "example" {
  name     = "example"
  role_arn = aws_iam_role.codepipeline.arn

  stage {
    name = "Source"

    action {
      name             = "Source"
      category         = "Source"
      owner            = "AWS"
      provider         = "CodeStarSourceConnection"
      version          = 1
      output_artifacts = ["Source"]

      configuration = {
        ConnectionArn        = aws_codestarconnections_connection.github.arn
        FullRepositoryId     = "KensukeTakahara/terraform-codepipeline-github"
        BranchName           = "main"
        OutputArtifactFormat = "CODEBUILD_CLONE_REF"
      }
    }
  }

  # Build, Deploy ステージの記述は省略
  # terraform apply 実行時には必須

  artifact_store {
    location = aws_s3_bucket.artifact.id
    type     = "S3"
  }
}

在提供者中指定”CodeStarSourceConnection”。
在配置中指定存储库名称和分支名称。
当在构建阶段使用CodeBuild时,应将OutputArtifactFormat指定为”CODEBUILD_CLONE_REF”。
如果指定为”CODE_ZIP”,则无法生成用于CodeBuild的元数据,并且构建阶段将失败。
参考资料

data "aws_iam_policy_document" "codepipeline" {
  statement {
    effect    = "Allow"
    resources = ["*"]

    actions = [
      "s3:PutObject",
      "s3:GetObject",
      "s3:GetObjectVersion",
      "s3:GetBucketVersioning",
      "codestar-connections:UseConnection"
    ]
  }
}

resource "aws_iam_policy" "codepipeline" {
  name   = "example-codepipeline-s3-policy"
  policy = data.aws_iam_policy_document.codepipeline.json
}

data "aws_iam_policy_document" "assume_role" {
  statement {
    actions = ["sts:AssumeRole"]

    principals {
      type        = "Service"
      identifiers = ["codepipeline.amazonaws.com"]
    }
  }
}

resource "aws_iam_role" "codepipeline" {
  name               = "example-codepipeline-role"
  assume_role_policy = data.aws_iam_policy_document.assume_role.json
}

resource "aws_iam_role_policy_attachment" "example" {
  role       = aws_iam_role.codepipeline.name
  policy_arn = aws_iam_policy.codepipeline.arn
}

在IAM角色中,设置对Artifact存储桶和CodeStar Connection的访问权限。
由于这仅是对Source阶段所需权限的设置,请根据需要添加Build和Deploy阶段所需的IAM策略。

提供

在执行 “terraform apply” 并在 AWS 环境中进行配置时,CodePipeline 的构建尚未完成。
需要进行 GitHub 侧的身份验证。

スクリーンショット 2021-09-12 22.57.32.png
スクリーンショット 2021-09-11 12.02.24.png
スクリーンショット 2021-09-11 12.05.22.png
スクリーンショット 2021-09-11 12.05.54.png
スクリーンショット 2021-09-11 12.06.55.png
スクリーンショット 2021-09-11 23.05.05.png
スクリーンショット 2021-09-11 23.06.39.png
スクリーンショット 2021-09-11 12.08.10.png
スクリーンショット 2021-09-12 22.58.54.png

总结

我们已经成功在Terraform上实现了安全的GitHub v2源操作。在Terraform上,资源的配置几乎可以完成,只需在AWS管理控制台上完成GitHub方面的身份验证。因此,实施起来简单且易于管理。

广告
将在 10 秒后关闭
bannerAds