Skip to content

Commit

Permalink
Merge pull request #849 from openmeterio/generate-node-sdk
Browse files Browse the repository at this point in the history
Generate node and web sdk with Dagger
  • Loading branch information
sagikazarmark authored May 7, 2024
2 parents 9e95248 + 57eba45 commit 1b6c9d6
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 60 deletions.
45 changes: 13 additions & 32 deletions .github/workflows/generate-client-node.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Node.js Client Generation
name: Generate / Node SDK

permissions:
contents: write
Expand All @@ -17,42 +17,23 @@ jobs:
generate:
runs-on: ubuntu-latest
steps:
- uses: actions/create-github-app-token@v1
id: app-token
- name: Checkout repository
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
with:
app-id: ${{ vars.BOT_APP_ID }}
private-key: ${{ secrets.BOT_APP_PRIVATE_KEY }}
- uses: actions/checkout@v4
with:
token: ${{ steps.app-token.outputs.token }}
ref: ${{ github.head_ref }}
# Make sure the value of GITHUB_TOKEN will not be persisted in repo's config
persist-credentials: false
- uses: pnpm/action-setup@v3
with:
version: 8
- name: Install Node.js
uses: actions/setup-node@v4
with:
node-version: "20"
cache: "pnpm"
cache-dependency-path: "./api/client/node/pnpm-lock.yaml"
- name: Install Dependencies
run: pnpm install --frozen-lockfile
working-directory: ./api/client/node

- name: Generate Client
run: pnpm run generate
working-directory: ./api/client/node
- name: Build Client
run: pnpm build
working-directory: ./api/client/node
- name: Run tests
run: pnpm test
working-directory: ./api/client/node
uses: dagger/dagger-for-github@eba69b4dddb54eddfdb51a88eb7fd86957137630 # v5.4.0
with:
verb: call
args: --source .:default generate node-sdk -o api/client/node
cloud-token: ${{ secrets.DAGGER_CLOUD_TOKEN }}
version: "0.11.2"

- name: Open Pull Request
uses: peter-evans/create-pull-request@v6
with:
branch: openapi/node-client
branch: openapi/node-sdk
branch-suffix: short-commit-hash
delete-branch: true
commit-message: "chore(api): generate node client"
Expand All @@ -61,4 +42,4 @@ jobs:
labels: |
area/api
release-note/misc
token: ${{ steps.app-token.outputs.token }}
token: ${{ secrets.BOT_GITHUB_TOKEN }}
39 changes: 13 additions & 26 deletions .github/workflows/generate-client-web.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Web Client Generation
name: Generate / Web SDK

permissions:
contents: write
Expand All @@ -17,36 +17,23 @@ jobs:
generate:
runs-on: ubuntu-latest
steps:
- uses: actions/create-github-app-token@v1
id: app-token
- name: Checkout repository
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
with:
app-id: ${{ vars.BOT_APP_ID }}
private-key: ${{ secrets.BOT_APP_PRIVATE_KEY }}
- uses: actions/checkout@v4
with:
token: ${{ steps.app-token.outputs.token }}
ref: ${{ github.head_ref }}
# Make sure the value of GITHUB_TOKEN will not be persisted in repo's config
persist-credentials: false
- uses: pnpm/action-setup@v3
with:
version: 8
- name: Install Node.js
uses: actions/setup-node@v4
with:
node-version: "20"
cache: "pnpm"
cache-dependency-path: "./api/client/web/pnpm-lock.yaml"
- name: Install Dependencies
run: pnpm install --frozen-lockfile
working-directory: ./api/client/web

- name: Generate Client
run: pnpm run generate
working-directory: ./api/client/web
uses: dagger/dagger-for-github@eba69b4dddb54eddfdb51a88eb7fd86957137630 # v5.4.0
with:
verb: call
args: --source .:default generate web-sdk -o api/client/web
cloud-token: ${{ secrets.DAGGER_CLOUD_TOKEN }}
version: "0.11.2"

