Skip to content

Commit

Permalink
#GOV-527 Build and push the Docker Image to Dockerhub (#11)
Browse files Browse the repository at this point in the history
* Updated config to build and push the docker image to dockerhub

* CircieCI to build and push docker image to dockerhub upon github tag release and latest

* remove imagetag from latest build

---------

Co-authored-by: Somanath Hugar <[email protected]>
  • Loading branch information
somanath21 and Somanath Hugar authored Aug 1, 2023
1 parent 86563af commit 7ae1405
Showing 1 changed file with 73 additions and 37 deletions.
110 changes: 73 additions & 37 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,47 +1,83 @@
version: 2.1
orbs:
slack: circleci/[email protected]
aws-ecr: circleci/[email protected]
jobs:
build:
executors:
docker-executor:
docker:
- image: cimg/openjdk:17.0.0
- image: docker:17.05.0-ce-git
working_directory: ~/repo
- image: circleci/openjdk:17-buster-node-browsers-legacy

jobs:
build_and_push_tag_image:
executor: docker-executor
environment:
JVM_OPTS: -Xmx512m
TERM: dumb
GITHUB_TOKEN: ${GITHUB_TOKEN} # Add the GitHub token as an environment variable

steps:
- checkout
- setup_remote_docker:
version: 20.10.14
- run:
name: Build and Push Docker tag Image
command: |
# Set environment variables
IMAGE_TAG=$CIRCLE_TAG
# Check if the Docker image with the same tag already exists in Docker Hub
if curl -s -f -u "$DOCKERHUB_USERNAME":"$DOCKERHUB_PASSWORD" "https://hub.docker.com/v2/repositories/openmf/ph-ee-connector-mojaloop/tags/$IMAGE_TAG" > /dev/null; then
echo "Skipping the build and push as the tag $IMAGE_TAG already exists in Docker Hub."
exit 0
fi
# Build and tag the Docker image
./gradlew bootJar
docker build -t "openmf/ph-ee-connector-mojaloop:$IMAGE_TAG" .
# Push the Docker image to Docker Hub
docker login -u "$DOCKERHUB_USERNAME" -p "$DOCKERHUB_PASSWORD"
docker push "openmf/ph-ee-connector-mojaloop:$IMAGE_TAG"
# when: always # The job will be executed even if there's no match for the tag filter

build_and_push_latest_image:
executor: docker-executor
environment:
# Customize the JVM maximum heap limit
JVM_OPTS: -Xmx512m
TERM: dumb

steps:
- checkout
- setup_remote_docker
# - slack/notify:
# event: fail
# mentions: '@here'
# template: basic_fail_1
# - slack/notify:
# event: pass
# template: basic_success_1
- run: ./gradlew clean bootJar
- aws-ecr/build-and-push-image:
aws-access-key-id: AWS_ACCESS_KEY_ID
aws-secret-access-key: AWS_SECRET_ACCESS_KEY
extra-build-args: '--compress'
push-image: true
region: ap-south-1
registry-id: AWS_REGISTRY_ID
repo: phee-mojaloop
repo-scan-on-push: true
role-arn: arn:aws:iam::419830066942:role/CustomAdmin
tag: latest
# - run: ./gradlew cucumberCli
# run tests! Slack Success/Fail Notification Step
#- run: ./gradlew test
# Install Docker to build and push the image
- setup_remote_docker:
version: 20.10.14

# Build the Docker image
- run:
name: Build Docker image
command: |
./gradlew bootJar
docker build -t openmf/ph-ee-connector-mojaloop:latest .
# Log in to DockerHub using environment variables
- run:
name: Login to DockerHub
command: echo "${DOCKERHUB_PASSWORD}" | docker login -u "${DOCKERHUB_USERNAME}" --password-stdin

# Push the Docker image to DockerHub
- run:
name: Push Docker image to DockerHub
command: docker push openmf/ph-ee-connector-mojaloop:latest

workflows:
build_and_push_image:
version: 2
build-and-push:
jobs:
- build:
- build_and_push_tag_image:
filters:
tags:
only: /^v\d+\.\d+\.\d+$/ # Match tags in the format v1.2.3
context:
- DOCKER
- build_and_push_latest_image:
context:
- AWS
- slack

- DOCKER

0 comments on commit 7ae1405

Please sign in to comment.