forked from Netcracker/qubership-apihub-npm-gitflow
-
Notifications
You must be signed in to change notification settings - Fork 0
103 lines (88 loc) · 3.1 KB
/
npm-ci.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
name: NPM CI/CD Workflow
on:
push:
branches:
- main
- release
- develop
- feature/*
delete:
branches:
- release
- feature/*
permissions:
contents: read
packages: write
jobs:
on-push:
if: github.event_name == 'push'
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
registry-url: 'https://npm.pkg.github.com/'
- name: Authenticate to GitHub NPM Registry
run: echo "//npm.pkg.github.com/:_authToken=${{ secrets.GITHUB_TOKEN }}" > ~/.npmrc
- name: Install dependencies
run: npm install
- name: Bump version and commit (prerelease only)
id: bump_version
if: startsWith(github.ref, 'refs/heads/develop') || startsWith(github.ref, 'refs/heads/release') || startsWith(github.ref, 'refs/heads/feature/')
run: |
CURRENT_BRANCH=$(echo $GITHUB_REF | sed 's/refs\/heads\///')
if [[ "$CURRENT_BRANCH" == "develop" || "$CURRENT_BRANCH" == "release" || "$CURRENT_BRANCH" == feature/* ]]; then
npm version prerelease -m "chore: bump prerelease version to %s [ci skip]"
echo "bump_version=true" >> $GITHUB_ENV
else
echo "bump_version=false" >> $GITHUB_ENV
fi
- name: Build the package
run: npm run build --if-present
- name: Run tests
run: npm test --if-present
- name: Publish to GitHub NPM Registry
run: |
CURRENT_BRANCH=$(echo $GITHUB_REF | sed 's/refs\/heads\///')
if [ "$CURRENT_BRANCH" == "main" ]; then
TAG_NAME="latest"
elif [ "$CURRENT_BRANCH" == "release" ]; then
TAG_NAME="next"
elif [ "$CURRENT_BRANCH" == "develop" ]; then
TAG_NAME="dev"
elif [[ "$CURRENT_BRANCH" == feature/* ]]; then
TAG_NAME="${CURRENT_BRANCH#feature/}"
fi
if [ -n "$TAG_NAME" ]; then
npm publish --tag "$TAG_NAME"
fi
- name: Push changes back
if: env.bump_version == 'true'
run: |
git config --global user.name "github-actions"
git config --global user.email "[email protected]"
git push origin HEAD
- name: Cleanup
if: always()
run: rm ~/.npmrc
on-delete:
if: github.event_name == 'delete'
runs-on: ubuntu-latest
steps:
- name: Authenticate to GitHub NPM Registry
run: echo "//npm.pkg.github.com/:_authToken=${{ secrets.GITHUB_TOKEN }}" > ~/.npmrc
- name: Delete associated tag
run: |
DELETED_BRANCH=$(echo $GITHUB_REF | sed 's/refs\/heads\///')
if [ "$DELETED_BRANCH" == "release" ] || [[ "$DELETED_BRANCH" == feature/* ]]; then
TAG_NAME=$(npm dist-tag ls | grep $DELETED_BRANCH | awk '{print $1}')
if [ -n "$TAG_NAME" ]; then
npm dist-tag rm "$TAG_NAME" "$DELETED_BRANCH"
fi
fi
- name: Cleanup
if: always()
run: rm ~/.npmrc