- name: Open Pull Request
uses: peter-evans/create-pull-request@v6
with:
branch: openapi/web-client
branch: openapi/web-sdk
branch-suffix: short-commit-hash
delete-branch: true
commit-message: "chore(api): generate web client"
Expand All @@ -55,4 +42,4 @@ jobs:
labels: |
area/api
release-note/misc
token: ${{ steps.app-token.outputs.token }}
token: ${{ secrets.BOT_GITHUB_TOKEN }}
3 changes: 2 additions & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ jobs:
with:
verb: call
module: github.com/${{ github.repository }}@${{ github.ref }}
args: --ref ${{ github.ref }} release --version ${{ github.ref_name }} --github-actor ${{ github.actor }} --github-token env:GITHUB_TOKEN --pypi-token env:PYPI_TOKEN
args: --ref ${{ github.ref }} release --version ${{ github.ref_name }} --github-actor ${{ github.actor }} --github-token env:GITHUB_TOKEN --pypi-token env:PYPI_TOKEN --npm-token env:NPM_TOKEN
cloud-token: ${{ secrets.DAGGER_CLOUD_TOKEN }}
version: "0.11.2"
env:
GITHUB_TOKEN: ${{ github.token }}
PYPI_TOKEN: ${{ secrets.PYPI_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
26 changes: 26 additions & 0 deletions ci/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,29 @@ func (m *Generate) PythonSdk() *Directory {
WithExec([]string{"autorest", "config.yaml"}).
Directory("/work/client/python")
}

// Generate the Node SDK.
func (m *Generate) NodeSdk() *Directory {
return dag.Container().
From("node:20-alpine").
WithExec([]string{"npm", "install", "-g", "pnpm"}).
WithDirectory("/work", m.Source.Directory("api")).
WithWorkdir("/work/client/node").
WithExec([]string{"pnpm", "install", "--frozen-lockfile"}).
WithExec([]string{"pnpm", "run", "generate"}).
WithExec([]string{"pnpm", "build"}).
WithExec([]string{"pnpm", "test"}).
Directory("/work/client/node")
}

// Generate the Web SDK.
func (m *Generate) WebSdk() *Directory {
return dag.Container().
From("node:20-alpine").
WithExec([]string{"npm", "install", "-g", "pnpm"}).
WithDirectory("/work", m.Source.Directory("api")).
WithWorkdir("/work/client/web").
WithExec([]string{"pnpm", "install", "--frozen-lockfile"}).
WithExec([]string{"pnpm", "run", "generate"}).
Directory("/work/client/web")
}
2 changes: 2 additions & 0 deletions ci/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ func (m *Ci) Ci(ctx context.Context) error {
wrapSyncables(m.releaseAssets("ci")),

wrapSyncable(m.Generate().PythonSdk()),
wrapSyncable(m.Generate().NodeSdk()),
wrapSyncable(m.Generate().WebSdk()),
)

return p.wait()
Expand Down
34 changes: 33 additions & 1 deletion ci/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"time"
)

func (m *Ci) Release(ctx context.Context, version string, githubActor string, githubToken *Secret, pypiToken *Secret) error {
func (m *Ci) Release(ctx context.Context, version string, githubActor string, githubToken *Secret, pypiToken *Secret, npmToken *Secret) error {
p := newPipeline(ctx)

p.addJobs(
Expand Down Expand Up @@ -43,6 +43,12 @@ func (m *Ci) Release(ctx context.Context, version string, githubActor string, gi
func(ctx context.Context) error {
return m.publishPythonSdk(ctx, version, pypiToken)
},
func(ctx context.Context) error {
return m.publishNodeSdk(ctx, version, npmToken)
},
func(ctx context.Context) error {
return m.publishWebSdk(ctx, version, npmToken)
},
)

return p.wait()
Expand Down Expand Up @@ -120,3 +126,29 @@ func (m *Ci) publishPythonSdk(ctx context.Context, version string, pypiToken *Se

return err
}

func (m *Ci) publishNodeSdk(ctx context.Context, version string, npmToken *Secret) error {
// TODO: generate SDK on the fly?
return m.publishToNpm(ctx, m.Source.Directory("api/client/node"), version, npmToken)
}

func (m *Ci) publishWebSdk(ctx context.Context, version string, npmToken *Secret) error {
// TODO: generate SDK on the fly?
return m.publishToNpm(ctx, m.Source.Directory("api/client/web"), version, npmToken)
}

func (m *Ci) publishToNpm(ctx context.Context, pkg *Directory, version string, npmToken *Secret) error {
_, err := dag.Container().
From("node:20-alpine").
WithExec([]string{"npm", "install", "-g", "pnpm"}).
WithExec([]string{"sh", "-c", "echo '//registry.npmjs.org/:_authToken=${NPM_TOKEN}' > /root/.npmrc"}).
WithDirectory("/work", pkg).
WithExec([]string{"pnpm", "install", "--frozen-lockfile"}).
WithExec([]string{"pnpm", "version", version, "--no-git-tag-version"}).
WithEnvVariable("CACHE_BUSTER", time.Now().Format(time.RFC3339Nano)).
WithSecretVariable("NPM_TOKEN", npmToken).
WithExec([]string{"pnpm", "--access=public", "--no-git-checks"}).
Sync(ctx)

return err
}

0 comments on commit 1b6c9d6

Please sign in to comment.