NPM private git module on Heroku

后端 未结 10 1136
不知归路
不知归路 2020-12-07 13:05

I am trying to deploy my app to Heroku however I rely on using some private git repos as modules. I do this for code reuse between projects, e.g. I have a custom logger I us

10条回答
  •  情书的邮戳
    2020-12-07 14:03

    I was able to setup resolving of Github private repositories in Heroku build via Personal access tokens.

    • Generate Github access token here: https://github.com/settings/tokens
    • Set access token as Heroku config var: heroku config:set GITHUB_TOKEN= --app your-app-name or via Heroku Dashboard
    • Add heroku-prebuild.sh script:

      #!/bin/bash
      if [ "$GITHUB_TOKEN" != "" ]; then
          echo "Detected GITHUB_TOKEN. Setting git config to use the security token" >&1
          git config --global url."https://${GITHUB_TOKEN}@github.com/".insteadOf git@github.com:
      fi
      
    • add the prebuild script to package.json:

      "scripts": {
          "heroku-prebuild": "bash heroku-prebuild.sh"
      }
      

    For local environment we can also use git config ... or we can add the access token to ~/.netrc file:

    machine github.com
      login PASTE_GITHUB_USERNAME_HERE
      password PASTE_GITHUB_TOKEN_HERE
    

    and installing private github repos should work.

    npm install OWNER/REPO --save will appear in package.json as: "REPO": "github:OWNER/REPO"

    and resolving private repos in Heroku build should also work. optionally you can setup a postbuild script to unset the GITHUB_TOKEN.

提交回复
热议问题