# GitLab CI for RH

stages:
  - deploy

deploy-job:
  stage: deploy
  rules:
    - if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "main"'
      when: always
  environment: production
  before_script:
    ## Install ssh-agent if not already installed, it is required by Docker.
    - 'command -v ssh-agent >/dev/null || ( apt-get update -y && apt-get install openssh-client -y )'

    ## Run ssh-agent (inside the build environment)
    - eval $(ssh-agent -s)

    ## Give the right permissions, otherwise ssh-add will refuse to add files
    ## Add the SSH key stored in SSH_PRIVATE_KEY file type CI/CD variable to the agent store
    - chmod 400 "$SSH_PRIVATE_KEY"
    - ssh-add "$SSH_PRIVATE_KEY"

    ## Create the SSH directory and give it the right permissions
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh

    ## Put the scm to known hosts
    - ssh-keyscan -t rsa gitea.ctisante.com >> ~/.ssh/known_hosts

    ## Set the git user name and email
    - git config --global user.email "bender@ctisante.com"
    - git config --global user.name "bender"
    - git remote add gitea git@gitea.ctisante.com:cti/rh-gitlab.git
  script:
    - echo "Pushing to Public Repo..."
    - git update-ref -d refs/heads/deploy #delete branch deploy
    - git branch deploy
    - git push -f gitea deploy:master
    - echo "RH module pushed."