Skip to content

MrTomerLevi/jenkins-pipeline-aws-shared-lib

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

67 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Jenkins Pipeline AWS Shared Library

This project provides easy AWS shared library which can be used in Jenkins pipeline code. References:

Setup instructions

In Jenkins, go to Manage Jenkins → Configure System. Under Global Pipeline Libraries, add a library with the following settings:

Then create a Jenkins job with the following pipeline (note that the underscore _ is not a typo):

@Library('jenkins-pipeline-aws-shared-lib')_

def aws = new com.jenkins.aws.Pipeline()

docker.image('garland/aws-cli-docker').inside {
    withAWS(credentials: 'aws-credentials',region: 'us-east-1'){
                    
        stage('Create CloudFormation Stack'){
            def cfTemplate = "./cloudformation.yaml"
            java.util.List<String> capabilities = ["CAPABILITY_IAM"]
            def s3Bucket = "my-s3-bucket-name"
            
            java.util.Map parameters = [
                "EnvName"          : params.environment_name,
                "OtherParameter"   : "1234"
            ]
            def stackId = aws.cloudFormationCreateOrUpdateStack("myStackName", cfPackedTemplate, parameters, capabilities)
            println("Stack id: ${stackId}")
            assert packingStatus != 2  : "CloudFormation update failed"
        }
    }
}
@Library('jenkins-pipeline-aws-shared-lib')_

def aws = new com.jenkins.aws.Pipeline()

docker.image('garland/aws-cli-docker').inside {
    withAWS(credentials: 'aws-credentials',region: 'us-east-1'){
                    
        stage('Package and Create CloudFormation Stack'){
              def cfTemplate = "./cloudformation.yaml"
              def cfPackedTemplate = "./cloudformation-packed.yaml"
              java.util.List<String> capabilities = ["CAPABILITY_IAM", "CAPABILITY_NAMED_IAM" ,"CAPABILITY_AUTO_EXPAND"]

              stackExist = aws.cloudFormationStackExist("myStackName")
              println("CloudFormation Stack already exist: ${stackExist}")

              def s3Bucket = "my-s3-bucket-name"
              def packingStatus = aws.cloudFormationPackage(s3Bucket, "ready-templates-folder", cfPackedTemplate, cfPackedTemplate)
              println("CloudFormation Stack packing status code is: ${packingStatus}")
              assert packingStatus != 255  : "CloudFormation packing failed"

              def stackId = aws.cloudFormationCreateOrUpdateStack("myStackName", cfPackedTemplate, bijoInfraCfParams, capabilities)
             println("Stack id: ${stackId}")
             assert packingStatus != 2  : "CloudFormation update failed"
        }
    }
}
@Library('jenkins-pipeline-aws-shared-lib')_

def aws = new com.jenkins.aws.Pipeline()

docker.image('garland/aws-cli-docker').inside {
    withAWS(credentials: 'aws-credentials',region: 'us-east-1'){
                    
        stage('Get or create ECS cluster'){
            ecrLogin = aws.awsECRGetLogin()
            println ecrLogin
        }
    }
}

Or:

@Library('jenkins-pipeline-aws-shared-lib') import com.jenkins.aws.Pipeline

def aws = new Pipeline()

//...

For template utils use:

@Library('jenkins-pipeline-aws-shared-lib') _ 

def template = new com.jenkins.utis.Template()

node {
    stage('Generate worker from template'){
                def binding = [:]
                binding.workerId    = "12345"
                binding.workerName  = "Tomer"

    
                def templateFile = "worker.template"
                def dstFile      = "worker.json"
                template.createFromTemplate(templateFile, binding ,dstFile)
    }
}