stages:
  - build
  - push

build:
  image: harbor.exem-oss.org/cicd-tools/docker:cli-curl
  stage: build
  services:
    - docker:dind
  variables:
    IMAGE_NAME: $REGISTRT/$APP:$VER
  script:
    - docker build --pull -t "$IMAGE_NAME" .
  after_script:
    - |
      if [ "$CI_JOB_STATUS" = "success" ]; then
        curl -s -o /dev/null -X POST -H 'Content-Type: application/json' -d "{
          \"username\": \"CI/CD 알림\",
          \"icon_url\": \"https://www.claranet.com/br/sites/all/assets/br/cicd-icon.svg\",
          \"attachments\": [{
            \"color\": \"#0000FF\",
            \"author_name\": \"$APP 빌드 결과\",
            \"author_icon\": \"https://www.claranet.com/br/sites/all/assets/br/cicd-icon.svg\",
            \"text\": \"$APP의 빌드가 성공적으로 마무리 되었습니다.\n빌드 버전 : $VER\n파이프라인 : https://lab.jhcloud.kr/sa_8001/$APP/-/pipelines\"
          }]
          }" https://chat.exem.io/hooks/d8eb7x771pdaiym8xfgju8mx8a
      else
        curl -s -o /dev/null -X POST -H 'Content-Type: application/json' -d "{
          \"username\": \"CI/CD 알림\",
          \"icon_url\": \"https://www.claranet.com/br/sites/all/assets/br/cicd-icon.svg\",
          \"attachments\": [{
            \"color\": \"#FF0000\",
            \"author_name\": \"$APP 빌드 결과\",
            \"author_icon\": \"https://www.claranet.com/br/sites/all/assets/br/cicd-icon.svg\",
            \"text\": \"$APP의 빌드에 실패하였습니다!\n빌드 버전 : $VER\n파이프라인 : https://lab.jhcloud.kr/sa_8001/$APP/-/pipelines\"
          }]
          }" https://chat.exem.io/hooks/d8eb7x771pdaiym8xfgju8mx8a
      fi

  rules:
    - if: '$TRIGGER_NAME == "exemone_java_agent"'

push:
  image: harbor.exem-oss.org/cicd-tools/docker:cli-curl
  stage: push
  services:
    - docker:dind
  variables:
    IMAGE_NAME: $REGISTRT/$APP:$VER
  before_script:
    - docker login -u "$REGSITRY_USER" -p "$REGSITRY_PASSWORD" $REGISTRT
  script:
    - docker push "$IMAGE_NAME"
  after_script:
    - |
      if [ "$CI_JOB_STATUS" = "success" ]; then
        curl -s -o /dev/null -X POST -H 'Content-Type: application/json' -d "{
          \"username\": \"CI/CD 알림\",
          \"icon_url\": \"https://www.claranet.com/br/sites/all/assets/br/cicd-icon.svg\",
          \"attachments\": [{
            \"color\": \"#0000FF\",
            \"author_name\": \"$APP 푸시 결과\",
            \"author_icon\": \"https://www.claranet.com/br/sites/all/assets/br/cicd-icon.svg\",
            \"text\": \"$APP의 푸시가 성공적으로 마무리 되었습니다.\n이미지 주소 : $IMAGE_NAME\"
          }]
          }" https://chat.exem.io/hooks/d8eb7x771pdaiym8xfgju8mx8a
      else
        curl -s -o /dev/null -X POST -H 'Content-Type: application/json' -d "{
          \"username\": \"CI/CD 알림\",
          \"icon_url\": \"https://www.claranet.com/br/sites/all/assets/br/cicd-icon.svg\",
          \"attachments\": [{
            \"color\": \"#FF0000\",
            \"author_name\": \"$APP 푸시 결과\",
            \"author_icon\": \"https://www.claranet.com/br/sites/all/assets/br/cicd-icon.svg\",
            \"text\": \"$APP의 푸시에 실패하였습니다!\n파이프라인 : https://lab.jhcloud.kr/sa_8001/$APP/-/pipelines\"
          }]
          }" https://chat.exem.io/hooks/d8eb7x771pdaiym8xfgju8mx8a
      fi
  rules:
    - if: '$TRIGGER_NAME == "exemone_java_agent"'