DEV Community

ashutosh049
ashutosh049

Posted on • Updated on

Gitlab CI/CD


variables:
      MAVEN_OPTS: "-Dhttps.protocols=TLSv1.2 -Dmaven.repo.local=$CI_PROJECT_DIR/.m2/repository -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=WARN -Dorg.slf4j.simpleLogger.showDateTime=true -Djava.awt.headless=true"
      MAVEN_CLI_OPTS: "--batch-mode --errors --fail-at-end --show-version -DinstallAtEnd=true -DdeployAtEnd=true"
      DOCKER_DRIVER: overlay2
      #IMAGE_PATH: $CI_REGISTRY/$CI_PROJECT_NAMESPACE/$CI_PROJECT_NAME/$CI_COMMIT_REF_NAME
      IMAGE_PATH_GITLAB: $CI_REGISTRY_IMAGE
      IMAGE_PATH_DKHUB: $DKHUB_REGISTRY/$DKHUB_REGISTRY_USER/$CI_PROJECT_NAME
      CI_BUILD_NAME: $CI_PROJECT_NAME

cache:
      key: mavenrepo
      paths:
      - ./.m2/repository

image: docker:19.03.1

services:
- docker:19.03.1-dind

stages:
      - build
      - deploy
      - push

mvn_build:
      image: maven:3.3.9-jdk-8
      stage: build
      only:
            - master
      script:
            - "mvn $MAVEN_CLI_OPTS clean install -Dmaven.test.skip=true -s ci_settings.xml"
      artifacts:
            paths:
            - target/$CI_BUILD_NAME*-exec.jar

mvn_deploy:
      image: maven:3.3.9-jdk-8
      stage: deploy
      only:
      - master
      before_script:
            - echo "Project-> [$CI_PROJECT_NAME]"
            - echo "IMAGE_PATH  = $IMAGE_PATH"
      script:
            - 'mvn $MAVEN_CLI_OPTS deploy -Dmaven.test.skip=true -s ci_settings.xml'
      when: on_success

.push_img_gitlab:
      stage: push
      only:
           - master
      before_script:
            - docker version
            - echo "IMAGE_PATH_GITLAB = $IMAGE_PATH_GITLAB"
            - docker login $IMAGE_PATH_GITLAB -u $GITLAB_REGISTRY_USER -p $GITLAB_REGISTRY_PAT
      script:
            - docker pull $IMAGE_PATH_GITLAB || true
            - docker build --tag $IMAGE_PATH_GITLAB:$CI_COMMIT_SHORT_SHA .
            - docker push $IMAGE_PATH_GITLAB:$CI_COMMIT_SHORT_SHA
      when: on_success

push_img_dockerhub:
      stage: push
      only:
            - master
      before_script:
            - docker version
            - echo "IMAGE_PATH_DKHUB = $IMAGE_PATH_DKHUB"
            - docker login -u $DKHUB_REGISTRY_USER -p $DKHUB_REGISTRY_PAT $DKHUB_REGISTRY
      script:
            - docker build --pull -t $IMAGE_PATH_DKHUB .
            - docker push $IMAGE_PATH_DKHUB
      when: on_success

Enter fullscreen mode Exit fullscreen mode

Top comments (0)