---
kind: pipeline
type: docker
name: default

steps:
  - name: Setup & Clone
    commands:
      - cd /var/www/html/
      - rm -rf solomonlai.ng
      - git clone https://git.inkletblot.com/inkletblot/solomonlai.ng.git
      - cd solomonlai.ng/
      - git checkout main

steps:
  - name: build
    image: alpine
    volumes: 
    - name: cache
      path: /hugo
    commands:
    - echo "Ititialising"
    - mkdir -p $BIN_DIR
    - echo "Dependencies"
    - apk add --no-cache curl tar
    - echo "Installing hugo"
    - curl -LJO https://github.com/gohugoio/hugo/releases/download/v$${HUGO_VERSION}/hugo_extended_$${HUGO_VERSION}_Linux-64bit.tar.gz;
    - mkdir -p hugo_extended
    - tar -xvf hugo_extended_$${HUGO_VERSION}_Linux-64bit.tar.gz -C hugo_extended;
    - mv hugo_extended/hugo $BIN_DIR
    - rm -rf hugo_extended*;
    - ls /bin
    - hugo version
    - echo "Building configuration"
    - hugo
    - ls
    environment:
      BIN_DIR: /bin
      HUGO_VERSION:
        from_secret: hugo_version


  - name: deploy
    image: alpine
    volumes: 
    - name: cache
      path: /hugo
    commands:
    - 'command -v ssh-agent >/dev/null || ( apt-get update -y && apt-get install openssh-client -y)'
    - eval $(ssh-agent -s)
    - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
    - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" >> ~/.ssh/config'
    - tar zcf ../inkletblot.com.tar.gz ./public
    - scp -o StrictHostKeyChecking=no ../inkletblot.com.tar.gz droneci@webhost.forest:/var/www/html
    - ssh droneci@webhost.forest "rm -Rf /var/www/html/inkletblot.com_old && mv /var/www/html/inkletblot.com /var/www/html/inkletblot.com_old && mkdir /var/www/html/inkletblot.com && mkdir /var/www/html/inkletblot.com_build && tar zxf /var/www/html/inkletblot.com.tar.gz -C /var/www/html/inkletblot.com_build && mv /var/www/html/inkletblot.com_build/public/* /var/www/html/inkletblot.com/ && rm -Rf /var/www/html/inkletblot.com_build && chmod -R 755 /var/www/html/inkletblot.com && exit"
    environment:
      SSH_PRIVATE_KEY:
        from_secret: ssh_key
