From 2c185171215d329db0e93ca368736c3201e5d637 Mon Sep 17 00:00:00 2001 From: Mihai Parparita Date: Wed, 24 Aug 2022 18:05:42 -0700 Subject: [PATCH] cmd/tsconnect: add npm publish workflow Adds an on-demand GitHub Action that publishes the package to the npm registry (currently under tailscale-connect, will be moved to @tailscale/connect once we get control of the npm org). Makes the package.json for the NPM package be dynamically generated to have the current Tailscale client version. Updates #5415 Signed-off-by: Mihai Parparita --- .github/workflows/tsconnect-pkg-publish.yml | 30 ++++++++++++++++++ cmd/tsconnect/build-pkg.go | 35 +++++++++++++++++++++ cmd/tsconnect/package.json.tmpl | 17 ++++++++++ cmd/tsconnect/pkg/package.json | 10 ------ 4 files changed, 82 insertions(+), 10 deletions(-) create mode 100644 .github/workflows/tsconnect-pkg-publish.yml create mode 100644 cmd/tsconnect/package.json.tmpl delete mode 100644 cmd/tsconnect/pkg/package.json diff --git a/.github/workflows/tsconnect-pkg-publish.yml b/.github/workflows/tsconnect-pkg-publish.yml new file mode 100644 index 000000000..29bd2926e --- /dev/null +++ b/.github/workflows/tsconnect-pkg-publish.yml @@ -0,0 +1,30 @@ +name: "@tailscale/connect npm publish" + +on: workflow_dispatch + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Set up node + uses: actions/setup-node@v3 + with: + node-version: "16.x" + registry-url: "https://registry.npmjs.org" + + - name: Build package + # Build with build_dist.sh to ensure that version information is embedded. + # GOROOT is specified so that the Go/Wasm that is trigged by build-pk + # also picks up our custom Go toolchain. + run: | + ./build_dist.sh tailscale.com/cmd/tsconnect + GOROOT="${HOME}/.cache/tailscale-go" ./tsconnect build-pkg + + - name: Publish + env: + NODE_AUTH_TOKEN: ${{ secrets.TSCONNECT_NPM_PUBLISH_AUTH_TOKEN }} + run: ./tool/yarn --cwd ./cmd/tsconnect/pkg publish --access public diff --git a/cmd/tsconnect/build-pkg.go b/cmd/tsconnect/build-pkg.go index 75b4f4cab..26700d192 100644 --- a/cmd/tsconnect/build-pkg.go +++ b/cmd/tsconnect/build-pkg.go @@ -5,9 +5,15 @@ package main import ( + "encoding/json" + "fmt" "log" + "os" + "path" esbuild "github.com/evanw/esbuild/pkg/api" + "github.com/tailscale/hujson" + "tailscale.com/version" ) func runBuildPkg() { @@ -41,4 +47,33 @@ func runBuildPkg() { log.Fatalf("Type generation failed: %v", err) } + if err := updateVersion(); err != nil { + log.Fatalf("Cannot update version: %v", err) + } + + log.Printf("Built package version %s", version.Long) +} + +func updateVersion() error { + packageJSONBytes, err := os.ReadFile("package.json.tmpl") + if err != nil { + return fmt.Errorf("Could not read package.json: %w", err) + } + + var packageJSON map[string]any + packageJSONBytes, err = hujson.Standardize(packageJSONBytes) + if err != nil { + return fmt.Errorf("Could not standardize template package.json: %w", err) + } + if err := json.Unmarshal(packageJSONBytes, &packageJSON); err != nil { + return fmt.Errorf("Could not unmarshal package.json: %w", err) + } + packageJSON["version"] = version.Long + + packageJSONBytes, err = json.MarshalIndent(packageJSON, "", " ") + if err != nil { + return fmt.Errorf("Could not marshal package.json: %w", err) + } + + return os.WriteFile(path.Join(*pkgDir, "package.json"), packageJSONBytes, 0644) } diff --git a/cmd/tsconnect/package.json.tmpl b/cmd/tsconnect/package.json.tmpl new file mode 100644 index 000000000..0d911b8f7 --- /dev/null +++ b/cmd/tsconnect/package.json.tmpl @@ -0,0 +1,17 @@ +// Copyright (c) 2022 Tailscale Inc & AUTHORS All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Template for the package.json that is generated by the build-pkg command. +// The version number will be replaced by the current Tailscale client version +// number. +{ + "author": "Tailscale Inc.", + "description": "Tailscale Connect SDK", + "license": "BSD-3-Clause", + "name": "tailscale-connect", + "type": "module", + "main": "./pkg.js", + "types": "./pkg.d.ts", + "version": "AUTO_GENERATED" +} diff --git a/cmd/tsconnect/pkg/package.json b/cmd/tsconnect/pkg/package.json deleted file mode 100644 index 366a63f96..000000000 --- a/cmd/tsconnect/pkg/package.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "author": "Tailscale Inc.", - "description": "Tailscale Connect SDK", - "license": "BSD-3-Clause", - "name": "@tailscale/connect", - "type": "module", - "main": "./pkg.js", - "types": "./pkg.d.ts", - "version": "0.0.5" -}