default:
  image: repo.ayeneh.co/cicd/ayeneh-ci-docker:1.0.2
  services:
    - name: repo.ayeneh.co/dockerhub/docker:dind
      alias: thedockerhost
variables:
  # Tell docker CLI how to talk to Docker daemon; see
  # https://docs.gitlab.com/ee/ci/docker/using_docker_build.html#use-docker-in-docker-executor
  DOCKER_HOST: tcp://thedockerhost:2375/
  # Use the overlayfs driver for improved performance:
  DOCKER_DRIVER: overlay2
  DOCKER_TLS_CERTDIR: ""
  CD_MANIFEST_FILE: panjareh/values.yaml
  TAG: $CI_COMMIT_REF_NAME-$CI_COMMIT_SHORT_SHA
  CD_REPO_NAME: tw-panjareh-deployment
  CD_GIT_URL: https://$CDUSER:$CDUSER_TOKEN@git.ayeneh.co/sre/deployment/$CD_REPO_NAME.git

before_script:
  # Configure Artifactory instance with JFrog CLI
  - /jfrog config add ayeneh --artifactory-url $ARTIFACTORY_URL --user $ARTIFACTORY_USER --apikey $ARTIFACTORY_PASS --interactive=false
  # Login to Artifactory docker registry
  - docker login -u $ARTIFACTORY_USER -p $ARTIFACTORY_PASS $ARTIFACTORY_DOCKER_REPOSITORY
stages:
  - build
  - test
  - deploy

build-master:
  stage: build
  script:
    - docker pull repo.ayeneh.co/ayeneh/$CI_PROJECT_NAME:$CI_COMMIT_REF_NAME-latest || true
    - docker build --cache-from repo.ayeneh.co/ayeneh/$CI_PROJECT_NAME:$CI_COMMIT_REF_NAME-latest -t repo.ayeneh.co/ayeneh/$CI_PROJECT_NAME:$TAG -t repo.ayeneh.co/ayeneh/$CI_PROJECT_NAME:$CI_COMMIT_REF_NAME-latest .
    - /jfrog rt dp repo.ayeneh.co/ayeneh/$CI_PROJECT_NAME:$TAG https://ARTIFACTORY_DOCKER_REPOSITORY/ayeneh
    - /jfrog rt dp repo.ayeneh.co/ayeneh/$CI_PROJECT_NAME:$CI_COMMIT_REF_NAME-latest https://ARTIFACTORY_DOCKER_REPOSITORY/ayeneh
  only:
    - master
test-master:
  stage: test
  script:
    - echo 'this job is intended for test running'
  only:
    - master
deploy-master:
  stage: deploy
  script:
    - git config --global user.name sre
    - git config --global user.email sre@git.ayeneh.co
    - git config --global http.sslverify "false"
    - git clone --single-branch --branch master $CD_GIT_URL
    - cd $CD_REPO_NAME
    - yq e ".image.tag = \"$TAG\"" -i $CD_MANIFEST_FILE
    - cat $CD_MANIFEST_FILE
    - git commit -am "update image tag" && git push origin master
  only:
    - master
build-staging:
  stage: build
  script:
    - docker pull repo.ayeneh.co/ayeneh/$CI_PROJECT_NAME:$CI_COMMIT_REF_NAME-latest || true
    - docker build --cache-from repo.ayeneh.co/ayeneh/$CI_PROJECT_NAME:$CI_COMMIT_REF_NAME-latest -t repo.ayeneh.co/ayeneh/$CI_PROJECT_NAME:$TAG -t repo.ayeneh.co/ayeneh/$CI_PROJECT_NAME:$CI_COMMIT_REF_NAME-latest .
    - /jfrog rt dp repo.ayeneh.co/ayeneh/$CI_PROJECT_NAME:$TAG https://ARTIFACTORY_DOCKER_REPOSITORY/ayeneh
    - /jfrog rt dp repo.ayeneh.co/ayeneh/$CI_PROJECT_NAME:$CI_COMMIT_REF_NAME-latest https://ARTIFACTORY_DOCKER_REPOSITORY/ayeneh
  only:
    - staging
test-staging:
  stage: test
  script:
    - echo 'this job is intended for test running'
  only:
    - staging
deploy-staging:
  stage: deploy
  script:
    - git config --global user.name sre
    - git config --global user.email sre@git.ayeneh.co
    - git config --global http.sslverify "false"
    - git clone --single-branch --branch staging $CD_GIT_URL
    - cd $CD_REPO_NAME
    - yq e ".image.tag = \"$TAG\"" -i $CD_MANIFEST_FILE
    - cat $CD_MANIFEST_FILE
    - git commit -am "update image tag" && git push origin staging
  only:
    - staging
build-dev:
  stage: build
  script:
    # - docker pull repo.ayeneh.co/ayeneh/$CI_PROJECT_NAME:$CI_COMMIT_REF_NAME-latest
    # Build docker image
    - docker pull repo.ayeneh.co/ayeneh/$CI_PROJECT_NAME:$CI_COMMIT_REF_NAME-latest || true
    - docker build --cache-from repo.ayeneh.co/ayeneh/$CI_PROJECT_NAME:$CI_COMMIT_REF_NAME-latest -t repo.ayeneh.co/ayeneh/$CI_PROJECT_NAME:$TAG -t repo.ayeneh.co/ayeneh/$CI_PROJECT_NAME:$CI_COMMIT_REF_NAME-latest .
    # Push docker image to Artifactory
    - /jfrog rt dp repo.ayeneh.co/ayeneh/$CI_PROJECT_NAME:$TAG https://ARTIFACTORY_DOCKER_REPOSITORY/ayeneh
    - /jfrog rt dp repo.ayeneh.co/ayeneh/$CI_PROJECT_NAME:$CI_COMMIT_REF_NAME-latest https://ARTIFACTORY_DOCKER_REPOSITORY/ayeneh
  only:
    - dev
test-dev:
  stage: test
  script:
    - echo 'this job is intended for test running'
  only:
    - dev
deploy-dev:
  stage: deploy
  script:
    - git config --global user.name sre
    - git config --global user.email sre@git.ayeneh.co
    - git config --global http.sslverify "false"
    - git clone --single-branch --branch dev $CD_GIT_URL
    - cd $CD_REPO_NAME
    - yq e ".image.tag = \"$TAG\"" -i $CD_MANIFEST_FILE
    - cat $CD_MANIFEST_FILE
    - git commit -am "update image tag" && git push origin dev
  only:
    - dev
