You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在AWS CodePipeline中读取SourceCodeArtifact里JSON文件的字段值?

如何在AWS CodePipeline中读取SourceArtifact里的JSON字段并传递给CloudFormation

要实现这个需求,CodePipeline本身没有直接读取Artifact文件内容的能力,所以需要借助中间步骤来提取JSON字段的值。CodeBuild是最直接且易维护的方案,当然你也可以用Lambda或Step Functions,但CodeBuild在处理文件操作和参数传递上更简单直观。下面是具体的实现步骤:

方案一:使用CodeBuild提取参数并传递

1. 创建CodeBuild项目并编写buildspec.yml

首先在你的GitHub仓库根目录添加一个buildspec.yml文件,用来读取imageManifest.json并输出image_version作为可被后续阶段读取的参数:

version: 0.2
phases:
  build:
    commands:
      # 用jq工具读取JSON文件中的image_version字段,存入环境变量
      - IMAGE_VERSION=$(jq -r '.image_version' imageManifest.json)
      # 将参数写入到输出文件,方便后续CodePipeline阶段读取
      - echo "{\"ImageVersion\": \"$IMAGE_VERSION\"}" > build-output.json
artifacts:
  files:
    - build-output.json

注意:CodeBuild的默认环境镜像已经包含jq工具,如果你的自定义镜像没有,需要在buildspec的安装步骤里添加apt-get install jq(Debian系)或yum install jq(RHEL系)来安装。

2. 在CodePipeline中插入CodeBuild阶段

在你的Pipeline的GitHubSource和DevQA阶段之间,新增一个CodeBuild阶段,用来处理SourceArtifact并输出包含参数的新Artifact:

- Name: ExtractImageVersion
  Actions:
  - Name: ExtractVersionAction
    ActionTypeId:
      Category: Build
      Owner: AWS
      Provider: CodeBuild
      Version: '1'
    InputArtifacts:
    - Name: SourceCodeArtifact
    OutputArtifacts:
    - Name: BuildOutputArtifact
    Configuration:
      ProjectName: !Ref YourCodeBuildProjectName

3. 修改Deploy阶段的CloudFormation配置

现在你可以在Deploy阶段通过CodePipeline的内置函数GetParam,读取CodeBuild输出的build-output.json里的参数:

- Name: DevQA
  Actions:
  - Name: DeployInfrastructure
    InputArtifacts:
    - Name: SourceCodeArtifact
    - Name: BuildOutputArtifact  # 添加这个输入Artifact
    ActionTypeId:
      Category: Deploy
      Owner: AWS
      Provider: CloudFormation
      Version: '1'
    Configuration:
      StackName: !Ref AppName
      Capabilities: CAPABILITY_NAMED_IAM
      RoleArn: !GetAtt [CloudFormationRole, Arn]
      ParameterOverrides: !Sub '{"ImageId": "${!GetParam(BuildOutputArtifact, build-output.json, ImageVersion)}"}'

!GetParam的语法是${!GetParam(ArtifactName, FilePath, JsonKey)},专门用来从指定Artifact的JSON文件中提取对应字段的值。

方案二:使用Lambda自定义动作(可选)

如果你需要更灵活的自定义逻辑(比如字段验证、格式转换),可以用Lambda来处理。核心思路是让Lambda下载SourceArtifact中的imageManifest.json,解析后将image_version作为输出变量返回给CodePipeline。

比如Python版Lambda的核心代码:

import json
import boto3
from botocore.exceptions import ClientError

s3 = boto3.client('s3')

def lambda_handler(event, context):
    # 获取CodePipeline传递的Artifact存储信息
    artifact = event['CodePipeline.job']['data']['inputArtifacts'][0]
    bucket = artifact['location']['s3Location']['bucketName']
    key = artifact['location']['s3Location']['objectKey']
    
    # 下载并解析JSON文件
    try:
        response = s3.get_object(Bucket=bucket, Key=key)
        manifest = json.loads(response['Body'].read().decode('utf-8'))
        image_version = manifest['image_version']
    except ClientError as e:
        # 通知CodePipeline任务失败
        put_job_failure(event, context, str(e))
        return
    
    # 将参数作为输出变量返回给CodePipeline
    put_job_success(event, context, {'ImageVersion': image_version})

def put_job_success(event, context, variables):
    codepipeline = boto3.client('codepipeline')
    codepipeline.put_job_success_result(
        jobId=event['CodePipeline.job']['id'],
        outputVariables=variables
    )

def put_job_failure(event, context, message):
    codepipeline = boto3.client('codepipeline')
    codepipeline.put_job_failure_result(
        jobId=event['CodePipeline.job']['id'],
        failureDetails={'message': message, 'type': 'JobFailed'}
    )

之后在Pipeline中添加一个自定义动作阶段调用这个Lambda,Deploy阶段就可以通过${!Variables.ImageVersion}来引用这个变量。

总结

  • 优先选CodeBuild:无需编写复杂的Lambda代码,用buildspec就能完成文件读取和参数输出,维护成本更低,适合大多数场景。
  • Lambda适合复杂逻辑:如果需要额外的自定义处理(比如字段校验、多文件合并),可以考虑使用Lambda方案。

内容的提问来源于stack exchange,提问作者Rob O'Doherty

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 17:17:49