Skip to content

Instantly share code, notes, and snippets.

@turgayozgur
Created January 19, 2019 22:09
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save turgayozgur/e5fe22ded70868c7e61c1dba0addfff0 to your computer and use it in GitHub Desktop.
Save turgayozgur/e5fe22ded70868c7e61c1dba0addfff0 to your computer and use it in GitHub Desktop.
Building .NET Core App on circleci with caching packages. Restore only if required.
#
# The fallowing circleci config yml and bash scripts demonstrate
# how to build .NET Core app without restoring packages for every build
# until you referance smothing diffrent to any project inside sln.
#
# If you are building docker container when building your application,
# use the docker layer caching insted of these scripts. https://circleci.com/docs/2.0/docker-layer-caching/
#
version: 2
jobs:
build:
docker:
- image: microsoft/dotnet:2.1.500-sdk
steps:
- checkout
- run:
name: Dotnet Packages Checksum File
command: bash ./ci-dotnet-packages.sh
- restore_cache:
keys:
- dotnet-packages-key-v1-{{ checksum "packages.txt" }}
- run:
name: Restore Packages
command: bash ./ci-dotnet-restore.sh
- save_cache:
key: dotnet-packages-key-v1-{{ checksum "packages.txt" }}
paths:
- ~/.nuget/packages
- ./objs.tar.gz
- run:
name: Build
command: bash ./ci-build.sh
# Now, you can zip your packages and deploy to somewhere.
#!/bin/bash
set -euo pipefail
IFS=$'\n\t'
PROJECT_NAME="./src/Example.Web" # change the project name.
# build projects.
dotnet build --no-restore -c Release
# publish artifacts to root folder.
# You can call only the pulish without --no-build option, if you are not building something not referanced on your project.
dotnet publish $PROJECT_NAME.csproj --no-build --no-restore -c Release -o ../artifacts
#!/bin/bash
#
# Find recursively all the .csproj files on given path
# and extract the package referance lines to packages.txt file.
#
set -euo pipefail
IFS=$'\n\t'
PATHS_TO_FIND="./**/*.csproj" # change it by depend on your project folder structure.
addPackages () {
local search="$1"
for path in $search; do
while read -r line; do
if [[ $line =~ "<PackageReference" ]]; then
echo "${line// /}" >> packages.txt
fi
done < "$path"
done
}
# projects
addPackages $PATHS_TO_FIND
#!/bin/bash
#
# .NET Core try to find obj folders to ensure the package required is installed before or not.
# The script collect all the obj folder to zip(tar.gz) file to extract it for the next build.
#
set -euo pipefail
IFS=$'\n\t'
PATHS_TO_FIND="./**/*.csproj" # change it by depend on your project folder structure.
copyObjFolders () {
local search="$1"
for path in $search; do
mkdir -p ./objs/$path/..
cp -r $path ./objs/$path/..
done
}
if [ ! -f ./objs.tar.gz ]; then
#restore
dotnet restore
#projects
copyObjFolders $PATHS_TO_FIND
#zip.
tar -czf objs.tar.gz -C objs .
else
#unzip objs
tar -xzf objs.tar.gz
echo "Restore skipped."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment