initial commit
Some checks failed
Build, Test & Publish / Build and Publish Container Image (push) Has been cancelled
Build, Test & Publish / Deploy to Infrastructure (push) Has been cancelled
Build, Test & Publish / Build (push) Has been cancelled

This commit is contained in:
2024-09-05 13:54:08 +10:00
commit 8ad5845efc
57 changed files with 6046 additions and 0 deletions

View File

@ -0,0 +1,61 @@
# Build and Publish a Docker Image
This action builds and publishes a Docker image to a container registry. For most of my projects I use DockerHub, but you could use this action and tweak it to use any container registry.
For publish docker images I usually restrict this to the `main` branch, however as this step is often just part of the CI pipeline, you will need some conditional logic to ensure the image is only published on the `main` branch (and not when the build is triggered by a `feature/*` or `fix/*` branch).
## Minimal Pipeline Example
```yaml
name: Build, Test & Publish
on:
push:
branches:
- main
- feature/*
- fix/*
pull_request:
branches:
- main
jobs:
publish:
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
name: Build and Publish Container Image
runs-on: ubuntu-latest
needs:
- build
steps:
- uses: actions/checkout@v3
- name: Setup Docker Metadata
id: meta
uses: docker/metadata-action@v4
with:
images: your-username/your-project
tags: |
type=raw,value=latest
- name: Login to DockerHub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_TOKEN }}
- name: Build and Push Docker Image to DockerHub
uses: docker/build-push-action@v4
with:
file: './path/to/project/Dockerfile'
context: ./path/to/project
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
```
In the above pipeline a few changes will need to be made to suit the project you are working on:
* In the setup docker metadata step, update the `images` to your DockerHub username and the name of your project.
* In the build and push docker image step, update the `file` to the path of your Dockerfile, and the `context` to the path of your project.
* You will also need to add your docker username and docker token to your GitHub repository secrets. The `DOCKER_USERNAME` is your DockerHub username, and the `DOCKER_TOKEN` is a token generated from DockerHub.

View File

@ -0,0 +1,44 @@
# Build and Test .NET Core Applicatons
This action builds and tests .NET Core applications using the `dotnet` CLI.
Building and testing .NET Core applications is a common task in CI/CD pipelines. I often like to ensure this step runs on all commits to the `main` branch, and any `feature/*` or `fix/*` branches.
## Minimal Pipeline Example
```yaml
name: Build and Test .NET Core
on:
push:
branches:
- main
- feature/*
- fix/*
jobs:
build:
name: Build and Test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 8.0.x
- name: Restore dependencies
run: dotnet restore ./path/to/project
- name: Build
run: dotnet build ./path/to/project --no-restore
- name: Test
run: dotnet test ./path/to/project --no-restore --no-build --verbosity normal
```
In the above pipeline only two main changes are required:
* Update the `path/to/project` to the path of your .NET Core project.
* Update the `dotnet-version` to the version of .NET Core you are using.

View File

@ -0,0 +1,5 @@
# Github Actions Snippets and Musings
#### [Build and Test .NET](./build-test-dotnet.md)
#### [Build and Publish Docker Image](./build-publish-container.md)
#### [Run Entity Framework Core Migrations](./run-ef-core-migrations.md)

View File

@ -0,0 +1,39 @@
# Run Entity Framework Core Migrations
Running Entity Framework Core migrations in a GitHub Actions pipeline is a common task. This action demonstrates how to run EF Core migrations in a GitHub Actions pipeline.
## Minimal Pipeline Example
In this example the pipeline will run the EF Core migrations on the `main` branch when a push event occurs. The pipeline runs this as part of a `publish` step, in most cases this will include other steps as well, but these have been omitted for brevity.
```yaml
name: Build, Test & Publish
on:
push:
branches:
- main
- feature/*
- fix/*
pull_request:
branches:
- main
jobs:
deploy:
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
name: Deploy to Infrastructure
runs-on: ubuntu-latest
- name: Run Database Migrations
run: |
dotnet tool install --global dotnet-ef
dotnet tool restore
dotnet ef database update -p .path/to/project/Project.csproj -s ./path/to/project/Project.csproj --connection "$DATABASE_CONNECTION_STRING"
env:
DATABASE_CONNECTION_STRING: ${{ secrets.DATABASE_CONNECTION_STRING }}
```
For this pipeline you will need to ensure that the database connection string is stored as a secret in the GitHub repository.
The above example also assumes that your project and startup project are the same and in the same directory, however you may need to adjust these slightly depending on your project setup.