2
votes

I created a Pipeline that takes the code from a CodeCommit repository, builds it via CodeBuild and pushes the code to an S3 bucket.

For my Codebuild I'm using an AWS managed image. aws/codebuild/nodejs:7.0.0

If I start my build manually via the CodeBuild Console and specify the repository I get the repository URL when I run the following command in the buildspec

- printf ${CODEBUILD_SOURCE_REPO_URL}

but If the CodeBuild is triggered automatically by a push to a repository. CODEBUILD_SOURCE_REPO_URL returns nothing

2
So what is the problem here? Does it not build successfully?Asdfg
The build runs fine. But the problem is I want to use the the CODEBUILD_SOURCE_REPO_URL variable to programmatically set the S3 bucket to copy the files to. My repo names and my bucket names are the same. So this would my best option to have one CodeBuild for multiple piplines and sources repos I have 4 different apps, both with a staging and a production version that needs to be uploaded to S3. I can have different codebuild projects with environment variables set and use my custom env vars for the copy. but I'd rather maintain 1 codebuild project than 4Qais Abou Jaoudé
I believe right now you are using aws cli to copy your artifacts to s3 and you are doing it in buildspec.yml file. The right way to do this is to use Artifacts option of CodeBuild to upload artifacts to S3.Asdfg
Yes, this is what I'm doing. How would I upload the artifact to S3? I didn't see any option in the CodeBuild stages that allows to upload the artifacts from the Codebuild to S3.Qais Abou Jaoudé
It is part of buildspec.yml file. You also need to set up bucket and keys to specify where to upload those files when setting up the build project. Look at the example at docs.aws.amazon.com/codebuild/latest/userguide/….Asdfg

2 Answers

1
votes

If you are running the process from codepipeline it would be empty.

Check this out: https://docs.aws.amazon.com/codebuild/latest/userguide/build-env-ref-env-vars.html

CODEBUILD_SOURCE_REPO_URL: The URL to the input artifact or source code repository. For Amazon S3, this is s3:// followed by the bucket name and path to the input artifact. For AWS CodeCommit and GitHub, this is the repository's clone URL. If a build originates from AWS CodePipeline then this may be empty.

0
votes

If you're connecting via CodeCommit, you can split strings to get more useful values such as Account Id and Repo Name using:

echo "Region = ${AWS_REGION}"
echo "Account Id = $(echo $CODEBUILD_BUILD_ARN | cut -f5 -d ':')"
echo "Repo Name = $(echo $CODEBUILD_SOURCE_VERSION | cut -f2 -d '/')"
echo "Commit Id = ${CODEBUILD_RESOLVED_SOURCE_VERSION}"

Which outputs:

Region = us-west-2
Account Id = 0123456789
Repo Name = my-app
Commit Id = a46218c9160f932f2a91748a449b3f9818964642