Zee
Zee

Reputation: 21

How to run my jenkins pipeline code in AWS CodeBuild?

I can trigger my AWS pipeline from jenkins but I don't want to create buildspec.yaml and instead use the pipeline script which already works for jenkins.

Upvotes: 2

Views: 3667

Answers (3)

Razr
Razr

Reputation: 1

@hynespm - excellent example mate.

Here is another one based off yours but with stripIndent() and "withAWS" to switch roles:

#!/usr/bin/env groovy

def cbResult = null

pipeline {
.
.
.
                script {
                        echo ("app_version TestwithAWS value : " + "${app_version}")

                        String buildspec = """\
                        version: 0.2
                        env:
                            parameter-store:
                                TOKEN: /some/token
                        phases:
                            pre_build:
                                commands:
                                - echo "List files...."
                                - ls -l
                                - echo "TOKEN is ':' \${TOKEN}"
                            build:
                                commands:
                                - echo "build':' Do something here..."
                                - echo "\${CODEBUILD_SRC_DIR}"
                                - ls -l "\${CODEBUILD_SRC_DIR}"
                            post_build:
                                commands:
                                - pwd
                                - echo "postbuild':' Done..."
                        """.stripIndent()

                        withAWS(region: 'ap-southeast-2', role: 'CodeBuildWithJenkinsRole', roleAccount: '123456789123', externalId: '123456-2c1a-4367-aa09-7654321') {
                            sh 'aws ssm get-parameter --name "/some/token"'
                            try {
                            cbResult = awsCodeBuild projectName: 'project-lambda',
                                sourceControlType: 'project',
                                credentialsType: 'keys',
                                awsAccessKey: env.AWS_ACCESS_KEY_ID,
                                awsSecretKey: env.AWS_SECRET_ACCESS_KEY,
                                awsSessionToken: env.AWS_SESSION_TOKEN,
                                region: 'ap-southeast-2',
                                envVariables: '[ { GITHUB_OWNER, special }, { GITHUB_REPO, project-lambda } ]',
                                artifactTypeOverride: 'S3', 
                                artifactLocationOverride: 'special-artifacts',
                                overrideArtifactName: 'True',
                                buildSpecFile: buildspec
                            } catch (Exception cbEx) {
                                cbResult = cbEx.getCodeBuildResult()
                            }
                        }
                } //script

.
.
.
}

Upvotes: 0

hynespm
hynespm

Reputation: 671

In order to user Codebuild you need to provide the Codebuild project with a buildspec.yaml file along with your source code or incorporate the commands into the actual project.

However, I think you are interested in having the creation of the buildspec.yaml file done within the Jenkins pipeline.

Below is a snippet of a stage within a Jenkinsfile, it creates a build spec file for building docker images and then sends the contents of the workspace to a codebuild project. This uses the plugin for Codebuild.

stage('Build - Non Prod'){

    String nonProductionBuildSpec = """
version: 0.1

phases:
  pre_build:
    commands:
      - \$(aws ecr get-login --registry-ids <number> --region us-east-1)
  build:
    commands:
      - docker build -t ces-sample-docker .
      - docker tag $NAME:$TAG <account-number>.dkr.ecr.us-east-1.amazonaws.com/$NAME:$TAG
  post_build:
    commands:
      - docker push <account-number>.dkr.ecr.us-east-1.amazonaws.com/$NAME:$TAG
""".replace("\t","    ")
    writeFile file: 'buildspec.yml', text: nonProductionBuildSpec

    //Send checked out files to AWS
    awsCodeBuild projectName: "<codebuild-projectname>",region: "us-east-1", sourceControlType: "jenkins"

}

I hope this gives you an idea of whats possible.

Good luck! Patrick

Upvotes: 2

Clare Liguori
Clare Liguori

Reputation: 1650

You will need to write a buildspec for the commands that you want AWS CodeBuild to run. If you use the CodeBuild plugin for Jenkins, you can add that to your Jenkins pipeline and use CodeBuild as a Jenkins build slave to execute the commands in your buildspec.

See more details here: https://docs.aws.amazon.com/codebuild/latest/userguide/jenkins-plugin.html

Upvotes: 0

Related Questions