npmgitlab-ci

GitLab npm publish with build electron


My need is to deploy an executable packaged by electron-builder through a GitLab pipeline to go inside package registry.

Currently, I have made this workflow that run well.

    - nvm use 18.20.3
    - $Version = node -p "require('./package.json').version"
    - $FullVersion = $Version + $BETA_RELEASE_TAG
    - $FullFileName = 'moniteur-virtuel.setup.' + $Version + '.exe'
    - $FullFilePath = (Get-Location).Path + '\electron-dist\' + $FullFileName
    - (Get-Content .\src\polyfills.auto.ts) | Set-Content .\src\polyfills.ts
    - npm run update-scope # met a jour le package-lock.json
    - npm install
    - npm run list-scope # list les versions utilises
    - npm run package:windows
    - if (Test-Path .npmrc) { Remove-Item .npmrc; } # Remove .npmrc if existing
    - echo "strict-ssl = false" > .npmrc
    - echo "@cossilys:registry=https://${CI_SERVER_HOST}/api/v4/projects/${CI_PROJECT_ID}/packages/npm/" >> .npmrc
    - echo "//${CI_SERVER_HOST}/api/v4/projects/${CI_PROJECT_ID}/packages/npm/:_authToken=${CI_JOB_TOKEN}" >> .npmrc
    - cat .npmrc
    - (Get-Content .\package.json) -Replace ' true,', ' false,' | Set-Content .\package.json
    - Invoke-RestMethod -Headers @{ "JOB-TOKEN"="$CI_JOB_TOKEN" } -InFile $FullFilePath -uri "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/moniteur-virtuel/${FullVersion}/${FullFileName}" -Method put
    # - npm publish
    # - npm dist-tag add @cossilys/moniteur-virtuel@$FullVersion $FullVersion

As you can see, I will prefer to use a npm publish to have the posibility of npm dist-tag. But I don't know and I don't find how to set the npm publish to use npm run package:windows instead of npm run build (or maybe instead of ng build, I don't know what npm publish really does).


Solution

  • To integrate your Electron build into the npm publish flow, you can add a prepublishOnly or prepare script in your package.json. This ensures that electron-builder runs automatically before publishing. Also you can include .exe via files or exclude via .npmignore this will include only the packaged installer and avoid unnecessary files in the registry.

    Make sure your .npmrc is correctly configured to point to GitLab’s npm registry and uses the project-scoped auth token. Once this is in place, npm publish will push your package directly to GitLab, and you’ll be able to use npm dist-tag to manage version tags like beta or latest.

    By following this method, you eliminate the need for manually uploading the .exe via Invoke-RestMethod. Instead, you’re using the native npm workflow, which provides better version control, tagging, and package management.