build:
  box: node:8
  steps:
    - script:
      name: install dependencies
      code: |
        apt-get update
        apt-get install build-essential -y
        npm install -g yarn
    - script:
      name: yarn install
      code: |
        yarn config set cache-folder "$WERCKER_CACHE_DIR/yarn"
        yarn install
    - script:
      name: build
      code: yarn run build
    - npm-test
    - script:
      name: purge dev files
      code: |
        yarn install --production --ignore-scripts --prefer-offline

deploy-docker-master:
  box: node:8-alpine
  steps:
    - script:
      name: install dependencies
      code: |
        apk update
        apk add bash curl git openssh
    - script:
      name: copy app files
      code: |
        mkdir -p /var/wiki
        cp -LR assets node_modules server config.sample.yml package.json LICENSE /var/wiki
        rm -rf /pipeline
    - internal/docker-push:
        username: $DOCKER_HUB_USERNAME
        password: $DOCKER_HUB_PASSWORD
        tag: latest, master
        ports: "3000"
        working-dir: /var/wiki
        entrypoint: node server
        env: "WIKI_JS_HEROKU=1"
        repository: requarks/wiki
        registry: https://registry.hub.docker.com

deploy-docker-dev:
  box: node:8-alpine
  steps:
    - script:
      name: install dependencies
      code: |
        apk update
        apk add bash curl git openssh
    - script:
      name: copy app files
      code: |
        mkdir -p /var/wiki
        cp -LR assets node_modules server config.sample.yml package.json LICENSE /var/wiki
        rm -rf /pipeline
    - internal/docker-push:
        username: $DOCKER_HUB_USERNAME
        password: $DOCKER_HUB_PASSWORD
        tag: dev
        ports: "3000"
        working-dir: /var/wiki
        entrypoint: node server
        repository: requarks/wiki
        registry: https://registry.hub.docker.com

deploy-github:
  box: node:8
  steps:
    - script:
      name: package
      code: |
        tar -chzf wiki-js.tar.gz assets server config.sample.yml package.json wiki.js LICENSE
        tar -chzf node_modules.tar.gz node_modules
        SEMVER_NEXT=`curl --request POST --url https://beta.requarks.io/api/version/increment --header "authorization: $WIKIJSORG_TOKEN" --header 'cache-control: no-cache' --header 'content-type: application/json' --data '{"channel": "stable"}'`
    - github-create-release:
      token: $GITHUB_TOKEN
      tag: "v${SEMVER_NEXT}"
      prerelease: true
      title: "$SEMVER_NEXT Release"
    - github-upload-asset:
      token: $GITHUB_TOKEN
      file: wiki-js.tar.gz
    - github-upload-asset:
      token: $GITHUB_TOKEN
      file: node_modules.tar.gz
