Skip to content

Instantly share code, notes, and snippets.

@waltervargas
Created June 28, 2017 17:11
Show Gist options
  • Save waltervargas/5ec0b17c1bc86542c3519056a64b6a68 to your computer and use it in GitHub Desktop.
Save waltervargas/5ec0b17c1bc86542c3519056a64b6a68 to your computer and use it in GitHub Desktop.
def service = "microservicea"
node('master') {
stage('Checkout') {
checkout poll:false, scm: [
$class: 'GitSCM', branches: [[name: "${GIT_BRANCH}"]], userRemoteConfigs: [[ url: "${GIT_URL}" ]]
]
}
stage('Build') {
docker.build(service)
}
stage('Push') {
def region = _get_aws_region()
def account_id = _get_aws_account_id(region)
def repo = service
def registry = _get_aws_ecr_url(region, account_id, repo)
def img = service
docker.withRegistry(registry){
// Login to ECR
_ecr_login(region)
// Docker Tag
docker.image(img).tag("latest")
// Docker Push
docker.image(img).push("latest")
}
}
}
def _get_aws_region() {
def region = sh (
script: 'curl -s http://169.254.169.254/latest/dynamic/instance-identity/document | jq .region -r',
returnStdout: true
).trim()
return region
}
def _get_aws_account_id(region){
def account_id = sh (
script: "aws ec2 --region ${region} describe-security-groups --query 'SecurityGroups[0].OwnerId' --output text",
returnStdout: true
).trim()
return account_id
}
def _get_aws_ecr_url(region, account_id, repo){
return "https://${account_id}.dkr.${region}.amazonaws.com/${repo}"
}
def _ecr_login(region){
def get_login = "aws ecr get-login --region ${region}"
def get_login_stdout = sh (script: get_login, returnStdout: true).trim()
sh (script: get_login_stdout, returnStdout: false)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment