Skip to content

Instantly share code, notes, and snippets.

@ahmetgeymen
Last active August 13, 2020 22:15
Show Gist options
  • Save ahmetgeymen/7db60c74348332527905f9680962217f to your computer and use it in GitHub Desktop.
Save ahmetgeymen/7db60c74348332527905f9680962217f to your computer and use it in GitHub Desktop.
Bitbucket Pipelines: Build Docker Image + GCP Image Push || AWS Image Push
image: openjdk:11-jdk-slim
definitions:
caches:
gradlewrapper: ~/.gradle/wrapper
steps:
- step: &lint
name: Lint
caches:
- gradle
- gradlewrapper
script:
- ./gradlew lintKotlin
- step: &test
name: Test
caches:
- gradle
- gradlewrapper
script:
- ./gradlew test
- step: &assemble
name: Assemble
caches:
- gradle
- gradlewrapper
script:
- ./gradlew assemble
artifacts:
- build/libs/*.jar
- step: &build-image
name: Build Docker image
services:
- docker
caches:
- docker
script:
# build the image
- export IMAGE_NAME=$GCP_IMAGE_NAME # alternatively can be set as AWS image name
- docker build -t $IMAGE_NAME --build-arg JAR_FILE=build/libs/app.jar . # consider file name as jar artifact
- docker save --output docker.tar $IMAGE_NAME
artifacts:
- docker.tar
- step: &push-image-to-gcp
name: Push image
image: google/cloud-sdk:alpine
services:
- docker
caches:
- docker
script:
# backup docker image
- docker load --input docker.tar
# Authenticating with the service account key file
- echo $GCP_API_KEYFILE | base64 -d > ./gcloud-api-key.json
- gcloud auth activate-service-account --key-file gcloud-api-key.json
- gcloud config set project $GCP_PROJECT_ID
# Tag docker image
- export IMAGE_NAME=$GCP_REGISTRY_HOSTNAME/$GCP_PROJECT_ID/$GCP_IMAGE_NAME
- docker tag $GCP_IMAGE_NAME ${IMAGE_NAME}
# Login to google docker hub
- cat ./gcloud-api-key.json | docker login -u _json_key --password-stdin https://$GCP_REGISTRY_HOSTNAME
- docker push ${IMAGE_NAME}
- step: &push-image-to-aws
name: Push image
services:
- docker
caches:
- docker
script:
# backup docker image
- docker load --input docker.tar
# use the pipe to push the image to AWS ECR
- pipe: atlassian/aws-ecr-push-image:1.1.2
variables:
AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID # Optional if already defined in the context.
AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY # Optional if already defined in the context.
AWS_DEFAULT_REGION: $AWS_DEFAULT_REGION # Optional if already defined in the context.
IMAGE_NAME: $AWS_IMAGE_NAME
pipelines:
pull-requests:
'**':
- step: *lint
- step: *test
branches:
master:
- step: *lint
- step: *test
- step: *assemble
- step: *build-image
- step: *push-image-to-gcp
- step: *push-image-to-aws
@ahmetgeymen
Copy link
Author

ahmetgeymen commented Jun 12, 2020

Environment variables:

GCP_API_KEYFILE: GCP service account api key json file as base64 encoded format
GCP_REGISTRY_HOSTNAME: GCP Container Registry hostname
GCP_PROJECT_ID: GCP project id
GCP_IMAGE_NAME: image name

AWS_ACCESS_KEY_ID: AWS access key id
AWS_SECRET_ACCESS_KEY: AWS secret key
AWS_DEFAULT_REGION: AWS region
AWS_IMAGE_NAME: The name of the image to push to the ECR. The name should be the same as your ECR repository name. Remember that you don't need to add your registry URL in front of the image name, the pipe will fetch this URL from AWS and add it to the image tag for you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment