javascriptcontinuous-integrationgithub-actions

How to find Package.json version, and use as variable in Github Action


I have a Github action shown below. My build artifact increments with my version in package.json for an Angular application. How do I reference the new version in the willo google drive upload action below?

strategy:
      matrix:
        os: [macos-latest]

    steps:
    - uses: actions/checkout@v4
    - uses: oven-sh/setup-bun@v1
      with:
        bun-version: "latest"

    - name: Install dependencies
      run: bun install

    - name: Install NX
      run: bun i nx -g

    - name: Build MacOS
      if: startsWith(matrix.os, 'macos')
      run: nx run app:build-distro-mac-ci

    - uses: actions/upload-artifact@v4
      if: startsWith(matrix.os, 'macos')
      with:
        name: app-mac
        path: dist/*.dmg
        retention-days: 7

    - uses: willo32/google-drive-upload-action@v1
      if: startsWith(matrix.os, 'macos')
      with:
        target: dist/app-2.0.1.dmg
        credentials: ${{ secret }}
        parent_folder_id: some_id

Solution

  • Add a step to extract the version from package.json and set it as an environment variable

      - name: Extract version
      id: version
      run: echo "version=$(node -p "require('./package.json').version")" >> $GITHUB_ENV
    

    Then use it in the Google Drive upload step:

      - uses: willo32/google-drive-upload-action@v1
      with:
        target: dist/app-${{ env.version }}.dmg
        credentials: ${{ secrets.GOOGLE_DRIVE_CREDENTIALS }}
        parent_folder_id: some_id