Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build NuGet packages in CI #9

Merged
merged 18 commits into from
Oct 15, 2024
Merged

Conversation

rmunn
Copy link
Contributor

@rmunn rmunn commented Jul 17, 2024

Build NuGet packages in GitHub Actions CI, upload prerelease packages to GitHub registry and release packages to NuGet.org.

  • Version number derived from tags as follows:
    • Current commit is tagged? Use version number from tag
    • In a PR? Prerelease suffix is -PR###.buildnumber
    • On develop branch? Prerelease suffix is -beta.buildnumber
    • On main branch? Prerelease suffix is -rc.buildnumber

@rmunn rmunn self-assigned this Jul 17, 2024
@rmunn rmunn linked an issue Jul 17, 2024 that may be closed by this pull request
@rmunn
Copy link
Contributor Author

rmunn commented Jul 17, 2024

You'll notice the multi-step process in the GHA workflow for determining the version number. GitVersion has a GitHub Actions workflow step, gittools/actions/gitversion/setup followed by gittools/actions/gitversion/execute, which we could use instead. However, I don't have complete understanding of GitVersion's extensive configurability, so I implemented just the part we'd care about using Bash scripts in the GHA workflow. It might be longer, but you can look at it and see exactly what it's doing, with no "magic".

Also omit the `${{ }}` since it's only *required* when the expression
starts with `!` (which in YAML would mean "non-specific tag").
@rmunn
Copy link
Contributor Author

rmunn commented Jul 17, 2024

Note: for a workflow to actually use a NuGet package published to the GitHub registry, you may need to run a step like the following:

dotnet nuget add source --username USERNAME --password ${{ secrets.GITHUB_TOKEN }} --store-password-in-clear-text --name github "https://nuget.pkg.github.com/sillsdev/index.json"

Where USERNAME, according to GitHub docs, should be "the username to be used when connecting to an authenticated source". It's unclear exactly what that means, but if I've understood it correctly, if the password is the GITHUB_TOKEN then the username is essentially ignored.

I believe the `dotnet nuget push` command wants to interpret the
`*.nuget` glob itself, rather than have the shell expand the glob.
Symbol packages include the .dll alongside the .pdb, so if you don't
need the .pdb then the only downside is a larger-than-needed download.
But since prerelease packages are what you're most likely to be trying
to debug, it's helpful to have the .pdb included.

This also avoids the "Package 'Foo.0.2.1.symbols.nupkg' already
exists" error when the "Foo.0.2.1.nupkg" package was uploaded earlier in
the process.
Just to check that it still works
Now that package write permission has been removed from the default
permissions for the repo, this is now required for packages to upload.
@rmunn rmunn changed the title Use Git tags for version numbers in CI Build NuGet packages in CI Jul 18, 2024
@rmunn rmunn marked this pull request as ready for review July 18, 2024 06:43
@rmunn
Copy link
Contributor Author

rmunn commented Jul 18, 2024

This will end up creating a LOT of packages. We might want to consider whether to use something like https://github.com/actions/delete-package-versions to delete prerelease packages that have had 0 downloads and have a newer version available. Because although the storage limits on the GitHub package registry are generous, if we upload packages for every commit to every PR we're going to hit them sooner or later.

Edit: Basically I want actions/delete-package-versions#12, which is an open feature request but should be doable. Maybe if we use the GitHub registry a lot I'll see about implementing that and sending a PR to that action.

@hahn-kev
Copy link
Collaborator

I think I'm going to scrap the publishing PR builds as it requires the consumers to have a github token on each machine that wants to restore the packages. Basically it's a big pain and there's probably better ways for us to do this. For now let's just have this PR build the nuget packages and upload them as artifacts without actually publishing them to a nuget repo

Too much hassle to consume since it doesn't allow anonymous access, so
anyone wanting to consume these packages, even if the packages are
public, would have to set up NuGet auth and a GitHub API key.
@rmunn
Copy link
Contributor Author

rmunn commented Aug 15, 2024

I think I'm going to scrap the publishing PR builds as it requires the consumers to have a github token on each machine that wants to restore the packages. Basically it's a big pain and there's probably better ways for us to do this. For now let's just have this PR build the nuget packages and upload them as artifacts without actually publishing them to a nuget repo

Okay, commit 8438cd6 comments out the dotnet nuget push steps (but leaves the code in so it's easy to put back later if GitHub improves the DX of consuming NuGet packages from their registry).

Copy link
Collaborator

@hahn-kev hahn-kev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to make this more maintainable by pulling all the inline bash scripting into a file so it can be easily tested locally. I'm find if it writes to an environment variable, but it should also write results to the console.

.github/workflows/nuget-ci-cd.yml Outdated Show resolved Hide resolved
@rmunn rmunn requested a review from hahn-kev September 27, 2024 06:07
Copy link
Collaborator

@hahn-kev hahn-kev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found a bug and left 1 suggestion.

I'd also like to document how this works, otherwise I'm going to forget, you can drop something in the end of the readme under a new development section to explain how the versioning tag works.

src/calculate-version.sh Outdated Show resolved Hide resolved
Whether running in GHA or by hand, the calculated version numbers should
always be echoed to the console. Then we can remove the GHA workflow
step that was doing the same thing.
@rmunn rmunn requested a review from hahn-kev October 15, 2024 03:06
@rmunn rmunn merged commit 28653e4 into main Oct 15, 2024
4 checks passed
@rmunn rmunn deleted the chore/nuget-packages-to-github-registry branch October 15, 2024 03:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Build and publish NuGet packages in GHA workflow
2 participants