Skip to content

Commit 9e4f21e

Browse files
authored
Publish a developer package to GitHub Packages (#7)
* 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 a developer package from GitHub Packages - Explains how to configure a GitHub personal access token to install a developer package
1 parent 6ad6faf commit 9e4f21e

4 files changed

Lines changed: 160 additions & 0 deletions

File tree

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: Installs 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: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,40 @@
1212
npm install https://github.com/codemonger-io/cdk-ghost-string-parameter.git#v0.2.0
1313
```
1414

15+
### GitHub Packagesからインストールする
16+
17+
`main`ブランチにコミットがプッシュされるたびに、*開発者用パッケージ*がGitHub Packagesが管理するnpmレジストリにパブリッシュされます。
18+
*開発者用パッケージ*のバージョンは次のリリースバージョンにハイフン(`-`)と短いコミットハッシュをつなげたものになります。例、`0.2.0-abc1234` (`abc1234`はパッケージをビルドするのに使ったコミット(*スナップショット*)の短いコミットハッシュ)。
19+
*開発者用パッケージ*[こちら](https://github.com/codemonger-io/cdk-ghost-string-parameter/pkgs/npm/cdk-ghost-string-parameter)にあります。
20+
21+
#### GitHubパーソナルアクセストークンの設定
22+
23+
*開発者用パッケージ*をインストールするには、最低限`read:packages`スコープの**クラシック**GitHubパーソナルアクセストークン(PAT)を設定する必要があります。
24+
以下、簡単にPATの設定方法を説明します。
25+
より詳しくは[GitHubのドキュメント](https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-npm-registry)をご参照ください。
26+
27+
PATが手に入ったら以下の内容の`.npmrc`ファイルをホームディレクトリに作成してください。
28+
29+
```
30+
//npm.pkg.github.com/:_authToken=$YOUR_GITHUB_PAT
31+
```
32+
33+
`$YOUR_GITHUB_PAT`はご自身のPATに置き換えてください。
34+
35+
プロジェクトのルートディレクトリに以下の内容の`.npmrc`ファイルを作成してください。
36+
37+
```
38+
@codemonger-io:registry=https://npm.pkg.github.com
39+
```
40+
41+
これで以下のコマンドで*開発者用パッケージ*をインストールできます。
42+
43+
```sh
44+
npm install @codemonger-io/cdk-ghost-string-parameter@0.2.0-abc1234
45+
```
46+
47+
`abc1234`はインストールしたい*スナップショット*の短いコミットハッシュに置き換えてください。
48+
1549
## サンプル
1650

1751
このライブラリが提供する唯一のクラスが[`GhostStringParameter`](./api-docs/markdown/cdk-ghost-string-parameter.md)です。

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,40 @@ This library is supposed to be combined with the [AWS Cloud Development Kit (CDK
1212
npm install https://github.com/codemonger-io/cdk-ghost-string-parameter.git#v0.2.0
1313
```
1414

15+
### Installing from GitHub Packages
16+
17+
Every time commits are pushed to the `main` branch, a _developer package_ is published to the npm registry managed by GitHub Packages.
18+
A _developer package_ bears the next release number but followed by a dash (`-`) plus the short commit hash; e.g., `0.2.0-abc1234` where `abc1234` is the short commit hash of the commit used to build the package (_snapshot_).
19+
You can find _developer packages_ [here](https://github.com/codemonger-io/cdk-ghost-string-parameter/pkgs/npm/cdk-ghost-string-parameter).
20+
21+
#### Configuring a GitHub personal access token
22+
23+
To install a _developer package_, you need to configure a **classic** GitHub personal access token (PAT) with at least the `read:packages` scope.
24+
Below briefly explains how to configure a PAT.
25+
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.
26+
27+
Once you have a PAT, please create a `.npmrc` file in your home directory with the following content:
28+
29+
```
30+
//npm.pkg.github.com/:_authToken=$YOUR_GITHUB_PAT
31+
```
32+
33+
Please replace `$YOUR_GITHUB_PAT` with your PAT.
34+
35+
In the root directory of your project, create another `.npmrc` file with the following content:
36+
37+
```
38+
@codemonger-io:registry=https://npm.pkg.github.com
39+
```
40+
41+
Then you can install a _developer package_ with the following command:
42+
43+
```sh
44+
npm install @codemonger-io/cdk-ghost-string-parameter@0.2.0-abc1234
45+
```
46+
47+
Please replace `abc1234` with the short commit hash of the _snapshot_ you want to install.
48+
1549
## Example
1650

1751
The only class this library provides is [`GhostStringParameter`](./api-docs/markdown/cdk-ghost-string-parameter.md).

0 commit comments

Comments
 (0)