How To Run GitHub Actions - .NET Framework Unit Test - github

I have a Sample Solution with a simple .NET Framework 4.8 Library Project.
this Solution has also a Unit Test Project for this Library. This Test Project has a Test which will succeed and one Test which will fail.
Now i want to upload this to github and it should Run the Test Project. But i cant figure out, how i can run the Test Project. All Tutorials are for .NET Core 5+
my actual workflow file looks like this:
name: .NET Framework Desktop
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
build:
strategy:
matrix:
configuration: [Release]
runs-on: self-hosted # For a list of available runner types, refer to
# https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idruns-on
env:
Solution_Name: SelfHostedPDMTest.sln # Replace with your solution name, i.e. MyWpfApp.sln.
Test_Project_Path: TestProjectTest.csproj # Replace with the path to your test project, i.e. MyWpfApp.Tests\MyWpfApp.Tests.csproj.
Wap_Project_Directory: your-wap-project-directory-name # Replace with the Wap project directory relative to the solution, i.e. MyWpfApp.Package.
Wap_Project_Path: your-wap-project-path # Replace with the path to your Wap project, i.e. MyWpf.App.Package\MyWpfApp.Package.wapproj.
steps:
- name: Checkout
uses: actions/checkout#v3
with:
fetch-depth: 0
# Install the .NET Core workload
- name: Install .NET
uses: actions/setup-dotnet#v2
with:
dotnet-version: 5.0.x
# Add MSBuild to the PATH: https://github.com/microsoft/setup-msbuild
- name: Setup MSBuild.exe
uses: microsoft/setup-msbuild#v1.0.2
# Execute all unit tests in the solution
- name: Execute unit tests
run: dotnet test
# Restore the application to populate the obj folder with RuntimeIdentifiers
- name: Restore the application
run: msbuild $env:Solution_Name /t:Restore /p:Configuration=$env:Configuration
env:
Configuration: ${{ matrix.configuration }}
Appx_Bundle_Platforms: x86|x64
Appx_Package_Build_Mode: StoreUpload
This is a sample Workflow from github.
i know the selected .net version is 5.0.x but 4.8.x is not possible.
and also dotnet test would run a .NET Core Test and not a .NET Framework Test.
Maybe someone has a good workflow file or can help me to start?

Try the below as documented
- name: Run vstests
uses: microsoft/vstest-action#v1.0.0
with:
testAssembly: TestProject.dll
searchFolder: .\TestProject\bin\Debug\
runInParallel: true

Related

Swift Package Github Action CI [duplicate]

I have swift package for iOS apps (it needs UIKit to run). I wan't to build this package using Github action this is how my workflow looks like
name: Swift
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
build:
runs-on: macos-latest
steps:
- uses: actions/checkout#v2
- uses: YOCKOW/Action-setup-swift#master
with:
swift-version: '5.3'
- name: Build
run: swift build -v
- name: Run tests
run: swift test -v
When I run this build I got error: no such module 'UIKit'.
So I have 2 options:
make my package UIKit independent (actually don't know how)
configure build to run on iOS specifically
What should I do?
We're using xcodebuild instead of swift build to do platform specific CI from GitHub Actions. See specific example at https://github.com/firebase/firebase-ios-sdk/blob/master/.github/workflows/spm.yml and https://github.com/firebase/firebase-ios-sdk/blob/master/scripts/build.sh#L540
The simplest approach as of 2022 is to use xcodebuild like stated above.
Condensed to the basics, the script can be as simple as this:
# This workflow will build a Swift project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-swift
name: Build and Test
on:
pull_request:
branches: [ "main" ]
jobs:
build:
runs-on: macos-latest
steps:
- uses: actions/checkout#v3
# Sanity check to make sure we use a valid device in the next step
- name: List available devices
run: xcrun simctl list devicetypes
#generic/platform=iOS is sufficient for just building, - but need to specify concrete destination for tests
- name: Build and run tests
run: xcodebuild test -scheme YourSchemeNameHere -destination 'platform=iOS Simulator,name=iPhone 13 Pro' #or any other name from the list of sim devices
This is enough to build the project and run tests on it.
In some cases you may need to install a later version of Swift than what's available by default on Github Actions, - you can use a third party step for that and use xcode-select.

Working directory is not working in my Github action

I'm starting to use Github actions. When I ran the process in my Ubuntu server, one of the config options was to select the work folder, by default, I leave _work. But previously, I had my repository in another folder.
So, I'm trying to add a specific value for working-directory to my yml, file but is not using the value in the build process.
YML File:
# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
name: Node.js CI
on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]
defaults:
run:
working-directory: /../../../../var/www/mysite.com
jobs:
deployment:
runs-on: self-hosted
steps:
- name: Checkout
uses: actions/checkout#v3
- name: Use Node.js
uses: actions/setup-node#v3
with:
node-version: '14.x'
- name: Install dependencies
run: yarn
- name: Build
run: yarn build
- name: Restart server application
run: pm2 restart pm2_script.json
The process ran ok, but is doing all the process (checkout, build, etc) inside _work and not inside working-directory
What I'm doing wrong?
Thanks!!

Github actions/upload-artifact#v2 breaks with windows-latest

