How to omit CODEBUILD_SRC_DIR from codebuild output artifact? - amazon-web-services

I have a CodePipeline which has a few stages, both Plan and Check are CodeBuild projects:
Source (Github) > Plan > Check
The files from the github source are used in plan and then at the end of the plan I use output artifact to export all the files in CODEBUILD_SRC_DIR using this:
files:
- '$CODEBUILD_SRC_DIR/**/*'
name: PlanArtifact
In the export s3 bucket the files are outputted in the same pathways so CODEBUILD_SIR_DIR/all-files
I want to export the artifact so the s3 bucket is just immediatelly all-files, i want to omit the CODEBUILD_SRC_DIR as it changes with each codebuild and I just need the files in the .zip to use. I tried playing around with the below code but it doesn't seem to work:
base-directory: '$CODEBUILD_SRC_DIR'
Can anyone help?

If you remove the $CODEBUILD_SRC_DIR variable from the files it should work:
artifacts:
files:
- '**/*'
name: PlanArtifact
CodeBuild will always look for the artifacts in the original build location (ie. $CODEBUILD_SRC_DIR), so there is no need to include it into the path.
base-directory can be used if you only want one or more subdirectories as artifact. If you set it, CodeBuild will go to that directory first (starting from the original build location, so it should be a relative path, too) and then start to resolve the files pattern.
artifacts/files in the buildspec reference
artifacts/base-directory in the buildspec reference

Related

CodePipeline not saving all files in source artifacts

