Skip to content

Commit 12af741

Browse files
updating nuget migration post with new version
1 parent 028d0af commit 12af741

File tree

1 file changed

+21
-40
lines changed

1 file changed

+21
-40
lines changed

_posts/2022-11-23-github-packages-migrate-nuget-packages.md

Lines changed: 21 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -33,31 +33,14 @@ I decided to store the script in a separate GitHub repo than my [github-misc-scr
3333

3434
### Prerequisites
3535

36-
1. [`gh cli`](https://cli.github.com) installed and logged in to be able to access the source GitHub instance:
37-
```bash
38-
gh auth login
39-
```
40-
{: .nolineno}
41-
2. Auth to read packages from the source GitHub instance with `gh`:
42-
```bash
43-
gh auth refresh -h github.com -s read:packages # update -h with source github host
44-
```
45-
{: .nolineno}
46-
3. [`gpr`](https://github.com/jcansdale/gpr) installed:
47-
```bash
48-
dotnet tool install gpr -g
49-
```
50-
{: .nolineno}
51-
4. Can use this one-liner to find the absolute path for [`gpr`](https://github.com/jcansdale/gpr) for the `<path-to-gpr>` parameter:
52-
```bash
53-
find / -wholename "*tools/gpr" 2> /dev/null
54-
```
55-
{: .nolineno}
56-
5. `<source-pat>` must have `read:packages` scope
57-
6. `<target-pat>` must have `write:packages` scope
58-
7. This assumes that the target org's repo name is the same as the source
59-
60-
We are passing [`gpr`](https://github.com/jcansdale/gpr) in as a parameter explicitly because sometimes [`gpr`](https://github.com/jcansdale/gpr) is aliased to `git pull --rebase` and that's not what we want here.
36+
1. [gh cli](https://cli.github.com) installed
37+
2. Set the source GitHub PAT env var: `export GH_SOURCE_PAT=ghp_abc` (must have at least `read:packages`, `read:org` scope)
38+
3. Set the target GitHub PAT env var: `export GH_TARGET_PAT=ghp_xyz` (must have at least `write:packages`, `read:org` scope)
39+
40+
Notes:
41+
42+
- This script installs [gpr](https://github.com/jcansdale/gpr) locally to the `./temp/tools`{: .filepath} directory
43+
- This script assumes that the target org's repo name is the same as the source (the target repo doesn't _need_ to exist, the package just won't be mapped to a repo)
6144

6245
### Usage
6346

@@ -67,29 +50,27 @@ You can call the script via:
6750
./migrate-nuget-packages-between-orgs.sh \
6851
<source-org>
6952
<source-host> \
70-
<source-pat> \
71-
<target-org> \
72-
<target-pat> \
73-
<path-to-gpr>
53+
<target-org>
7454
```
7555

7656
### Example
7757

7858
An example of this in practice:
7959

8060
```bash
61+
export GH_SOURCE_PAT=ghp_abc
62+
export GH_TARGET_PAT=ghp_xyz
63+
8164
./migrate-nuget-packages-between-orgs.sh \
8265
joshjohanning-org-packages \
8366
github.com \
84-
ghp_abc \
85-
joshjohanning-org-packages-migrated \
86-
ghp_xyz \
87-
/home/codespace/.dotnet/tools/gpr
67+
joshjohanning-org-packages-migrated
8868
```
8969

9070
## Notes
9171

92-
- The script uses [`gpr`](https://github.com/jcansdale/gpr) to re-push the packages to the target org. It assumes that the target org's repo has the same name as the source.
72+
- The script assumes that the target org's repo has the same name as the source - it's not required, but the package won't be mapped to a repo if the target repo doesn't exist
73+
- The script uses [`gpr`](https://github.com/jcansdale/gpr) to re-push the packages to the target org
9374
+ I initially tried writing this with [`dotnet nuget push`](https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-nuget-push), but that doesn't seem to work since the package's `<RepositoryUrl>` element would still be referencing the original repository. See error:
9475

9576
```
@@ -107,9 +88,9 @@ An example of this in practice:
10788
{: file='\'dotnet nuget push\' error'}
10889
10990
+ [`gpr`](https://github.com/jcansdale/gpr) works because it rewrites the `<repository url="..." />` element in the `.nuspec`{: .filepath} file in the `.nupkg`{: .filepath} before pushing
110-
+ There is an item on [GitHub's roadmap](https://github.com/github/roadmap/issues/589) to support pushing packages directly to an organization; this should allow [`dotnet nuget push`](https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-nuget-push) to work instead of [`gpr`](https://github.com/jcansdale/gpr)
91+
+ Update Dec 2022: Now that NuGet Packages has supports granular permissions and organization sharing ([GitHub's roadmap item](https://github.com/github/roadmap/issues/589)), [`dotnet nuget push`](https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-nuget-push) should work - but using [`gpr`](https://github.com/jcansdale/gpr) for mapping convenience
11192
- [`gpr`](https://github.com/jcansdale/gpr) still might be preferred since you would have to tie the NuGet package to the repository manually post-migration
112-
- For [`dotnet nuget push`](https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-nuget-push) to work, you will have to add the feed first using this command:
93+
- If attempting to use [`dotnet nuget push`](https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-nuget-push), you will have to add the feed first using this command:
11394
```bash
11495
dotnet nuget add source \
11596
--username my-github-username \
@@ -122,15 +103,15 @@ An example of this in practice:
122103
- Also, in the script, I had to delete `_rels/.rels`{: .filepath} and `[Content_Types].xml`{: .filepath} because there was somehow two copies of each file in the package and it causes gpr to fail when extracting/zipping the package to re-push
123104
- To clean up the working directory when done, run this one-liner:
124105
```bash
125-
rm *.nupkg *.zip
106+
rm -rf ./temp
126107
```
127108
{: .nolineno}
128109

129110
## Improvement Ideas
130111

131-
* [ ] Add a source folder input instead of relying on current directory
132-
* [ ] Map between repositories where the target repo is named differently than the source repo
133-
* [ ] Dynamically determine out where [`gpr`](https://github.com/jcansdale/gpr) is installed instead of passing in a parameter (right now we are passing the `gpr` path in as a parameter explicitly because sometimes `gpr` is aliased to `git pull --rebase`)
112+
* [x] Add a source folder input instead of relying on current directory (just using `./temp`{: .filepath})
113+
* [ ] 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)
114+
* [x] Dynamically determine out where [`gpr`](https://github.com/jcansdale/gpr) is installed instead of passing in a parameter (right now we are passing the `gpr` path in as a parameter explicitly because sometimes `gpr` is aliased to `git pull --rebase`) (installing `gpr` locally to the `./temp/tools`{: .filepath} directory)
134115
* [x] Update script because of GitHub Packages GraphQL [deprecation](https://github.blog/changelog/2022-08-18-deprecation-notice-graphql-for-packages/)
135116

136117
## Summary

0 commit comments

Comments
 (0)