I have the following GitHub actions workflow for a C# (.net 6) project that works fine with ubuntu-latest. But for some reason we need it to use windows-latest, it breaks with the error (the error after yaml workflow).
Yaml workflow:
jobs:
build:
name: Create Release
runs-on: windows-latest
steps:
- name: Setup Checkout
uses: actions/checkout#v2
- name: Setup .NET
uses: actions/setup-dotnet#v1
with:
dotnet-version: 6.0.x
- name: Restore .NET dependencies
run: dotnet restore
- name: Build .NET
run: dotnet build --no-restore
- name: Test .NET
run: dotnet test --no-build --verbosity normal
- name: Publish .NET
run: |
dotnet publish /p:PublishProfile=Release-win-x86 -c Release
dotnet publish /p:PublishProfile=Release-win-x64 -c Release
- name: Upload Published Artifact
uses: actions/upload-artifact#v2
with:
name: softwarename
path: |
/home/runner/work/solution/project/Software/win-x86/
/home/runner/work/solution/project/Software/win-x64/
The error:
Upload Published Artifact
Run actions/upload-artifact#v2
Warning: No files were found with the provided path:
/home/runner/work/solution/project/Software/win-x86/
/home/runner/work/solution/project/Software/win-x64/. No artifacts will be uploaded.
Download Published Artifact
Run actions/download-artifact#v2
Starting download for softwarename
Error: Unable to find any artifacts for the associated workflow
I read here that I need to change actions/upload-artifact#v2 to actions/upload-artifact#v2.2.4, that I tried and failed with the same error.
Any Idea how to fix this issue?
I give credit for #jessehouwing for making me a ware of the concept.
Regarding this thread, my solution needed ${{ github.workspace }}, so that my changes looks like this:
${{ github.workspace }}\Software\win-x86
${{ github.workspace }}\Software\win-x64
Don't rely on:
/home/runner/work/solution/project
Instead use
${{ github.workspace }}/project
That way it points to the correct path for the runner independent of the operating system and configuration

How can I prevent my unit test project from being deployed to my Azure Web App along with my ASP.NET Core website project?

I have an ASP .NET Core website whose source code is stored in GitHub. This is automatically deployed to an Azure App Service (running on Linux) when I do a check-in to GitHub. (I set this up a while back using the Azure "wizard", but now I can't find any way to tinker with it).
Recently I added a unit test project to my solution, and when I checked that in the unit test project was deployed along with the web project - which I don't want. (Especially since the runtime host gets confused about which DLL it should run, and decides to run neither - so the website does not start!).
Is there a way to prevent the unit test project from being built or deployed? (I'm happy just to run these tests locally). I'm not even sure where to start - would I do this in GitHub, or in the Azure portal? I can't find any likely-looking knobs or levers either in GitHub or in the Azure portal.
Update: I've found a file that has been created in my repo in a .github/workflows folder. Here's the content:
name: Build and deploy ASP.Net Core app to Azure Web App - MyApp
on:
push:
branches:
- master
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- name: Set up .NET Core
uses: actions/setup-dotnet#v1
with:
dotnet-version: '5.0.x'
include-prerelease: true
- name: Build with dotnet
run: dotnet build --configuration Release
- name: dotnet publish
run: dotnet publish -c Release -o ${{env.DOTNET_ROOT}}/myapp
- name: Upload artifact for deployment job
uses: actions/upload-artifact#v2
with:
name: .net-app
path: ${{env.DOTNET_ROOT}}/myapp
deploy:
runs-on: ubuntu-latest
needs: build
environment:
name: 'Production'
url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}
steps:
- name: Download artifact from build job
uses: actions/download-artifact#v2
with:
name: .net-app
- name: Deploy to Azure Web App
id: deploy-to-webapp
uses: azure/webapps-deploy#v2
with:
app-name: 'My-App'
slot-name: 'Production'
publish-profile: ${{ secrets.AZUREAPPSERVICE_PUBLISHPROFILE_0C864EC866B247EABE271A962BE84378 }}
package: .
While that's progress, I still can't see where I could modify it to prevent it from including the unit test project, because it doesn't actually reference anything directly.
Here's what worked for me:
I changed:
run: dotnet build --configuration Release
...to:
run: dotnet build MyProject --configuration Release
...where MyProject is the name of the folder containing the website project. (dotnet build is run from the solution folder, and the default behavior is that it will build all projects in the solution; specifying a sub-folder name makes it build only the project(s) it finds in that folder - see the documentation here).
Similarly, I changed:
run: dotnet publish -c Release -o ${{env.DOTNET_ROOT}}/myapp
to:
run: dotnet publish MyProject -c Release -o ${{env.DOTNET_ROOT}}/myapp
Thanks to #LexLi for pointing me in the right direction.

Ignore projects in GitHub workflow

I have a solution with 5 projects all using dotnet5 except one (AZ functions) which uses dotnet3.1. My build action fails with The framework 'Microsoft.NETCore.App', version '3.1.0' was not found..
Is it possible to:
Set which project to build, or...
Have multiple solutions and select the solution to build, or...
Use multiple frameworks
...or anything else to make it work...
Turns out you can have multiple build frameworks :) :
jobs:
build:
runs-on: windows-latest
steps:
- uses: actions/checkout#v2
- name: Set up .NET Core
uses: actions/setup-dotnet#v1
with:
dotnet-version: '5.0.x'
- name: Setup .NET Core 3.1
uses: actions/setup-dotnet#v1
with:
dotnet-version: 3.1.x