Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
alsi-lawr committed Aug 31, 2024
0 parents commit 9cfe4b4
Show file tree
Hide file tree
Showing 9 changed files with 204 additions and 0 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/test-action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: 'build-test'

on:
pull_request:
push:
branches:
- master

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Deploy to NuGet
uses: ./
with:
deploy-nuget-project: 'test'
version: '1.0.0'
nuget-api-key: 'GARBAGE'
image-version: '8.0'
build-only: true

2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
**/bin/**
**/obj/**
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2024 GitHub, Inc. and contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
54 changes: 54 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Deploy to NuGet

This reusable action builds a .NET project and deploys the resulting package to NuGet. It supports configuration for custom project paths and versioning, and securely handles the NuGet API key as a secret.

## Usage

Simply use this action in your workflow by specifying the project file path, version, and your NuGet API key.

### Inputs

- `deploy-nuget-project`: **Required**. The path to the `.csproj` file of the .NET project that you want to build and deploy.
- `version`: **Required**. The version to assign to the NuGet package. Typically, this is dynamically generated from tags or other versioning strategies.
- `nuget-api-key`: **Required**. The NuGet API key used to push the package to NuGet.org. This should be stored securely as a GitHub Secret.
- `dotnet-version`: **Optional**. The .NET SDK version to use. Defaults to `8.0`.

### Example

```yaml
name: Deploy

on:
push:
branches:
- main

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Deploy to NuGet
uses: alsi-lawr/deploy-nuget@v1
with:
deploy-nuget-project: 'src/MyProject' # Specify the correct path to your project file directory
version: 1.0.${{ github.run_number }} # Example using the run number
nuget-api-key: ${{ secrets.NUGET_API_KEY }} # NuGet API key stored as a secret
dotnet-version: '8.0'
```
### Security
- **NuGet API Key:** The `nuget-api-key` input should always be stored as a secret to avoid exposing sensitive information. You can add this secret in your GitHub repository settings under `Settings` > `Secrets and variables` > `Actions`.

### Additional Notes

- This action leverages Docker to run inside a .NET SDK container, ensuring a consistent build environment.
- The action automatically restores NuGet packages, adds `Microsoft.SourceLink.GitHub`, and builds the project with various settings optimized for CI/CD environments.

## Contributing

Contributions are welcome! Please refer to the [contributing guidelines](./docs/CONTRIBUTING.md) for more information.

43 changes: 43 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: 'Deploy to NuGet'

description: 'Reusable action to build and deploy a .NET project to NuGet the @v tag of the action is the dotnet sdk version.'

inputs:
deploy-nuget-project:
description: 'The path to the .NET project to deploy'
required: true
version:
description: 'The version to assign to the NuGet package'
required: true
nuget-api-key:
description: 'Your NuGet API key'
required: true
secret: true
dotnet-version:
description: 'The .NET SDK version to use'
default: '8.0'
options:
- '8.0'
- '7.0'
- '6.0'
build-only:
description: 'Only build the project, do not deploy'
default: false
options:
- true
- false

runs:
using: 'composite'
steps:
- name: Deploy to NuGet in Docker
run: |
docker run --rm \
-v ${{ github.workspace }}:/workspace \
-e NUGET_API_KEY=${{ inputs.nuget-api-key }} \
-e GITHUB_REF_NAME=${{ github.ref_name }} \
-e GITHUB_SHA=${{ github.sha }} \
-w /workspace \
mcr.microsoft.com/dotnet/sdk:${{ inputs.dotnet-version }} \
bash -c "./src/deploy.sh ${{ inputs.deploy-nuget-project }} ${{ inputs.version }} ${{ inputs.build-only }}"
8 changes: 8 additions & 0 deletions docs/CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# How to Contribute

1. **Modify the Action**: Make changes to the `action.yml` or related scripts (if any exist).

2. **Test Your Changes**: Ensure your changes work by testing in a separate repository or locally using `act`.

3. **Submit Your Changes**: Once you're confident your changes work as expected, submit a pull request with a brief description of the improvements or fixes.

34 changes: 34 additions & 0 deletions src/deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/bash

PROJECT_PATH=$1
PACKAGE_VERSION=$2
BUILD_ONLY=$3
echo "Deploying package in $PROJECT_PATH with version $PACKAGE_VERSION"
echo "Dotnet version used: "
dotnet --version

dotnet restore -s "https://api.nuget.org/v3/index.json" $PROJECT_PATH
dotnet add $PROJECT_PATH package Microsoft.SourceLink.GitHub
dotnet build $PROJECT_PATH -c Release \
/p:ContinuousIntegrationBuild=true \
/p:EmbedUntrackedSources=true \
/p:PublishRepositoryUrl=true \
/p:GenerateDocumentationFile=true \
/p:IncludeSymbols=true \
/p:SymbolPackageFormat=snupkg \
/p:NoWarn=CS1591 \
/p:RepositoryBranch=$GITHUB_REF_NAME \
/p:RepositoryCommit=$GITHUB_SHA \
/p:GeneratePackageOnBuild=true \
/p:Version=$PACKAGE_VERSION \
/p:BaseOutputPath=bin/ \
--no-incremental

if [ "$BUILD_ONLY" = "true" ]; then
exit 0
fi

dotnet nuget push "$PROJECT_PATH/bin/Release/*.nupkg"\
--source https://api.nuget.org/v3/index.json\
--api-key $NUGET_API_KEY

9 changes: 9 additions & 0 deletions test/Class1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Test.Package;

public class Class1
{
public Class1()
{
Console.WriteLine("Hello World!");
}
}
9 changes: 9 additions & 0 deletions test/Test.Package.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>

0 comments on commit 9cfe4b4

Please sign in to comment.