diff --git a/.github/workflows/build-publish.yml b/.github/workflows/build-publish.yml new file mode 100644 index 00000000..577fc375 --- /dev/null +++ b/.github/workflows/build-publish.yml @@ -0,0 +1,67 @@ + +name: Node.js CI/CD + +on: + push: + branches: + - master # Build on every push to main + - feat/add-cicd # Build on every push to feat/add-cicd + tags: + - v* # Publish to npm when pushing a tag that starts with "v" + +jobs: + # ------------------------------------- + # 1. Build job (for pushes to main) + # ------------------------------------- + build: + # Only run this job when *not* publishing a tag + if: startsWith(github.ref, 'refs/tags/') != true + runs-on: ubuntu-latest + + steps: + - name: Checkout source code + uses: actions/checkout@v3 + + - name: Setup Node + uses: actions/setup-node@v3 + with: + node-version: '20' + + - name: Install dependencies + run: npm ci + + - name: Build + run: npm run build + + # ------------------------------------- + # 2. Release job (for pushes with tags) + # ------------------------------------- + release: + # Only run when the ref is a tag + if: startsWith(github.ref, 'refs/tags/') + runs-on: ubuntu-latest + + steps: + - name: Checkout source code + uses: actions/checkout@v3 + + - name: Setup Node + uses: actions/setup-node@v3 + with: + node-version: '20' + # Configure default registry to npmjs.org + registry-url: 'https://registry.npmjs.org' + + - name: Install dependencies + run: npm ci + + - name: Build + run: npm run build + + - name: Run tests + run: npm test + + - name: Publish to npm + run: npm publish + env: + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} \ No newline at end of file