简体   繁体   English

通过 aws-cdk 触发 aws-amplify 构建

[英]Trigger an aws-amplify build via aws-cdk

I am creating a aws-amplify app via aws-cdk and everything works fine except it doesn't start a build automatically.我正在通过 aws-cdk 创建一个 aws-amplify 应用程序,一切正常,除了它不会自动启动构建。 If I do a git commit (I have enabled continuous deploys) it will build and run just fine.如果我执行 git 提交(我启用了连续部署),它将构建并运行得很好。 But on a new aws account with a cdk deploy I have to start the first commit manually...但是在一个带有 cdk 部署的新 aws 帐户上,我必须手动开始第一次提交......

I had this issue as well while working on the deployment of an amplify hosted application.在部署放大托管应用程序时,我也遇到了这个问题。

After going through the AWS CLI documentation for Amplify, I found the start-job command.在查看 Amplify 的 AWS CLI 文档后,我找到了start-job命令。 This command allows you to start an amplify job for a specific branch.此命令允许您为特定分支启动放大作业。

You can then create an AwsCustomResource that makes an SDK call to start-job.然后,您可以创建一个AwsCustomResource来调用 SDK start-job。

This is what I ended up with.这就是我最终得到的。

const build_trigger = new customResource.AwsCustomResource(this, 'triggerAppBuild', {
    policy: customResource.AwsCustomResourcePolicy.fromSdkCalls({
        resources: customResource.AwsCustomResourcePolicy.ANY_RESOURCE
    }),
    onCreate: {
        service: 'Amplify',
        action: 'startJob',
        physicalResourceId: customResource.PhysicalResourceId.of('app-build-trigger'),
        parameters: {
            appId: amplifyApp.appId,
            branchName: master.branchName,
            jobType: 'RELEASE',
            jobReason: 'Auto Start build',
        }
    },
});

Note that you should replace amplifyApp.apiId and master.branchName with your own app ID and branch name.请注意,您应该将amplifyApp.apiIdmaster.branchName替换为您自己的应用程序 ID 和分支名称。 I am using the amplify-alpha, so you may need to get the App ID and branch name another way.我正在使用 amplify-alpha,因此您可能需要通过其他方式获取 App ID 和分支名称。

Amplify app builds are triggered by push events. Amplify 应用程序构建由推送事件触发。 Use a CDK Custom Resource or its simpler cousin Trigger to generate a push event during the CDK stack creation.使用 CDK 自定义资源或其更简单的同类触发器在 CDK 堆栈创建期间生成推送事件。

For a github repo, for instance, your Trigger construct's lambda would call the Github webhook test API, which will trigger the hook with the latest push to the current repository .例如,对于 github 存储库,您的Trigger构造的 lambda 将调用 Github webhook 测试 API,这将通过最新推送到当前存储库触发挂钩 Both Custom Resources and Triggers can be configured to run only on stack creation.自定义资源和触发器都可以配置为仅在堆栈创建时运行。 Remember to give your lambda any necessary repo credentials (eg set environment variables via a secret).请记住向您的 lambda 提供任何必要的回购凭据(例如,通过秘密设置环境变量)。

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

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