Skip to content

Commit 8678c3d

Browse files
kikuomaxCopilot
andauthored
Publish a developer package via GitHub Actions workflow (#13)
* feat(gha): publish developer package Introduces a GitHub Actions workflow `publish-dev-package` which publishes a developer package to the GitHub npm registry when commits are pushed to the `main` branch. A developer package bears the target release version but followed by the short commit hash of the commit used to build the package. The actual steps are described in `publish-package.yml`. * docs: update README Topics: - Explains how to install from GitHub Packages - Explains how to configure a GitHub personal access token to install a developer package - `npm` → `pnpm` for development --------- Co-authored-by: Copilot <[email protected]>
1 parent 41ed250 commit 8678c3d

File tree

4 files changed

+165
-5
lines changed

4 files changed

+165
-5
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: "Publish a developer package to GitHub npm registry"
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
permissions:
9+
contents: read
10+
packages: write
11+
12+
jobs:
13+
publish-dev:
14+
uses: ./.github/workflows/publish-package.yml
15+
16+
with:
17+
npm-registry-url: "https://npm.pkg.github.com/"
18+
19+
secrets:
20+
npm-token: ${{ secrets.GITHUB_TOKEN }}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
name: "Publish a package to a specified npm registry"
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
npm-registry-url:
7+
description: "URL of the npm registry to publish to; e.g., https://npm.pkg.github.com for GitHub Packages"
8+
type: string
9+
required: true
10+
11+
secrets:
12+
npm-token:
13+
description: "Token that is allowed to publish to the npm registry; e.g., secrets.GITHUB_TOKEN for GitHub Packages"
14+
required: true
15+
16+
permissions:
17+
contents: read
18+
packages: write
19+
20+
env:
21+
node-version: 22
22+
pnpm-version: 10
23+
24+
jobs:
25+
build-and-publish:
26+
runs-on: ubuntu-latest
27+
28+
steps:
29+
- name: Checkout
30+
uses: actions/checkout@v4
31+
32+
- name: Get short commit hash
33+
id: commit-hash
34+
run: echo "short-commit-hash=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
35+
36+
# appends the short commit hash to the version number
37+
# 1. reads the package.json file
38+
# 2. replaces the version and saves it in the package.json
39+
- name: Read package information
40+
id: package-info
41+
# uses the exact commit to prevent harmful updates
42+
uses: jaywcjlove/github-action-package@f6a7afaf74f96a166243f05560d5af4bd4eaa570
43+
with:
44+
path: package.json
45+
- name: Append short commit hash to the version
46+
# uses the exact commit to prevent harmful updates
47+
uses: jaywcjlove/github-action-package@f6a7afaf74f96a166243f05560d5af4bd4eaa570
48+
with:
49+
path: package.json
50+
version: ${{ steps.package-info.outputs.version }}-${{ steps.commit-hash.outputs.short-commit-hash }}
51+
52+
- name: Install pnpm ${{ env.pnpm-version }}
53+
uses: pnpm/action-setup@v4
54+
with:
55+
version: ${{ env.pnpm-version }}
56+
57+
- name: Setup Node.js ${{ env.node-version }}
58+
uses: actions/setup-node@v4
59+
with:
60+
node-version: ${{ env.node-version }}
61+
cache: pnpm
62+
registry-url: ${{ inputs.npm-registry-url }}
63+
scope: '@codemonger-io'
64+
65+
- name: Install dependencies
66+
run: pnpm install
67+
68+
# the build script is executed by the prepare script
69+
- name: Build and publish
70+
env:
71+
NODE_AUTH_TOKEN: ${{ secrets.npm-token }}
72+
run: pnpm publish --no-git-checks

README.ja.md

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,40 @@ import { makeIntegrationResponsesAllowCors } from '@codemonger-io/cdk-cors-utils
2929

3030
このライブラリはCDK v2.x用に設計されており、CDK v1.xでは使えません。
3131

32+
### GitHub Packagesからインストールする
33+
34+
`main`ブランチにコミットがプッシュされるたびに、*開発者用パッケージ*がGitHub Packagesの管理するnpmレジストリにパブリッシュされます。
35+
*開発者用パッケージ*のバージョンは次のリリースバージョンにハイフン(`-`)と短いコミットハッシュをつなげたものになります。例、`0.4.0-abc1234` (`abc1234`はパッケージをビルドするのに使ったコミット(*スナップショット*)の短いコミットハッシュ)。
36+
*開発者用パッケージ*[こちら](https://github.com/orgs/codemonger-io/packages?repo_name=cdk-cors-utils)にあります。
37+
38+
#### GitHubパーソナルアクセストークンの設定
39+
40+
*開発者用パッケージ*をインストールするには、最低限`read:packages`スコープの*クラシック*GitHubパーソナルアクセストークン(PAT)を設定する必要があります。
41+
以下、簡単にPATの設定方法を説明します。
42+
より詳しくは[GitHubのドキュメント](https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-npm-registry)をご参照ください。
43+
44+
PATが手に入ったら以下の内容の`.npmrc`ファイルをホームディレクトリに作成してください。
45+
46+
```
47+
//npm.pkg.github.com/:_authToken=$YOUR_GITHUB_PAT
48+
```
49+
50+
`$YOUR_GITHUB_PAT`はご自身のPATに置き換えてください。
51+
52+
プロジェクトのルートディレクトリに以下の内容の`.npmrc`ファイルを作成してください。
53+
54+
```
55+
@codemonger-io:registry=https://npm.pkg.github.com
56+
```
57+
58+
これで以下のコマンドで*開発者用パッケージ*をインストールできます。
59+
60+
```sh
61+
npm install @codemonger-io/[email protected]
62+
```
63+
64+
`abc1234`はインストールしたい*スナップショット*の短いコミットハッシュに置き換えてください。
65+
3266
## API
3367

3468
[`api-docs/markdown/index.md`](./api-docs/markdown/index.md)を参照ください(**英語版のみ**)。
@@ -44,7 +78,7 @@ TBD
4478
`build`スクリプトは`src`フォルダ内のTypeScriptファイルをトランスパイルします。
4579

4680
```sh
47-
npm run build
81+
pnpm build
4882
```
4983

5084
トランスパイルされたJavaScript(`*.js`)ファイルと型宣言(`*.d.ts`)ファイルが`dist`フォルダに作られます。
@@ -54,7 +88,7 @@ npm run build
5488
`doc`スクリプトはAPIドキュメントを生成します。
5589

5690
```sh
57-
npm run doc
91+
pnpm doc
5892
```
5993

6094
`api-docs/markdown`フォルダ内のMarkdownファイルが更新されます。

README.md

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,40 @@ import { makeIntegrationResponsesAllowCors } from '@codemonger-io/cdk-cors-utils
2929

3030
This library is designed for CDK v2.x, and does not work with CDK v1.x.
3131

32+
### Installing from GitHub Packages
33+
34+
Every time commits are pushed to the `main` branch, a *developer package* is published to the npm registry managed by GitHub Packages.
35+
A *developer package* bears the next release number but followed by a dash (`-`) plus the short commit hash; e.g., `0.4.0-abc1234` where `abc1234` is the short commit hash of the commit used to build the package (*snapshot*).
36+
You can find *developer packages* [here](https://github.com/orgs/codemonger-io/packages?repo_name=cdk-cors-utils).
37+
38+
#### Configuring a GitHub personal access token
39+
40+
To install a *developer package*, you need to configure a **classic** GitHub personal access token (PAT) with at least the `read:packages` scope.
41+
Below briefly explains how to configure a PAT.
42+
Please refer to the [GitHub documentation](https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-npm-registry) for more details.
43+
44+
Once you have a PAT, please create a `.npmrc` file in your home directory with the following content:
45+
46+
```
47+
//npm.pkg.github.com/:_authToken=$YOUR_GITHUB_PAT
48+
```
49+
50+
Please replace `$YOUR_GITHUB_PAT` with your PAT.
51+
52+
In the root directory of your project, please create a `.npmrc` file with the following content:
53+
54+
```
55+
@codemonger-io:registry=https://npm.pkg.github.com
56+
```
57+
58+
Then you can install a *developer package* with the following command:
59+
60+
```sh
61+
npm install @codemonger-io/[email protected]
62+
```
63+
64+
Please replace `abc1234` with the short commit hash of the *snapshot* you want to install.
65+
3266
## APIs
3367

3468
Please refer to [`api-docs/markdown/index.md`](./api-docs/markdown/index.md).
@@ -41,10 +75,10 @@ TBD
4175

4276
### Transpiling TypeScript files
4377

44-
`build` script transplies TypeScript files in the `src` folder.
78+
`build` script transpiles TypeScript files in the `src` folder.
4579

4680
```sh
47-
npm run build
81+
pnpm build
4882
```
4983

5084
You will find transpiled JavaScript (`*.js`) and type declaration (`*.d.ts`) files created in a folder `dist`.
@@ -54,7 +88,7 @@ You will find transpiled JavaScript (`*.js`) and type declaration (`*.d.ts`) fil
5488
`doc` script generates the API documentation.
5589

5690
```sh
57-
npm run doc
91+
pnpm doc
5892
```
5993

6094
You will find markdown files updated in the folder `api-docs/markdown`.

0 commit comments

Comments
 (0)