In conclusion, the code is compiled using dotnet build, and it is then ready for deployment by incorporating all required runtime dependencies using dotnet publish. In the following situations, you should use dotnet publish rather than dotnet build in your CI/CD workflow:
Getting ready for deployment:
When you need to prepare the application for deployment, use dotnet publish. The code must be compiled and packaged with its dependencies, configuration files, and runtime-specific binaries to prepare for deployment to a server, container, or cloud environment.
Build for Release:
Dotnet publish should be used to generate the application's release version, which includes any framework-dependent or self-contained packages. This is required when the finished artifacts must be moved to production or staging settings.
Containerization:
Dotnet publish assists in getting the required files ready for containerization and making sure the runtime environment is configured properly if you are deploying your application to a container (such as Docker).
For local development or integration testing, use dotnet build to compile and validate code; however, when you're prepared for deployment, use dotnet publish.