GitHub actions fails publishing to Azure - asp.net

I'm trying to build and deploy ASP.NET Web application using GitHub actions and Azure.
I've successfully created and published to Azure a small app (.NET 4.8) using VS2017.
Now I'm trying to setup automatic deployment and I'm using the basic GitHub actions workflow that you get from Azure when setting up deployment.
When running the action I get into this error:
D:\a\pathtomyproject\myprojectname.csproj : error MSB4057: The target "pipelinePreDeployCopyAllFilesToOneFolder" does not exist in the project.
Here's the full action:
name: Build and deploy ASP app to Azure Web App
on:
push:
branches:
- develop
workflow_dispatch:
jobs:
build-and-deploy:
runs-on: 'windows-latest'
steps:
- uses: actions/checkout#master
- name: Setup MSBuild path
uses: microsoft/setup-msbuild#v1.0.2
- name: Setup NuGet
uses: NuGet/setup-nuget#v1.0.5
- name: Restore NuGet packages
run: nuget restore
- name: Publish to folder
run: msbuild /nologo /verbosity:m /t:Build /t:pipelinePreDeployCopyAllFilesToOneFolder /p:_PackageTempDir="\published\"
- name: Deploy to Azure Web App
uses: azure/webapps-deploy#v2
with:
app-name: 'myname'
slot-name: 'production'
publish-profile: ${{ ... }}
package: \published\
Specifically, the error occurs during "Publish to folder" step.

Found the solution to my problem. Since I was using multi project solution, I had to modify msbuild command and specify to build my startup project. Now my publish to folder step looks something like this:
- name: Publish to folder
run: msbuild src\myproj\myproj.csproj ...

Related

Github Actions: Build NextJS Project in Different Directory

