Publish Package to npmjs #207
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Publish Package to npmjs | |
| on: | |
| workflow_dispatch: | |
| inputs: | |
| package_name: | |
| description: Select the package to publish or choose "all" to publish all specified packages | |
| type: choice | |
| required: true | |
| options: | |
| - all | |
| - design-extensions-tools | |
| - csk-components | |
| - csk-cli | |
| - csk-recipes | |
| publish_env: | |
| description: Select the environment to publish to (production or alpha) | |
| type: choice | |
| required: true | |
| options: | |
| - production | |
| - alpha | |
| jobs: | |
| publish_package: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: read | |
| id-token: write | |
| env: | |
| PACKAGES: "csk-cli design-extensions-tools csk-components csk-recipes" | |
| steps: | |
| # Step 1: Checkout repository | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| # Step 2: Setup Node.js environment | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: "20.x" | |
| registry-url: "https://registry.npmjs.org" | |
| # Step 3: Export NPM_TOKEN for .npmrc | |
| - name: 🌱 Export NPM_TOKEN | |
| run: echo "NPM_TOKEN=${{ secrets.NPM_TOKEN }}" >> $GITHUB_ENV | |
| # Step 4: Install dependencies | |
| - name: Install dependencies | |
| run: npm ci | |
| # Step 5: Publish Selected Package(s) | |
| - name: Publish Package(s) | |
| run: | | |
| if [ "${{ inputs.publish_env }}" == "alpha" ]; then | |
| PUBLISH_TAG="--tag alpha" | |
| else | |
| PUBLISH_TAG="" | |
| fi | |
| if [ "${{ inputs.package_name }}" == "all" ]; then | |
| echo "Publishing package(s): ${{ inputs.package_name }}" | |
| IFS=' ' read -r -a package_array <<< "$PACKAGES" | |
| for i in "${!package_array[@]}"; do | |
| pkg=${package_array[$i]} | |
| echo "Publishing package: $pkg to ${{ inputs.publish_env }} environment" | |
| cd ./packages/$pkg | |
| npm run build | |
| npm publish $PUBLISH_TAG | |
| if [ $? -eq 0 ]; then | |
| if [ $i -lt $((${#package_array[@]} - 1)) ]; then | |
| echo "Successfully published $pkg. Waiting for 20 seconds before publishing the next package..." | |
| sleep 20 | |
| else | |
| echo "Successfully published the last package: $pkg." | |
| fi | |
| else | |
| echo "Failed to publish $pkg. Exiting..." | |
| exit 1 | |
| fi | |
| cd - | |
| done | |
| else | |
| echo "Publishing selected package: ${{ inputs.package_name }} to ${{ inputs.publish_env }} environment" | |
| cd ./packages/${{ inputs.package_name }} | |
| npm run build | |
| npm publish $PUBLISH_TAG | |
| fi | |
| env: | |
| NODE_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }} |