I need some help regarding publishing the ASP.NET Core Web APIs to the gitlab docker production environment. I have inherited the code from another team, and the current APIs are working fine on production, even if I make changes on them and re-publish it again.
However, when I add another controller, it works well in the local environment, but fails to load in the production environment.
I have set the following in launchsettings.json
:
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Production"
}
I added a default out-of-the-box controller and it works locally, but I get an http 404 error in the production environment.
using Microsoft.AspNetCore.Mvc;
// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
namespace testweb.default
{
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
// GET: api/<ValuesController>
[HttpGet]
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// GET api/<ValuesController>/5
[HttpGet("{id}")]
public string Get(int id)
{
return "value";
}
// POST api/<ValuesController>
[HttpPost]
public void Post([FromBody] string value)
{
}
// PUT api/<ValuesController>/5
[HttpPut("{id}")]
public void Put(int id, [FromBody] string value)
{
}
// DELETE api/<ValuesController>/5
[HttpDelete("{id}")]
public void Delete(int id)
{
}
}
}
Here is my .gitlab-ci.yml
file
variables:
DOCKER_IMAGE_PRODUCTION : $CI_REGISTRY_IMAGE
DOCKER_IMAGE_TEST : $CI_REGISTRY_IMAGE
DOCKER_IMAGE_DEV : $CI_REGISTRY_IMAGE
stages :
- push_registry_dev
- update_manifests_dev
- push_registry_test
- update_manifests_test
- push_registry_uat
- update_manifests_uat
build_image_dev:
stage: push_registry_dev
image : mcr.microsoft.com/dotnet/sdk:8.0
services:
- docker:24.0.6-dind
variables:
DOCKER_HOST: tcp://docker:2375/
DOCKER_DRIVER: overlay2
DOCKER_TLS_CERTDIR: ""
before_script:
- cat /etc/os-release
- curl -sSL https://get.docker.com/ | sh
- echo -n $CI_JOB_TOKEN | docker login -u gitlab-ci-token --password-stdin $CI_REGISTRY
- export PATH="$PATH:/root/.dotnet/tools"
- dotnet tool install --global dotnet-ef
script:
# - cat .env.development > .env
# - docker build -f dev.Dockerfile --tag $DOCKER_IMAGE_TEST:$CI_COMMIT_SHORT_SHA-dev .
#- dotnet ef database update --project TestProject.Infrastructure/ --startup-project TestProject.Web/ --connection "Server=***.***.***.***;Initial Catalog=db;User Id=sa;Password=*******;TrustServerCertificate=true"
- cd ./TestProject.Web
- dotnet publish --os linux --arch x64 -p:PublishProfile=DefaultContainer -p:ContainerImageName=test-backend
- docker tag test-backend $DOCKER_IMAGE_TEST:$CI_COMMIT_SHORT_SHA-dev
- docker push $DOCKER_IMAGE_TEST:$CI_COMMIT_SHORT_SHA-dev
only:
- develop
- main
- prod
tags :
- gitlab-org-docker
update_manifest_dev:
stage: update_manifests_dev
variables:
GIT_STRATEGY: none
retry: 2
image: docker:24.0.6
services:
- docker:24.0.6-dind
script:
# Add SSH key to root
- mkdir -p /root/.ssh
- cat "${GIT_SSH}" > /root/.ssh/id_rsa
- ssh-keyscan -H gitlab.com > /root/.ssh/known_hosts
- chmod 400 /root/.ssh/id_rsa
- apk add --no-cache git
- export GIT_SSH_COMMAND="ssh -i ~/.ssh/id_rsa"
- git config --global user.name "username"
- git config --global user.email "[email protected]"
- git clone --single-branch --branch main [email protected]:company/k8s-manifests.git
- cd k8s-manifests/test/dev/api
- export REPO=$DOCKER_IMAGE_DEV:$CI_COMMIT_SHORT_SHA-dev
- chmod -R 777 ${PWD}
- docker run --rm -v ${PWD}:/workdir -e REPO=$REPO mikefarah/yq:4.12.2 e '.spec.template.spec.containers[0].image=env(REPO)' --inplace deployment.yml
- git commit -am "New version updated ${CI_COMMIT_SHORT_SHA}-dev" && git push origin main
tags:
- gitlab-org-docker
needs: ["build_image_dev"]
only:
refs:
- develop
- main
- prod
Is there anything else I need to change in the code? I am not that conversant with docker technologies.
2