diff --git a/.circleci/config.yml b/.circleci/config.yml index b4d1777..1692cb6 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -1,47 +1,83 @@ version: 2.1 -orbs: - slack: circleci/slack@4.12.5 - aws-ecr: circleci/aws-ecr@8.2.1 -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 - \ No newline at end of file + - DOCKER +