Skip to content

Commit 8d7d221

Browse files
authored
Merge pull request #18 from mcpgod/yrnfg0-codex/add-github-action-for-automatic-npm-publishing
Publish to npm on merge
2 parents af0a555 + f3a3196 commit 8d7d221

6 files changed

Lines changed: 96 additions & 12 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: actionlint
2+
3+
on:
4+
# run only when workflow files change
5+
pull_request:
6+
paths: ['.github/workflows/**']
7+
push:
8+
branches: [main]
9+
paths: ['.github/workflows/**']
10+
11+
jobs:
12+
lint:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- uses: actions/checkout@v4
16+
17+
# installs the actionlint binary and scans .github/workflows
18+
- name: Lint GitHub Actions
19+
uses: eifinger/actionlint-action@v1
20+
# with:
21+
# version: "v1.7.7" # ⇐ uncomment to pin an exact linter version

.github/workflows/onPushToMain.yml

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ name: version, tag and github release
44
on:
55
push:
66
branches: [main]
7+
workflow_dispatch:
78

89
jobs:
910
release:
@@ -13,21 +14,27 @@ jobs:
1314
steps:
1415
- uses: actions/checkout@v4
1516
- uses: actions/setup-node@v4
17+
with:
18+
node-version: latest
1619
- name: Check if version already exists
1720
id: version-check
21+
shell: bash
1822
run: |
19-
package_version=$(node -p "require('./package.json').version")
20-
exists=$(gh api repos/${{ github.repository }}/releases/tags/v$package_version >/dev/null 2>&1 && echo "true" || echo "")
23+
package_name="$(node -p "require('./package.json').name")"
24+
package_version="$(node -p "require('./package.json').version")"
25+
26+
gh_exists="$(gh api \
27+
"repos/${{ github.repository }}/releases/tags/v${package_version}" \
28+
>/dev/null 2>&1 && echo 'true' || echo '')"
29+
30+
npm_exists="$(npm view "${package_name}@${package_version}" --json \
31+
>/dev/null 2>&1 && echo 'true' || echo '')"
2132
22-
if [ -n "$exists" ];
23-
then
24-
echo "Version v$package_version already exists"
25-
echo "::warning file=package.json,line=1::Version v$package_version already exists - no release will be created. If you want to create a new release, please update the version in package.json and push again."
26-
echo "skipped=true" >> $GITHUB_OUTPUT
33+
if [[ -n "$gh_exists" || -n "$npm_exists" ]]; then
34+
echo "::warning file=package.json,line=1::Version v${package_version} already exists—skipping release."
35+
echo "skipped=true" >>"${GITHUB_OUTPUT}"
2736
else
28-
echo "Version v$package_version does not exist. Creating release..."
29-
echo "skipped=false" >> $GITHUB_OUTPUT
30-
echo "tag=v$package_version" >> $GITHUB_OUTPUT
37+
echo "skipped=false" >>"${GITHUB_OUTPUT}"
3138
fi
3239
env:
3340
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
@@ -56,3 +63,20 @@ jobs:
5663
commit: ${{ github.ref_name }}
5764
token: ${{ secrets.GITHUB_TOKEN }}
5865
skipIfReleaseExists: true
66+
- name: Install dependencies
67+
if: ${{ steps.version-check.outputs.skipped == 'false' }}
68+
run: npm install
69+
- name: Build
70+
if: ${{ steps.version-check.outputs.skipped == 'false' }}
71+
run: npm run build
72+
- name: Prepare package
73+
if: ${{ steps.version-check.outputs.skipped == 'false' }}
74+
run: npm run prepack
75+
- name: Publish to npm
76+
uses: JS-DevTools/npm-publish@19c28f1ef146469e409470805ea4279d47c3d35c
77+
if: ${{ steps.version-check.outputs.skipped == 'false' }}
78+
with:
79+
token: ${{ secrets.NPM_TOKEN }}
80+
- name: Cleanup package
81+
if: ${{ steps.version-check.outputs.skipped == 'false' }}
82+
run: npm run postpack

.github/workflows/onRelease.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
name: publish
22

33
on:
4+
workflow_dispatch:
5+
46
release:
57
types: [published]
68
push:

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,25 @@ Run the CLI in development mode:
145145
./bin/dev
146146
```
147147

148+
## Publishing
149+
150+
Automatic publishing is handled by the `version, tag and github release` workflow. A new npm version is published whenever a version bump is merged into the `main` branch. The workflow can also be triggered manually from the **Actions** tab.
151+
152+
To enable publishing, set the following repository secrets:
153+
154+
- `NPM_TOKEN` – authentication token for npm.
155+
- `GH_EMAIL` and `GH_USERNAME` – used by the README update step.
156+
157+
Once the secrets are configured, pushing a new version or running the workflow manually will build the project, create a GitHub release and publish the package to npm.
158+
159+
### Linting workflows
160+
161+
Before committing changes to workflows, you can lint them locally:
162+
163+
```sh
164+
npm run lint:workflows
165+
```
166+
148167
## Contributing
149168

150169
Contributions are always welcome! To contribute:

package-lock.json

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
"oclif": "^4",
3131
"shx": "^0.3.3",
3232
"ts-node": "^10",
33-
"typescript": "^5"
33+
"typescript": "^5",
34+
"node-actionlint": "^1.2.2"
3435
},
3536
"engines": {
3637
"node": ">=18.0.0"
@@ -62,7 +63,8 @@
6263
"prepack": "oclif manifest && oclif readme",
6364
"test": "mocha --forbid-only \"test/**/*.test.ts\"",
6465
"coverage": "c8 --reporter=lcov --reporter=text npm test",
65-
"version": "oclif readme && git add README.md"
66+
"version": "oclif readme && git add README.md",
67+
"lint:workflows": "node-actionlint '.github/workflows/**/*.yml'"
6668
},
6769
"types": "dist/index.d.ts"
6870
}

0 commit comments

Comments
 (0)