dotnet

How to Solve “error, cannot create resource in API group” when Deploying Application with Jenkins and OpenShift

Had this error when im using Jenkins and integrate it to Openshift Container Platform

--> Creating resources with label build=hello-world-dot-net-core ...
    error: imagestreams.image.openshift.io is forbidden: User "system:serviceaccount:cicd:default" cannot create resource "imagestreams" in API group "image.openshift.io" in the namespace "cicd"
    error: buildconfigs.build.openshift.io is forbidden: User "system:serviceaccount:cicd:default" cannot create resource "buildconfigs" in API group "build.openshift.io" in the namespace "cicd"
--> Failed

How to solve this issue is actually quite simple, running this below command can solve it directly.

$ oc policy add-role-to-user admin system:serviceaccount:cicd:default -n cicd

Error While Running dotnet test

Had this error while running automated test on my dotnet core application thru Bitbucket Pipeline

Starting test execution, please wait...
A total of 1 test files matched the specified pattern.

Passed!  - Failed:     0, Passed:     1, Skipped:     0, Total:     1, Duration: < 1 ms - micro-service-test.dll (net8.0)
Testhost process for source(s) '/opt/atlassian/pipelines/agent/build/micro-service/bin/Debug/net8.0/micro-service.dll' exited with error: Error:
  An assembly specified in the application dependencies manifest (testhost.deps.json) was not found:
    package: 'Microsoft.TestPlatform.CommunicationUtilities', version: '17.11.1-release-24455-02'
    path: 'Microsoft.TestPlatform.CommunicationUtilities.dll'
. Please check the diagnostic logs for more information.

Apperently this happen because of my main project is detected as test folder. We can exclude it by adding this configuration in .csproj file

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <IsTestProject>false</IsTestProject>
  </PropertyGroup>
</Project>

Deploying a Dot Net Core Apps into Openshift 4

The goal of this article is to display a simple hello-world apps build on top of a .net core 7 that can be use to test a deployment to Openshift 4 platform. And we can start it by using a git clone command

$ git clone https://github.com/edwin/hello-world-dot-net-core

Go to the corresponding folder,

$ cd hello-world-dot-net-core

Create a namespace for this app,

$ oc new-project dot-net-ns

And run this command within the sourcecode folder,

$ oc new-app dotnet:7.0-ubi8~.

It will generate logs like this,

warning: Cannot check if git requires authentication.
--> Found image 4466483 (2 months old) in image stream "openshift/dotnet" under tag "7.0-ubi8" for "dotnet:7.0-ubi8"

    .NET 7
    ------
    Platform for building and running .NET 7 applications

    Tags: builder, .net, dotnet, dotnetcore, dotnet-70

    * A source build using source code from https://github.com/edwin/hello-world-dot-net-core#master will be created
      * The resulting image will be pushed to image stream tag "hello-world-dot-net-core:latest"
      * Use 'oc start-build' to trigger a new build

--> Creating resources ...
    imagestream.image.openshift.io "hello-world-dot-net-core" created
    buildconfig.build.openshift.io "hello-world-dot-net-core" created
    deployment.apps "hello-world-dot-net-core" created
    service "hello-world-dot-net-core" created
--> Success
    Build scheduled, use 'oc logs -f buildconfig/hello-world-dot-net-core' to track its progress.
    Application is not exposed. You can expose services to the outside world by executing one or more of the commands below:
     'oc expose service/hello-world-dot-net-core'
    Run 'oc status' to view your app.

And finally we can create a route for this service,

$ oc create route edge --service=hello-world-dot-net-core

We can try to do some changes on Index.cshtml file,

@{
    ViewData["Title"] = "Home Page";
}

<div class="text-center">
    <h1 class="display-4">Welcome</h1>
    <p>Learn about <a href="https://redhat.com">Red Hat loves dotnet</a>.</p>
</div>

Save and redeploy it by running below command in the root sourcecode folder,

$ oc start-build hello-world-dot-net-core --from-dir=.

The result would be something like this,

Lets do some more changes, and deploy it to Openshift

@{
    ViewData["Title"] = "Home Page";
}

<div class="text-center">
    <h1 class="display-4">Welcome</h1>
    <p>Learn about <a href="https://redhat.com">Red Hat loves alot of programming language but we loves dotnet more</a>.</p>
</div>

And we instantly can see changes within the web page,

Code can be seen here,

https://github.com/edwin/hello-world-dot-net-core