How do I create an AWS CodeCommit repository that uses a specified default branch name using AWS CloudFormation?

4 minutos de lectura
2

I want to create an AWS CodeCommit repository that uses a specified default branch name using AWS CloudFormation?

Short description

On January 19, 2021, AWS changed the name of the default branch in CodeCommit from master to main. This name change affects the default behavior of CodeCommit when you do the following with a new repository that you create:

  • Commit to a repository using the CodeCommit console, APIs, or the AWS Command Line Interface (AWS CLI).
  • Create a repository with an AWS CloudFormation template and add code when you create the template.
    Important: This impact affects AWS CloudFormation templates after February 8, 2021.
  • Create and commit to a repository with the AWS Cloud Development Kit (AWS CDK).
    Important: This impact affects AWS CDK templates after February 8, 2021.

To avoid any impacts from the name change, you can do either of the following:

  • Use the BranchName property of the AWS::CodeCommit::Repository Code resource in your AWS CloudFormation templates to specify the name of the default branch that your code expects.
  • Modify your code so that it expects the new default main branch name.

Important: The name change affects only repositories created using AWS CloudFormation or AWS CDK templates after February 8, 2021.

Note: If you receive errors when running AWS CLI commands, make sure that you’re using the most recent AWS CLI version.

Resolution

Choose one of the following resolutions depending on whether you're using AWS CloudFormation or the AWS CDK.

Update your AWS CloudFormation templates to create a default branch with a specific name

1.    Update your stack template, and set the BranchName property of the CodeCommit repository to your branch name. See the following YAML and JSON examples:

YAML:

  MyRepoResource:
    Type: AWS::CodeCommit::Repository
    Properties:
      RepositoryName: MyRepo
      Code:
        BranchName: DefaultBranchName
        S3: 
          Bucket: MySourceCodeBucket,
          Key: MyKey

JSON:

 {
    "MyRepoResource": {
        "Type": "AWS::CodeCommit::Repository",
        "Properties": {
            "RepositoryName": "MyRepo",
            "Code": {
                "BranchName": "DefaultBranchName",
                "S3": {
                    "Bucket": "MySourceCodeBucket",
                    "Key": "MyKey"
                }
            }
        }
    }
}

Important: In your YAML and JSON templates, make the following updates:

Set MyRepo to the name of the CodeCommit repository that you're creating. Set DefaultBranchName to the default branch that you'll use to import code into a repository. Set MySourceCodeBucket to the name or Amazon Resource Name (ARN) of the Amazon Simple Storage Service (Amazon S3) bucket that contains the .zip file with the content that you're committing to the new repository. Set MyKey to the key that identifies the object in Amazon S3.

2.    Use the updated template whenever you create a new AWS CloudFormation stack.

Now, when you create a new stack, your AWS CloudFormation template creates a new repository for you. Then, CodeCommit pushes your code to a default branch with the specific name that you defined in your template.

Update your AWS CDK code to create a CodeCommit repository with a specified default branch name

Update your AWS CDK code to specify a BranchName using the CfnRepository.CodeProperty.BranchName property. Then, BranchName becomes the name for the default branch of the repository when your code creates and pushes the initial commit to that repository.

See the following TypeScript example for setting the BranchName when you create a CodeCommit repository:

import * as cdk from '@aws-cdk/core';
import codecommit = require('@aws-cdk/aws-codecommit');
export class CdkCodecommitStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);
    // The code creates a new CodeCommit repository with Branch name DefaultBranchName
      new codecommit.CfnRepository(this, 'MyRepoResource', {
            repositoryName: "MyRepo",
            code: {
              "branchName": "DefaultBranchName",
              "s3": {
                "bucket": "MySourceCodeBucket",
                "key": "MyKey"
              }
            },
        });
  }
}

Important: In the preceding TypeScript example, make the following updates:

Set MyRepo to the name of the CodeCommit repository that you're creating. Set DefaultBranchName to the default branch that you'll use to import code into a repository. Set MySourceCodeBucket with the name or ARN of the S3 bucket that contains the .zip file with the content that you're committing to the new repository. Set MyKey to the key that identifies the object in Amazon S3.

Note: To convert the sample into another language supported by the AWS CDK, see Translating TypeScript AWS CDK code to other languages.

2.    Use the updated AWS CDK code whenever you deploy AWS CloudFormation stacks.

Now, AWS CloudFormation creates a new repository for you. Then, CodeCommit pushes the code in the S3 object to a default branch with the specific name that you defined in your AWS CDK app.


OFICIAL DE AWS
OFICIAL DE AWSActualizada hace 3 años