|
| 1 | +--- |
| 2 | +title: 'GitHub Packages: Migrate Maven Packages Between GitHub Instances' |
| 3 | +author: Josh Johanning |
| 4 | +date: 2023-09-25 14:30:00 -0500 |
| 5 | +description: Migrating Maven packages stored in GitHub Packages from one instance to another |
| 6 | +categories: [GitHub, Migrations] |
| 7 | +tags: [GitHub, Scripts, GitHub Packages, gh cli, Maven, Migrations] |
| 8 | +img_path: /assets/screenshots/2023-09-25-github-packages-migrate-maven-packages |
| 9 | +image: |
| 10 | + path: maven-packages-light.png |
| 11 | + width: 100% |
| 12 | + height: 100% |
| 13 | + alt: Maven packages in GitHub Packages |
| 14 | +--- |
| 15 | + |
| 16 | +## Overview |
| 17 | + |
| 18 | +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](/posts/github-packages-migrate-nuget-packages/) and [npm](/posts/github-packages-migrate-npm-packages/) solutions, I also scripted out the Maven package migration. Take a look and let me know what you think! |
| 19 | + |
| 20 | +> See my other GitHub Package --> GitHub Package migration posts: |
| 21 | +> - [Migrate NuGet Packages Between GitHub Instances](/posts/github-packages-migrate-nuget-packages/) |
| 22 | +> - [Migrate npm Packages Between GitHub Instances](/posts/github-packages-migrate-npm-packages/) |
| 23 | +{: .prompt-info } |
| 24 | + |
| 25 | +## The script |
| 26 | + |
| 27 | +The script can be found in my [github-misc-scripts](/posts/github-misc-scripts/) repo here: |
| 28 | +- **[https://github.com/joshjohanning/github-misc-scripts/blob/main/scripts/migrate-maven-packages-between-github-instances.sh](https://github.com/joshjohanning/github-misc-scripts/blob/main/scripts/migrate-maven-packages-between-github-instances.sh)** |
| 29 | + |
| 30 | +## Running the script |
| 31 | + |
| 32 | +### Prerequisites |
| 33 | + |
| 34 | +1. [`gh cli`](https://cli.github.com) installed |
| 35 | +2. Set the source GitHub PAT env var: `export GH_SOURCE_PAT=ghp_abc` (must have at least `read:packages`, `read:org` scope) |
| 36 | +3. Set the target GitHub PAT env var: `export GH_TARGET_PAT=ghp_xyz` (must have at least `write:packages`, `read:org`, `repo` scope) |
| 37 | + |
| 38 | +Notes: |
| 39 | + |
| 40 | +- Until Maven supports the new GitHub Packages type, `mvnfeed` requires the target repo to exist |
| 41 | +- Link to [GitHub public roadmap item](https://github.com/github/roadmap/issues/578) |
| 42 | +- This scripts creates the repo if it doesn't exist |
| 43 | +- Otherwise, if the repo doesn't exist, receive "`example-1.0.5.jar`{: .filepath} was not found in the repository" error |
| 44 | +- Currently [`mvnfeed-cli`](https://github.com/microsoft/mvnfeed-cli) only supports migrating `.jar`{: .filepath} and `.pom`{: .filepath} files (not `.war`{: .filepath}) |
| 45 | + |
| 46 | +### Usage |
| 47 | + |
| 48 | +You can call the script via: |
| 49 | + |
| 50 | +```bash |
| 51 | +./migrate-maven-packages-between-github-instances.sh \ |
| 52 | + <source-org> \ |
| 53 | + <source-host> \ |
| 54 | + <target-org> \ |
| 55 | + <target-host> |
| 56 | +``` |
| 57 | + |
| 58 | +### Example |
| 59 | + |
| 60 | +An example of this in practice: |
| 61 | + |
| 62 | +```bash |
| 63 | +export GH_SOURCE_PAT=ghp_abc |
| 64 | +export GH_TARGET_PAT=ghp_xyz |
| 65 | + |
| 66 | +./migrate-maven-packages-between-github-instances.sh \ |
| 67 | + joshjohanning-org \ |
| 68 | + github.com \ |
| 69 | + joshjohanning-org-packages \ |
| 70 | + github.com |
| 71 | +``` |
| 72 | + |
| 73 | +## Notes |
| 74 | + |
| 75 | +- This script assumes that the target org's repo name is the same as the source |
| 76 | +- If the repo doesn't exist, the package will still import but won't be mapped to a repo |
| 77 | +- The script uses [`mvnfeed-cli`](https://github.com/microsoft/mvnfeed-cli) to migrate Maven packages to the target org |
| 78 | +- Currently `mvnfeed-cli` only supports migrating `.jar`{: .filepath} + `.pom`{: .filepath} files (not `.war`{: .filepath}) |
| 79 | +- To clean up the working directory when done, run this one-liner: |
| 80 | + ```bash |
| 81 | + rm -rf ./temp |
| 82 | + ``` |
| 83 | + {: .nolineno} |
| 84 | + |
| 85 | +## Improvement Ideas |
| 86 | + |
| 87 | +* [x] Add a source folder input instead of relying on current directory (just using `./temp`{: .filepath}) |
| 88 | +* [ ] Map between repositories where the target repo is named differently than the source repo |
| 89 | +* [x] Update script because of GitHub Packages GraphQL [deprecation](https://github.blog/changelog/2022-08-18-deprecation-notice-graphql-for-packages/) |
| 90 | +* [ ] Fork [`mvnfeed-cli`](https://github.com/microsoft/mvnfeed-cli) to support migrating `.war`{: .filepath} files ([issue](https://github.com/microsoft/mvnfeed-cli/issues/16)) - this only supports `.jar`{: .filepath} files currently |
| 91 | + |
| 92 | +## Summary |
| 93 | + |
| 94 | +Drop a comment here or an issue or PR on my [github-misc-scripts repo](https://github.com/joshjohanning/github-misc-scripts/blob/main/scripts/migrate-maven-packages-between-github-instances.sh) if you have any feedback or suggestions! Happy packaging! 📦 |
0 commit comments