I'm wondering how I would go about building my project in a sibling directory to the existing repo. The reason for this is that I will occasionally get errors when builds fail and leave something messed up in the build directory. It seems like it would be far more ideal to build in a separate directory, then if the build succeeds, move all files to the actual running directory and restart PM2. I know I can use working-directory in the .yml, but I'm not sure how exactly to write this to ensure that there won't be any downtime. If this isn't a good solution and anyone has any other ideas, I am open to them. Here's my current deploy file:
name: Staging Deploy
on:
push:
branches: [ "main" ]
jobs:
build:
runs-on: Staging
environment: Staging
strategy:
matrix:
node-version: [16.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
steps:
- uses: actions/checkout#v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node#v3
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm install
- name: Create env file
run: |
touch .env
...create env file with repo secrets
- run: npm run build --if-present
- name: Restart PM2
id: restart_pm2
run: pm2 reload reponame
continue-on-error: true
- name: Start PM2
if: steps.restart_pm2.outcome != 'success'
run: pm2 start pm2.config.js```
Modify the pm2 reload step to reload the app from the running directory. You can do this by specifying the path to the running directory as the working directory for the reload step. For example:
name: Restart PM2
id: restart_pm2
run: pm2 reload reponame
working-directory: .
continue-on-error: true
By following these steps, you should be able to build your project in a separate directory and move the built files to the running directory without any downtime. It's a good idea to test this process on a staging environment before deploying to production to ensure that everything is working as expected.

Strange behavior restore package from local feed in azure devops pipeline

I've a strange problem. I try to restore a .net core application with dotnet restore command in Azure pipeline. One of my package is on my local feed (repo and feed are in same azure devops project).
With classic editor (UI editor) I've no problem but with yaml file restore is unable. I've the error : error NU1301: Unable to load the service index for source
I export yaml from classic editor and copy it in a new yaml pipeline file but build faild also... The configuration of restore task is the same but it doesn't work with yaml.
What is stranger is that the restore task worked friday (with yaml) but fail since yesterday without changes...
I'don't undersand anything... Who would have an idea ?
Thanks
(The agent is an azure agent)
The content of yaml file :
jobs:
- job: Job_1
displayName: Agent job 1
pool:
vmImage: windows-2019
steps:
- checkout: self
fetchDepth: 1
- task: DotNetCoreCLI#2
displayName: "dotnet restore"
inputs:
command: restore
projects: '**\*.sln'
vstsFeed: "afcdef6a-5c72-4a0e-90c5-33d9e751869c/ab1da1d1-3562-4a0a-9781-4f4d80de93ba"
For Classic pipeline, your Build job authorization scope may be Project Scope(Current project)
Classic Pipeline Options
When you use yaml pipeline, the Build job authorization scope is defined by
Project setting->Limit job authorization scope to current project for non-release pipelines
Project Settings
If it is off, the build job authorization scope is Organization Scope (Project Collection)
If it is on, the build job authorization scope is Project Scope (Current project)
You could try to add Project Collection Build Service (OrgName) to Feed setting->Permissions.
Feed settings

Azure App Service deployment Github Actions: Error: Deployment Failed with Error: Error: No package found with specified pattern

I have a dotnet core app I am trying to deploy to Azure with GitHub actions CI/CD. However, the app is not on root repository but rather inside of another folder MYAPP. So I had to update working-directory of build and publish jobs but now I keep getting this error:
Run azure/webapps-deploy#v2
Error: Deployment Failed with Error: Error: No package found with specified pattern: ./published
My project structure is as such
├── .github/workflows
├── MYAPP
├───── MYAPP.sln
├───── MYAPPTESTS
├───── MYAPP
├─────────── MYAPP.csproj
├── docs
├── OtherFolder
└── README.md
My YML file is as below
name: Build and deploy ASP.Net Core app to Azure Web App - myappdeploy
on:
push:
branches:
- main
workflow_dispatch:
env:
AZURE_WEBAPP_PACKAGE_PATH: './published'
jobs:
build:
runs-on: windows-latest
steps:
- uses: actions/checkout#v2
- name: Set up .NET Core
uses: actions/setup-dotnet#v1
with:
dotnet-version: '6.0.x'
include-prerelease: true
- name: dotnet build and publish
working-directory: ./MYAPP
run: |
dotnet restore
dotnet build --configuration Release
dotnet publish -c Release -o ${{ env.AZURE_WEBAPP_PACKAGE_PATH }}
- name: 'Run Azure webapp deploy action using publish profile credentials'
uses: azure/webapps-deploy#v2
with:
app-name: 'myappdeploy'
publish-profile: ${{ secrets.AZUREAPPSERVICE_PUBLISHPROFILE_7894EB5E4C174F72A5EDE9C64B658907 }}
package: ${{ env.AZURE_WEBAPP_PACKAGE_PATH }}
What am I missing?
Thanks
Instead of working directory I have tried adding .deployment to root repository.
[config]
project = ./MYAPP
However this file was not read and the pipeline would say that it can't find .sln file. I have also tried adding Project app setting on Azure App Service configuration but still the pipeline would not work.
I have tried removing the package parameter of the azure/webapps-deploy#v2 and the pipeline runs successfully however I instead get the error when accessing the website:
You do not have permission to view this directory or page
Also, none of the routes of my app can be found (404)
When running the solution locally there are no issues.. and when deploying manually to an app service through Visual Studio GUI there are also no issues. However, this is not a proper solution as GitHub CI/CD is needed.
For anyone who might come into the same issue, I had to specify where the .sln is during dotnet build and where the .csproj is for dotnet publish. Also remove working-directory
- name: dotnet build and publish
# working-directory: ./MYAPP
run: |
dotnet restore
dotnet build MYAPP/MYAPP.sln --configuration Release
dotnet publish MYAPP/MYAPP/MYAPP.csproj -c Release -o ${{ env.AZURE_WEBAPP_PACKAGE_PATH }}

Test stage failed in GitLab CI for .NET Core projects

I'm facing trouble with my GitLab CI. The expected workflow for my Test stage is :
fetching changes
checking out
downloading artifacts
run dotnet test
uploading artifacts
The current workflow is :
fetching changes
checking out (removing all bin/obj folder)
uploading artifacts -> this line failed due to inexistance of my test-results.xml file
My solution looks like this:
-- yml file
-- MyApp
-- MyApp.Tests
The version of my gitlab-runner is 12.4.1 and I'm using the shell executor on Windows environment.
Please find below my yml file:
image: mcr.microsoft.com/dotnet/core/sdk:3.1
variables:
EXE_RELEASE_FOLDER: 'Oncolin.UI.Desktop\bin\Release'
# the folder inside the deploy folder
DEPLOY_FOLDER_APP: 'Oncolin.UI.Desktop\Builds'
CONFIGURATION: 'release'
#NUGET_PATH: 'C:\NuGet\nuget.exe'
#MSBUILD_PATH: 'C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin\MSBuild.exe'
#MSTEST_PATH: 'C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\MSTest.exe'
NUNIT_PATH: '.\packages\NUnit.ConsoleRunner.3.9.0\tools\nunit3-console.exe'
SOLUTION: 'Oncolin.sln'
stages:
- build
- test
build:
stage: build
tags:
- windows
script:
- '& nuget restore' # restore Nuget dependencies
- '& dotnet restore'
- '& SonarScanner.MSBuild.exe begin /k:Oncolin /d:sonar.host.url=https://docker:10500 '
- '& dotnet build -c release'
- '& "$env:MSBUILD_PATH" "$env:SOLUTION" /t:Build /p:Configuration=Release' # build the project
- '& SonarScanner.MSBuild.exe end'
artifacts:
expire_in: 1 week # save gitlab server space, we copy the files we need to deploy folder later on
paths:
- '$env:EXE_RELEASE_FOLDER\*' # saving exe to copy to deploy folder
- 'RNC.RULES.QUALITY.TESTS\*' # saving entire Test project so NUnit can run tests
- 'RNC.MODEL.TNM.TESTS\*' # saving entire Test project so NUnit can run tests
- 'Oncolin.Model.Tests\*' # saving entire Test project so NUnit can run tests
test:
stage: test
tags:
- windows
allow_failure: false
script:
- '& dotnet test --test-adapter-path:. --logger:"junit;LogFilePath=$CI_PROJECT_DIR\artifacts\{assembly}-test-result.xml;MethodFormat=Class;FailureBodyFormat=Verbose"'
artifacts:
expire_in: 1 week # save gitlab server space, we copy the files we need to deploy folder later on
when: always
paths:
- $CI_PROJECT_DIR\artifacts\*test-result.xml
reports:
junit:
- $CI_PROJECT_DIR\artifacts\*test-result.xml
dependencies:
- build
Can someone explain to me why the downloading and dotnet test steps are skipped ? I hope I gave enough informations.
Thanks in advance.
The issue came from the sonar scanner. Even if I used the .Net Core sonar scanner, I had the same issue during my test stage.
I don't know why and I have already lost too many time to investigate on that.
I resolved it by creating a new stage specificly to run the sonar and ran it after the test stage.

How to add working directory to deployment in GitHub actions

I recently moved into GitHub actions, so what I'm trying to do is host my react project in firebase when a push is done. And I used GitHub actions to this CI/CD process. And this is the main.yml that I have right now.
name: Build and Deploy
on:
push:
branches:
- master
jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
- name: Checkout Repo
uses: actions/checkout#master
- name: Install Dependencies
working-directory: ./my-app
run: npm install
- name: Build
working-directory: ./my-app
run: npm run build
deploy:
name: Deploy
needs: build
runs-on: ubuntu-latest
steps:
- name: Checkout Repo
uses: actions/checkout#master
- name: Deploy to Firebase
uses: w9jds/firebase-action#master
with:
args: deploy --only hosting
env:
FIREBASE_TOKEN: ${{ secrets.FIREBASE_TOKEN }}
And I somehow manage to set the working directory when npm installation and project building. But in deployment I'm keep getting this error,
So what I have understood is, this error occurs due to the working directory problem. So my current project structure looks like this.
. (root of my GitHub repository)
└── my-app
├── firebase.json <-- Git Hub action must point to this sub-dir
└── my-app-mobile
├── packages.json
So how should I do this within my firebase deployment process? If I'm wrong with the problem, What would be the problem and the answer? It seems I can't use working-directory: ./my-app with uses:
I looked at the documentation for the firebase CLI and didn't see any way to set the path to your firebase.json via a CLI parameter. There is, however, an environment variable that stores the root directory. It's in the context of predeploy and postdeploy hooks though, so I'm not sure if the CLI will respect it.
$PROJECT_DIR — The root directory containing firebase.json
https://firebase.google.com/docs/cli#environment_variables
The w9jds/firebase-action you are using is just a wrapper around the CLI. I'm not sure if this will work but you could try setting the project directory as follows. The reason the variable is set in a separate step is because you cannot evaluate expressions in env sections. See this answer for more detail. Container actions like w9jds/firebase-action will have access to the variable without passing it directly via env.
- name: Set project dir environment var
run: echo ::set-env name=PROJECT_DIR::"$GITHUB_WORKSPACE/my-app"
- name: Deploy to Firebase
uses: w9jds/firebase-action#master
with:
args: deploy --only hosting
env:
FIREBASE_TOKEN: ${{ secrets.FIREBASE_TOKEN }}
If that doesn't work, an alternative is to fork w9jds/firebase-action and add a PROJECT_PATH parameter to the entrypoint.sh script here:
https://github.com/w9jds/firebase-action/blob/master/entrypoint.sh
Update: I raised a PR to add a PROJECT_PATH parameter to w9jds/firebase-action. You can now use the action as follows.
- name: Deploy to Firebase
uses: w9jds/firebase-action#master
with:
args: deploy --only hosting
env:
FIREBASE_TOKEN: ${{ secrets.FIREBASE_TOKEN }}
PROJECT_PATH: ./my-app

Resources