stages:
  - validate
  - build
  - deploy
  - benchmark
  - post_deploy
  - scripts

.ssh_default: &ssh_default
  - 'which rsync || ( apk update && apk add rsync )'
  - 'which ssh-agent || ( apk update && apk add openssh-client)'
  - eval $(ssh-agent -s)
  - mkdir -p ~/.ssh
  - chmod 700 ~/.ssh
  - echo "$SSH_PRIVATE_KEY" | tr -d '\r' >> ~/.ssh/key_firq
  - chmod 600 ~/.ssh/key_firq
  - echo "Host $DEPLOY_HOST" >> ~/.ssh/config
  - echo $'\n\tIdentityFile ~/.ssh/key_firq' >> ~/.ssh/config
  - echo $'\n\tStrictHostKeyChecking no\n\tIdentitiesOnly yes\n' >> ~/.ssh/config
  - chmod 644 ~/.ssh/config
  - echo "$SSH_KNOWN_HOSTS" > ~/.ssh/known_hosts
  - chmod 644 ~/.ssh/known_hosts
  - echo "SSH setup completed"

checking:
  image: node:lts
  stage: validate
  before_script:
    - npm install
  script:
    - echo "Running Astro checks"
    - npm run astro check

build-site:
  image: node:lts
  stage: build
  when: on_success
  cache:
    paths:
      - node_modules/
  script:
    - npm install
    - npm run build
    - rm -r public/assets/data/
    - cp serve.json public
  artifacts:
    paths:
      - public
    expire_in: 1 day

