# this pipeline should not be run on version tag releases (it may cause a non correct sha to override version tagged image in dockerhub)
version: "1.0"

stages:
- CI
- CD

steps:
  main_clone:
    title: Cloning main repository...
    type: git-clone
    repo: '${{CF_REPO_OWNER}}/${{CF_REPO_NAME}}'
    revision: '${{CF_REVISION}}'

  install_dependencies:
    title: 'Installing testing dependencies'
    stage: CI
    image: codefresh/build-cli
    commands:
        - yarn install --frozen-lockfile

  eslint:
    title: 'Running linting logic'
    stage: CI
    image: codefresh/build-cli
    commands:
        - yarn eslint

  unit-tests:
    stage: CI
    title: 'Running unit tests'
    image: codefresh/build-cli
    commands:
        - yarn test

  add_git_tag:
    title: "Add Git tag"
    stage: CD
    image: codefresh/cli
    commands:
    - 'apk update && apk add jq'
    - 'export PACKAGE_VERSION=$(jq -r ".version" package.json)'
    - "echo Current version: $PACKAGE_VERSION"
    - "cf_export PACKAGE_VERSION"
    - "git tag $PACKAGE_VERSION"
    when:
      branch:
        only: [ master ]

  create_release:
    stage: CD
    title: "Create github release"
    image: codefresh/build-cli
    commands:
    - 'curl --fail -X POST -d ''{"tag_name":"v${{PACKAGE_VERSION}}","target_commitish":"${{CF_REVISION}}","name":"Codefresh V${{PACKAGE_VERSION}}"}'' -H "Content-Type: application/json" -H "Authorization: token ${{GITHUB_TOKEN}}" https://api.github.com/repos/codefresh-io/js-sdk/releases'
    when:
      branch:
        only: [ master ]

  deploy_to_npm:
    stage: CD
    type: npm-publish
    arguments:
      NPM_TOKEN: '${{NPM_TOKEN}}'
      DIR: '${{CF_REPO_NAME}}'
    when:
      branch:
        only: [ master ]
