1 name: Publish to npm
2
3 on:
4 push:
5 tags:
6 - "v*.*.*"
7
8 jobs:
9 publish-npm-binaries:
10 name: Publish NPM packages
11 runs-on: ${{ matrix.build.os }}
12 strategy:
13 fail-fast: false
14 matrix:
15 build:
16 - {
17 NAME: linux-x64-glibc,
18 OS: ubuntu-20.04,
19 TOOLCHAIN: stable,
20 TARGET: x86_64-unknown-linux-gnu,
21 }
22 # Cannot cross-compile for now
23 #- {
24 # NAME: linux-arm64-glibc,
25 # OS: ubuntu-20.04,
26 # TOOLCHAIN: stable,
27 # TARGET: aarch64-unknown-linux-gnu,
28 # }
29 - {
30 NAME: win32-x64-msvc,
31 OS: windows-2022,
32 TOOLCHAIN: stable,
33 TARGET: x86_64-pc-windows-msvc,
34 }
35 - {
36 NAME: win32-arm64-msvc,
37 OS: windows-2022,
38 TOOLCHAIN: stable,
39 TARGET: aarch64-pc-windows-msvc,
40 }
41 - {
42 NAME: darwin-x64,
43 OS: macos-11,
44 TOOLCHAIN: stable,
45 TARGET: x86_64-apple-darwin,
46 }
47 - {
48 NAME: darwin-arm64,
49 OS: macos-11,
50 TOOLCHAIN: stable,
51 TARGET: aarch64-apple-darwin,
52 }
53 steps:
54 - name: Checkout
55 uses: actions/checkout@v3
56
57 - name: Set the release version
58 shell: bash
59 run: echo "RELEASE_VERSION=${GITHUB_REF:11}" >> $GITHUB_ENV
60
61 - name: Install toolkit
62 uses: dtolnay/rust-toolchain@master
63 with:
64 targets: ${{ matrix.build.TARGET }}
65 toolchain: ${{ matrix.build.TOOLCHAIN}}
66
67 - name: Build
68 env:
69 GH_OAUTH_CLIENT_ID: ${{ secrets.GH_OAUTH_CLIENT_ID }}
70 GH_OAUTH_CLIENT_SECRET: ${{ secrets.GH_OAUTH_CLIENT_SECRET }}
71 GH_OAUTH_REDIRECT_URI: ${{ secrets.GH_OAUTH_REDIRECT_URI }}
72 shell: bash
73 run: |
74 cargo build --release --locked --target ${{ matrix.build.TARGET }}
75
76 - name: Install node
77 uses: actions/setup-node@v3
78 with:
79 node-version: "18"
80 registry-url: "https://registry.npmjs.org"
81
82 - name: Publish to NPM
83 shell: bash
84 run: |
85 # We want to name our npm package @smbpndk/cli, but the main command is smb.
86 # This is the tricky part. All the optional dependencies should follow the naming convention.
87 # So, we will generate @smbpndk/cli-${matrix.build.TARGET}
88
89 # Move to the npm directory
90 cd npm
91 # set the binary name, this is from the rust Cargo.toml file
92 cargo_bin="smb"
93 node_pkg_prefix="cli"
94 # derive the OS and architecture from the build matrix name
95 # note: when split by a hyphen, first part is the OS and the second is the architecture
96 node_os=$(echo "${{ matrix.build.NAME }}" | cut -d '-' -f1)
97 export node_os
98 node_arch=$(echo "${{ matrix.build.NAME }}" | cut -d '-' -f2)
99 export node_arch
100 # set the version
101 export node_version="${{ env.RELEASE_VERSION }}"
102 # set the package name
103 # note: use 'windows' as OS name instead of 'win32'
104 if [ "${{ matrix.build.OS }}" = "windows-2022" ]; then
105 export node_pkg="${node_pkg_prefix}-windows-${node_arch}"
106 else
107 export node_pkg="${node_pkg_prefix}-${node_os}-${node_arch}"
108 fi
109 # create the package directory
110 mkdir -p "${node_pkg}/bin"
111 # generate package.json from the template
112 envsubst < package.json.tmpl > "${node_pkg}/package.json"
113 # generate readme.md from the template
114 envsubst < README.md.tmpl > "${node_pkg}/README.md"
115 # copy the binary into the package
116 # note: windows binaries has '.exe' extension
117 if [ "${{ matrix.build.OS }}" = "windows-2022" ]; then
118 cargo_bin="${cargo_bin}.exe"
119 fi
120 cp "../target/${{ matrix.build.TARGET }}/release/${cargo_bin}" "${node_pkg}/bin"
121 # publish the package
122 cd "${node_pkg}"
123 npm publish --access public
124 env:
125 NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
126
127 publish-npm-base:
128 name: Publish the base NPM package
129 needs: publish-npm-binaries
130 runs-on: ubuntu-20.04
131 steps:
132 - name: Checkout
133 uses: actions/checkout@v3
134
135 - name: Set the release version
136 shell: bash
137 run: echo "RELEASE_VERSION=${GITHUB_REF:11}" >> $GITHUB_ENV
138
139 - name: Install node
140 uses: actions/setup-node@v3
141 with:
142 node-version: "18"
143 registry-url: "https://registry.npmjs.org"
144
145 - name: Publish the package
146 shell: bash
147 run: |
148 cd npm/smbpndk-cli
149
150 # set the version
151 export release_version="${{ env.RELEASE_VERSION }}"
152
153 # use the tag version
154 envsubst < ../package-main.json.tmpl > "./package.json"
155
156 yarn install # requires optional dependencies to be present in the registry
157 yarn build
158 npm publish --access public
159 env:
160 NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}