# This is a basic workflow to help you get started with Actions

name: CI

# Controls when the action will run.
on:
  # Triggers the workflow on push or pull request events but only for the master branch
  pull_request:
    branches: [master]
  push:
    branches: [master]
    
  workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  test:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest
    # services:
    #   # Label used to access the service container
    #   redis:
    #     # Docker Hub image
    #     image: redis
    #     # Set health checks to wait until redis has started
    #     options: >-
    #       --health-cmd "redis-cli ping"
    #       --health-interval 10s
    #       --health-timeout 5s
    #       --health-retries 5
    #     ports:
    #       # Opens tcp port 6379 on the host and service container
    #       - 6379:6379
    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v1
        with:
          node-version: 14.x
      - run: npm ci
      - name: setup
        run: |
          docker run -d --name etcd -p 2380:2380 -p 4001:4001 quay.io/coreos/etcd:v3.5.9 /usr/local/bin/etcd \
          --data-dir=data.etcd --name "my-etcd" --cors='*' --initial-advertise-peer-urls http://0.0.0.0:2380 \
          --listen-peer-urls http://0.0.0.0:2380 --advertise-client-urls http://0.0.0.0:4001 \
          --listen-client-urls http://0.0.0.0:4001 --initial-cluster-state new
      - name: lint and test
        run: |
          npm run lint
          npm run test:cov
      - name: Coveralls
        uses: coverallsapp/github-action@v1.1.2
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
  publish:
    if: github.ref == 'refs/heads/master'
    needs: test
    runs-on: ubuntu-latest
    #Needed for OIDC - Npm repo publishing
    permissions:
      id-token: write
      contents: write
    steps:
      - uses: actions/checkout@v2
      - name: setup git
        run: |
          git config --local user.email "action@github.com"
          git config --local user.name "GitHub Action"
      - name: version
        run: |
          git checkout -f -b version-branch
          npm version patch --no-commit-hooks -m "$(git log -1 --pretty=%B) .... bump version [skip ci]"
          git push origin version-branch:master --follow-tags
      - uses: actions/setup-node@v4 # Use v4 for OIDC support
        with:
            node-version: 24 # Use a modern Node version
            registry-url: https://registry.npmjs.org
            scope: '@hkube'
      - name: Enable provenance
        run: npm config set provenance true
      - name: Publish to npm
        run: NODE_AUTH_TOKEN="" npm publish --access public --loglevel verbose
