0

I have a Terraform configuration that creates a github_repository_webhook and an aws_codepipeline_webhook, and sets up an aws_codepipeline with a source stage that retrieves code from a GitHub repository. The github_repository_webhook is configured to trigger a push event, and the aws_codepipeline_webhook filters for JSON path $.ref with a match of refs/heads/(.*).

I want to capture the branch name from the webhook event and use it in the aws_codepipeline configuration for the Branch field in the source stage action. However, I am not sure how to access the webhook event data to retrieve the branch name.

Is there a way to access the webhook event data and use it to set the Branch field in the aws_codepipeline configuration?

Code sample below:

resource "github_repository_webhook" "source_webhook" {
  repository = var.source_repo

  configuration {
    url          = aws_codepipeline_webhook.codepipeline_source_webhook.url
    content_type = "json"
    insecure_ssl = false
    secret       = var.webhook_secret
  }

  events = ["push"]
}

resource "aws_codepipeline_webhook" "codepipeline_source_webhook" {
  name            = "my-project-webhook"
  authentication  = "GITHUB_HMAC"
  target_pipeline = aws_codepipeline.codepipeline.name
  target_action   = "Source"

  authentication_configuration {
    secret_token = var.webhook_secret
  }

  filter {
    json_path    = "$.ref"
    match_equals = "refs/heads/(.*)"
  }
}

resource "aws_codepipeline" "codepipeline" {
  name     = "my-project-pipeline"
  role_arn = aws_iam_role.codepipeline_role.arn

  artifact_store {
    location = aws_s3_bucket.my-project_codepipeline_bucket.bucket
    type     = "S3"
  }

  stage {
    name = "my-project-pipeline-source"

    action {
      name             = "Source"
      category         = "Source"
      owner            = "ThirdParty"
      provider         = "GitHub"
      version          = "1"
      run_order        = 1
      output_artifacts = ["source_output"]

      configuration = {
        Repo       = var.source_repo
        Branch     = var.source_branch
        OAuthToken = var.source_token
        Owner      = var.source_owner
      }
    }
  }

  stage {
    name = "my-project-pipeline-test"

    action {
      name             = "Test"
      category         = "Test"
      owner            = "AWS"
      provider         = "CodeBuild"
      version          = "1"
      input_artifacts  = ["source_output"]
      output_artifacts = ["test_output"]

      configuration = {
        ProjectName = "my-project-tests"
      }
    }
  }
}
Gean Ribeiro
  • 1,025
  • 2
  • 10
  • 23

0 Answers0