Post

GitHub Packages: Migrate npm Packages Between GitHub Instances

Overview

I have been working with more customers who are migrating GitHub instances and want to be able to migrate GitHub Packages. There is not an easy lift-and-shift approach to migrate GitHub Packages between instances; each ecosystem is independent. To go along with my NuGet solution, I also scripted out the npm package migration. Take a look and let me know what you think!

See my other GitHub Package –> GitHub Package migration posts:

The script

The script can be found in my github-misc-scripts repo here:

Running the script

Prerequisites

  1. gh cli installed
  2. Set the source GitHub PAT env var: export GH_SOURCE_PAT=ghp_abc (must have at least read:packages, read:org scope)
  3. Set the target GitHub PAT env var: export GH_TARGET_PAT=ghp_xyz (must have at least write:packages, read:org scope)

Notes:

  • This script assumes that the target org’s repo name is the same as the source
  • If the repo doesn’t exist, the package will still import but won’t be mapped to a repo

Usage

You can call the script via:

1
2
3
4
5
./migrate-npm-packages-between-github-instances.sh \
  <source-org> \
  <source-host> \
  <target-org> \
  <target-host>

Example

An example of this in practice:

1
2
3
4
5
6
7
8
export GH_SOURCE_PAT=ghp_abc
export GH_TARGET_PAT=ghp_xyz

./migrate-npm-packages-between-github-instances.sh \
  joshjohanning-org \
  github.com \
  joshjohanning-org-packages \
  github.com

Notes

  • This script assumes that the target org’s repo name is the same as the source
  • If the repo doesn’t exist, the package will still import but won’t be mapped to a repo
  • This script uses RegEx to find/replace source org with target org in the package’s package.json (see the GitHub docs for more info)
  • To clean up the working directory when done, run this one-liner:
    1
    
    rm -rf ./temp
    

Improvement Ideas

  • Add a source folder input instead of relying on current directory (just using ./temp)
  • Map between repositories where the target repo is named differently than the source repo (likely this isn’t needed since if repo doesn’t exist, packages will still be pushed, the package just won’t be linked to a repository)
  • Update script because of GitHub Packages GraphQL deprecation

Summary

Drop a comment here or an issue or PR on my github-misc-scripts repo if you have any feedback or suggestions! Happy packaging! 📦

This post is licensed under CC BY 4.0 by the author.