I've set up a new pipeline in AWS CodePipeline, and connected it to my GitHub account. I'm getting build errors in CodeBuild because a folder that is in my GitHub repository, static/css/, is missing (I'm using CodeBuild to do a gatsby build).
This is not a folder generated in the build process - this folder and its files exist in a clean repo. I've also checked that the branch is correct (master).
When I inspect the zip file in the SourceArtifacts folder in my S3 bucket, this folder is not there.
Any ideas why CodePipeline is not retrieving, or at least keeping, this subfolder and its contents?
Go to your Github repo and select the Green button "Clone or Download", then download the zip file. This is essentially what CodePipeline is doing to get your Github source. Now inspect the files in the zip file and confirm if 'static' directory is there. If it is not there you need to fix that and get the files into github.
It turned out that the missing folder was listed with an export-ignore attribute in the .gitattributes folder. The static/css folder got zipped up with everything else after removing this attribute.

Using ant with AWS CodeBuild - build.xml does not exist and other questions for a newbie

I am trying to switch over to using CodeBuild to build my code so I can then easily push it to my EC2 instances instead of manually building and copying.
I can manually run ant on my station and all will build as it should.
I am now trying to use the AWS CodeBuild console to try this.
I zipped up my source code files and put it in an S3 bucket and put its location in the source fields of AWS CodeBuild. I have the build.xml in this same bucket and I also put the build.xml in the base of the codes zip file. In the build commands I put "ant".
I assume that the build.xml needs to go somewhere else?
Do I need more then just "ant" in the build commands? That is all I use when i manually build the project.
From what I have read i should be able to zip up my code , put it in the S3 location and CodeBuild will extract it and build it correct?
Also, under "Environment: How to Build" - what is the "Output files" section for? It's not for the artifacts that are built correct?
Any other tips or tricks? I am very new to all of this so any help is appreciated! I just learned about ant this week. This is building a rather large project with many classes being built - Will this cause an issue? Like I stated earlier - I do have it building file if I run it manually on my system.
Here is the error I get when I build through Code Build:
[Container] 2019/03/21 15:32:27 Entering phase BUILD
[Container] 2019/03/21 15:32:27 Running command ant
Buildfile: build.xml does not exist!
Build failed
I figured out my issue - I zipped the build files from the folder level and not the root level. I re-zipped and it can now see the build.xml.
I built again with these changes and it looks like I am close! It failed for the following -
https://forums.aws.amazon.com/ 2019/03/21 20:57:13 Expanding myapp.jar
https://forums.aws.amazon.com/ 2019/03/21 20:57:13 Skipping invalid artifact path myapp.jar
https://forums.aws.amazon.com/ 2019/03/21 20:57:13 Phase complete: UPLOAD_ARTIFACTS Success: false
https://forums.aws.amazon.com/ 2019/03/21 20:57:13 Phase context status code: CLIENT_ERROR Message: no matching artifact paths found
Isn't myapp.jar what the build is creating?
I am very confused as to what the Artifact/name should be - isn't this what is being created from the build? It is asking for an ARN - how can there be an ARN for it when it is not created?
Also very confused as to what the Environment/Output files is? It is required but I have no idea what should go in this field? It states that output files can not be empty. Does this mean it wants all the class files that are being built? If so then this build is creating over 30 class files in multiple locations - that is a ton to list.
Thanks
Ernie
I have it working! I will post my findings for others going that might be struggling -
So I figured out that the "Outputs" means what are all the files and/or directories that you want to go into your final artifact after all is built.
I have two directories that I want in the final jar artifact. One is WebContent and the other is build. They both have multiple sub-directories. I put "WebContent/*,build/*" in the output files field. It gave me a jar artifact but when I open the jar it did not have any sub-directories. In order to get it to include all sub-directories I had to make the output files field with "WebContent/**/*,build/**/*". All sub-directories are now in the zip and it appears as if the build was successful.
Hopefully this can help others out.
Now on to creating a script for this and also getting this to work from GitLab.

aws: .net Core: zip the built code and copy to s3 output bucket

I am a .net developer and using a .net core 2.x application to build and upload the release code to s3 bucket. later that code will be used to deploy to ec2 instance.
I am new to CI/CD using aws and in learning phase.
In order to create CI/CD for my sample project, I gone through some aws tutorials and was able to create the following buildspec.yml file. Using that file I am able to run the successful build.
The problem comes in the phase UPLOAD_ARTIFACTS. I am unable to understand how to create a zip file that will be used to upload to the s3 bucket specified in the build project.
My buildspec.yml files contains the following code, Please help me finding what is wrong or what I am missing.
version: 0.2
phases:
build:
commands:
- dotnet restore
- dotnet build
artifacts:
files:
- target/cicdrepo.zip
- .\bin\Debug\netcoreapp2.1\*
I think I have to add post_build and some commands that will generate the zip file. But don't know the commands.
Following is the output image from the build logs.
your file is good all what you need to do is to create a S3 bucket then
you need to configure your CodeBuild to generate zip (or not) your artifacts for you, and to store it to s3.
this is the step you need to configure:
Edit:
if you want all your files to be copied on the root of your Zip file you can use:
artifacts:
files:
- ...
discard-paths: yes

Artifact from CodeBuild cannot be deployed with CodeDeploy

With CodeBuild I compile my SpringBoot application and put the resulting jar in a folder called deploy/. In my buildspec.yml at the end I provide:
artifacts:
files:
- deploy/*
When I plug this in to CodePipeline, this creates a zip file in S3. But the problem is, that this zip file contains the root folder, i.e. deploy. So the structure of the zip file is:
deploy.zip
- deploy/
- appspec.yml
- app.jar
- ...
This leads to the problem that CodeDeploy cannot find the appspec.yml, becuase it's not looking inside the deploy folder. I've been trying to overcome this providing a zip file as artifact at the end of the build process, but then in the pipeline it just gets zipped again..
Any idea how to solve this would be much appreciated.
There is a new flag you can add in the artifacts section called base-directory to specify a base path for your artifacts package. This will allow you to truncate the deploy/ path out of your artifacts without losing the rest of the structure underneath.
artifacts:
files:
- **/*
base-directory: deploy
http://docs.aws.amazon.com/codebuild/latest/userguide/build-spec-ref.html
You can add discard-paths: yes
artifacts:
files:
- deploy/*
discard-paths: yes
Keep in mind that it discards ALL paths, including sub-directories
Source: http://docs.aws.amazon.com/codebuild/latest/userguide/build-spec-ref.html

Error in AWS codepipeline when deploying ElasticBeanstalk

I've created a pipeline which does the following:
Git changes trigger next action (code build)
Codebuild initiates & builds a docker image from git source
Set latest docker container up on Elasticbeanstalk
The first 2 steps are working fine, git changes initiate a codebuild, the codebuild builds a docker image, and then tries to set it up on Elasticbeanstalk (which fails). The following error is thrown:
Invalid action configuration The action failed because either the
artifact or the Amazon S3 bucket could not be found. Name of artifact
bucket: MY_BUCKET_NAME. Verify that this bucket
exists. If it exists, check the life cycle policy, then try releasing
a change.
In my codebuild project, I've set the artifact location to MY_BUCKET_NAME & named it aws-test-artifact. Is this all I have to do?
I've tried looking around and am unable to find anything on this issue.
I had the same problem. Just changed Input artifacts from BuildArtifact to SourceArtifact in the build stage, and everything worked.
As Adam Loving commented we must add artifacts section.
Adding this section to your buildspec.yml file will make this work.
artifacts:
files:
- '**/*'
From documentation https://docs.aws.amazon.com/codebuild/latest/userguide/build-spec-ref.html#build-spec.artifacts.files adding '**/*' will include all files into the build target.
So I found the fix to this issue! What I had to do was goto codebuild => edit project => Show advanced settings => Artifacts packaging
From here I changed Artifacts packaging to Zip!