language: node_js

node_js:
  - 22

services:
  - docker

before_install:
  - export WS_APIKEY=${WS_APIKEY}
  - export WS_USERKEY=${WS_USERKEY}
  - export WS_PRODUCTNAME=${WS_PRODUCTNAME}
  - export WS_PROJECTNAME=RemoteResource
  - export WS_WSS_URL=https://ibmets.whitesourcesoftware.com/agent
  - ./build/download-kubelint.sh

script:
  # Audit npm packages. Fail build whan a PR audit fails, otherwise report the vulnerability and proceed.
  - if [ "${TRAVIS_PULL_REQUEST}" != "false" ]; then npx audit-ci --config audit-ci.json; else npx audit-ci --config audit-ci.json || true; fi
  - npm run lint
  - npm test
  - if [[ "${TRAVIS_TAG}" =~ ^[0-9]+\.[0-9]+\.[0-9]+(-rc\.[0-9]+)?$ ]]; then npm version --no-git-tag-version "${TRAVIS_TAG}"; fi
  - docker build --rm -t "quay.io/razee/remoteresource:${TRAVIS_COMMIT}" .
  - if [ -n "${TRAVIS_TAG}" ]; then docker tag quay.io/razee/remoteresource:${TRAVIS_COMMIT} quay.io/razee/remoteresource:${TRAVIS_TAG}; fi
  - docker images
  - ./build/process-template.sh kubernetes/RemoteResource/resource.yaml >/tmp/resource.yaml
  - kubelint /tmp/resource.yaml
  # Perform UA scan on non-PR builds
  - if [ "${TRAVIS_PULL_REQUEST}" = "false" ]; then curl -LJO https://unified-agent.s3.amazonaws.com/wss-unified-agent.jar; java -jar wss-unified-agent.jar -d . || echo "UA Scan Error occurred"; fi

before_deploy:
  - export GITHUB_TOKEN="${GITHUB_TOKEN_20250114}"
  - export NPMJS_API_KEY="${NPMJS_API_KEY_20250116}"
  - export QUAY_ID="${QUAY_ID_20250106}"
  - export QUAY_TOKEN="${QUAY_TOKEN_20250106}"
  - docker login -u="${QUAY_ID}" -p="${QUAY_TOKEN}" quay.io
  # Use npm v9 for deployments (v10 has problems with authentication with api_key)
  - npm install -g npm@9

deploy:
  # Publish npm package with tag "next" on release candidates
  - provider: npm
    email: "${NPMJS_EMAIL}"
    api_key: "${NPMJS_API_KEY}"
    tag: next
    skip_cleanup: true
    on:
      tags: true
      condition: ${TRAVIS_TAG} =~ ^[0-9]+\.[0-9]+\.[0-9]+(-rc\.[0-9]+)$
  # Publish docker image on release and release candidates
  - provider: script
    script: docker push "quay.io/razee/remoteresource:${TRAVIS_TAG}"
    skip_cleanup: true
    on:
      tags: true
      condition: ${TRAVIS_TAG} =~ ^[0-9]+\.[0-9]+\.[0-9]+(-rc\.[0-9]+)?$
  # Publish npm package as "latest" on release
  - provider: npm
    email: "${NPMJS_EMAIL}"
    api_key: "${NPMJS_API_KEY}"
    skip_cleanup: true
    on:
      tags: true
      condition: ${TRAVIS_TAG} =~ ^[0-9]+\.[0-9]+\.[0-9]+$
  # Publish GitHub release assets on release
  - provider: releases
    file: /tmp/resource.yaml
    skip_cleanup: true
    api_key: "${GITHUB_TOKEN}"
    name: "${TRAVIS_TAG}"
    on:
      tags: true
      condition: ${TRAVIS_TAG} =~ ^[0-9]+\.[0-9]+\.[0-9]+$