deploy-site:
  stage: deploy
  when: on_success
  only:
    - tags
  except:
    - branches
  before_script:
    - *ssh_default
  script:
    - echo "Getting artifacts"
    - ls public
    - echo "Stopping screen session, cleaning"
    - ssh $DEPLOY_USER@$DEPLOY_HOST "screen -X -S firq_dev-public kill; rm -r -f firq_dev/public/*;"
    - echo "Copying to proxmox machine"
    - rsync -az --stats public/* $DEPLOY_USER@$DEPLOY_HOST:~/firq_dev/public
    - echo "Restarting screen session"
    - ssh $DEPLOY_USER@$DEPLOY_HOST 'PATH="$HOME/.local/bin:$PATH"; cd firq_dev; find maintenance public -maxdepth 1 -printf "%p\n"; screen -S firq_dev-public -dm serve public/ -p 9000'
    - echo "Website is up on https://firq.dev/"

deploy-testing:
  stage: deploy
  when: on_success
  except:
    - tags
    - main
  before_script:
    - *ssh_default
  script:
    - echo "Getting artifacts"
    - ls public
    - echo "Stopping screen session, cleaning"
    - ssh $DEPLOY_USER@$DEPLOY_HOST "screen -X -S firq_dev-staging kill; rm -r -f firq_dev/staging/*;"
    - echo "Copying to proxmox machine"
    - rsync -az --stats public/* $DEPLOY_USER@$DEPLOY_HOST:~/firq_dev/staging
    - echo "Restarting screen session"
    - ssh $DEPLOY_USER@$DEPLOY_HOST 'PATH="$HOME/.local/bin:$PATH"; cd firq_dev; find staging -maxdepth 1 -printf "%p\n"; screen -S firq_dev-staging -dm serve staging/ -p 9100'
    - echo "Staging environment is up!"

unlighthouse:
  image: registry.gitlab.com/gitlab-ci-utils/lighthouse:latest
  stage: benchmark
  when: manual
  except:
    - main
    - tags
  before_script:
    - npm install @unlighthouse/cli puppeteer
    - mkdir unlighthouse-reports
  script:
    - node_modules/.bin/unlighthouse-ci
    - cp public/serve.json unlighthouse-reports
  artifacts:
    paths:
      - unlighthouse-reports
    expire_in: 1 day

unlighthouse-publish:
  stage: benchmark
  when: on_success
  needs:
    - unlighthouse
  except:
    - tags
    - main
  before_script:
    - *ssh_default
  script:
    - echo "Getting artifacts"
    - ls unlighthouse-reports
    - echo "Stopping screen session, cleaning"
    - ssh $DEPLOY_USER@$DEPLOY_HOST "screen -X -S firq_dev-unlighthouse kill; rm -r -f firq_dev/unlighthouse/*;"
    - echo "Copying to proxmox machine"
    - rsync -az --stats unlighthouse-reports/* $DEPLOY_USER@$DEPLOY_HOST:~/firq_dev/unlighthouse
    - echo "Restarting screen session"
    - ssh $DEPLOY_USER@$DEPLOY_HOST 'PATH="$HOME/.local/bin:$PATH"; cd firq_dev; find unlighthouse -maxdepth 1 -printf "%p\n"; screen -S firq_dev-unlighthouse -dm serve unlighthouse/ -p 9101'
    - echo "Unlighthouse results are up!"

success_notification:
  stage: post_deploy
  before_script:
    - apk update
    - apk add rsync
    - apk add curl
    - apk add git
    - apk add bash
  script:
    - wget https://raw.githubusercontent.com/DiscordHooks/gitlab-ci-discord-webhook/master/send.sh
    - chmod +x send.sh
    - ./send.sh success $WEBHOOK_URL
  when: on_success

failure_notification:
  stage: post_deploy
  before_script:
    - apk update
    - apk add curl
    - apk add git
    - apk add bash
  script:
    - wget https://raw.githubusercontent.com/DiscordHooks/gitlab-ci-discord-webhook/master/send.sh
    - chmod +x send.sh
    - ./send.sh failure $WEBHOOK_URL
  when: on_failure

maintenance-mode:
  stage: post_deploy
  when: on_failure
  before_script:
    - *ssh_default
  only:
    - tags
    - main
  script:
    - echo "Stopping screen session"
    - ssh $DEPLOY_USER@$DEPLOY_HOST "screen -X -S firq_dev-public kill;" || true
    - echo "Enabeling maintenance mode"
    - ssh $DEPLOY_USER@$DEPLOY_HOST 'PATH="$HOME/.local/bin:$PATH"; screen -S firq_dev-maintenance -dm serve firq_dev/maintenance/ -p 9000'
    - echo "Maintenance Website is up on https://firq.dev/"

redeploy-site:
  stage: scripts
  when: manual
  only:
    - tags
  except:
    - branches
  before_script:
    - *ssh_default
  script:
    - echo "Stopping screen session"
    - ssh $DEPLOY_USER@$DEPLOY_HOST "screen -X -S firq_dev-public kill" || true
    - echo "Restarting screen session"
    - ssh $DEPLOY_USER@$DEPLOY_HOST 'PATH="$HOME/.local/bin:$PATH"; screen -S firq_dev-public -dm serve firq_dev/public/ -p 9000'
    - echo "Website is up on https://firq.dev/"

redeploy-staging:
  stage: scripts
  when: manual
  except:
    - main
    - tags
  before_script:
    - *ssh_default
  script:
    - echo "Stopping screen session"
    - ssh $DEPLOY_USER@$DEPLOY_HOST "screen -X -S firq_dev-staging kill" || true
    - echo "Restarting screen session"
    - ssh $DEPLOY_USER@$DEPLOY_HOST 'PATH="$HOME/.local/bin:$PATH"; screen -S firq_dev-staging -dm serve firq_dev/staging/ -p 9100'
    - echo "Staging environment is up!"

redeploy-unlighthouse:
  stage: scripts
  when: manual
  except:
    - main
    - tags
  before_script:
    - *ssh_default
  script:
    - echo "Stopping screen session"
    - ssh $DEPLOY_USER@$DEPLOY_HOST "screen -X -S firq_dev-unlighthouse kill" || true
    - echo "Restarting screen session"
    - ssh $DEPLOY_USER@$DEPLOY_HOST 'PATH="$HOME/.local/bin:$PATH"; screen -S firq_dev-unlighthouse -dm serve firq_dev/unlighthouse/ -p 9101'
    - echo "Staging environment is up!"

maintenance-deploy:
  stage: scripts
  when: manual
  only:
    - main
    - tags
  before_script:
    - *ssh_default
  script:
    - echo "Stopping screen session"
    - ssh $DEPLOY_USER@$DEPLOY_HOST "screen -X -S firq_dev-public kill;" || true
    - echo "Enabeling maintenance mode"
    - ssh $DEPLOY_USER@$DEPLOY_HOST 'PATH="$HOME/.local/bin:$PATH"; screen -S firq_dev-maintenance -dm serve firq_dev/maintenance/ -p 9000'
    - echo "Maintenance Website is up on https://firq.dev/"