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