Skip to content

Instantly share code, notes, and snippets.

@stories2
Last active February 22, 2022 22:53
Show Gist options
  • Save stories2/b4fb0da9d052e21db5e445e7f91dfc8e to your computer and use it in GitHub Desktop.
Save stories2/b4fb0da9d052e21db5e445e7f91dfc8e to your computer and use it in GitHub Desktop.
Publish ASP.NET Framework project using command line

ASP.NET Framework Publish

This is my reference. So what we will do, is make the project build release and copy to specific directory which is build result files.

It will run only Windows Command line.

  1. Open the command line console.
  2. Move to project folder which is same direction with [PROJECT-NAME].csproj
cd [PROJECT-DIRECTION]
  1. Build our project
msbuild [PROJECT-NAME].csproj /p:DeployOnBuild=true /p:PublishProfile=[PUBLISH-PROFILE-NAME] /p:Configuration=Release /p:AutoParameterizationWebConfigConnectionStrings=false
  1. Copy the result files
xcopy obj\Release\Package\PackageTmp [DIRECTORY-WHERE-YOU-WANT] /s /e

Done.

After we do is, if we commit changes to git server then auto build and deploy(File System) it. We will use git hooks. Reference git hooks ,msbuild options and precompile option

  1. Go to the project root folder which is in with .git folder
cd [PROJECT-DIRECTORY]
  1. Move to git hooks folder
cd .git\hooks
  1. There is some .sample files, open the pre-push.sample file using notepad.
  2. Write some script down below.
#!/bin/sh

echo "Move to working directory"
# change env
cd [PROJECT-DIRECTORY]
pwd

# build

echo "Clean obj folder"
rm -rf ./obj/Release

echo "Clean bin folder"
rm -rf ./bin/_PublishedWebsites

echo "Start to run publish process"
/C/Windows/Microsoft.NET/Framework64/v4.0.30319/msbuild.exe ./[PROJECT-NAME].csproj //p:OutputPath=bin //p:DeployOnBuild=true //p:Configuration=Release //p:AutoParameterizationWebConfigConnectionStrings=false //p:WebPublishMethod="FileSystem";PrecompileBeforePublish=true;EnableUpdateable=false;DebugSymbols=true;WDPMergeOption="CreateSeparateAssembly";UseFixedNames=true

cp -vr ./bin/_PublishedWebsites/[PROJECT-NAME]/* /[PATH-TO-WHAT-YOU-WANT]

echo "Done!"

exit 0

...And this is version of latest auto build script

#!/bin/sh

projectDir=
projectFile=
outputDir=
branch=
processResult=1

currentBranch=$(git branch | sed --quiet 's/* \(.*\)/\1/p')

echo "Project directory path: ${projectDir}"
echo "Project csproj name: ${projectFile}"
echo "Project output directory path: ${outputDir}"
echo "Git branch ${branch} : ${currentBranch}"

if [ $currentBranch != $branch ]; then
  echo "Current branch[${currentBranch}] is not matching expected branch[${branch}]"
  exit 1
fi

echo "Move to working directory"
# change env
cd $projectDir
pwd

# build

echo "Clean obj folder"
rm -rf ./obj/Release

echo "Clean bin folder"
rm -rf ./bin/_PublishedWebsites

echo "Start to run publish process"
/C/Windows/Microsoft.NET/Framework64/v4.0.30319/msbuild.exe ./${projectFile}.csproj //p:OutputPath=bin //p:DeployOnBuild=true //p:Configuration=Release //p:AutoParameterizationWebConfigConnectionStrings=false //p:WebPublishMethod="FileSystem";PrecompileBeforePublish=true;EnableUpdateable=false;DebugSymbols=true;WDPMergeOption="CreateSeparateAssembly";UseFixedNames=true

processResult=$?

if [ "${processResult}" != 0 ]; then
  echo "Cannot build current project."
  exit 1
fi

echo "Start to move build files"

cp -vr ./bin/_PublishedWebsites/${projectFile}/* $outputDir

processResult=$?

if [ "${processResult}" != 0 ]; then
  echo "Cannot move build files."
  exit 1
fi
echo "Done!"

exit 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment