image: node:alpine

stages: 
    - build
    - test
    - deploy

before_script:
  - yarn

cache:
  paths:
    - node_modules/

build:
    stage: build
    script:
        - apk add git
        - git tag | grep -c "^${PACKAGE_VERSION}$" && echo 'You forgot to bump the version number!' && exit 1   
        - yarn build
    artifacts:
        paths:
            - dist/
        untracked: false
        expire_in: 30 days
    only:
        - branches
        - web
        - pushes  

test:
    stage: test
    needs:
        - build
    script:
        - yarn test

publish:
    stage: deploy
    needs:
        - test
    script:
        - apk add jq git httpie
        - export PACKAGE_VERSION=`cat package.json | jq -cr .version`
        - echo '//registry.npmjs.org/:_authToken=${NPM_ACCESS_TOKEN}'>.npmrc
        - "export MSG=`git log --no-merges --pretty=\"- %aN (%h): %s%n%b\"   HEAD...HEAD~1`"
        - echo "# EasyDITA Client release notes:" > notes.txt
        - echo $MSG >> notes.txt
        - cat notes.txt
        - http -j -v --check-status --ignore-stdin POST ${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/releases name=${PACKAGE_VERSION} tag_name=${PACKAGE_VERSION} description=@notes.txt ref=${CI_COMMIT_SHA} private_token=${GITLAB_ACCESS_TOKEN}
        - npm publish --access public
    only:
        - master