Ef core Migration in DevOps pipelines

Steps

Configure build pipeline

  • Modify your pipeline to include dotnet ef tool. And then add the steps to generate the bundle.
trigger:
- master

pool:
vmImage: 'ubuntu-latest'

variables:
buildConfiguration: 'Release'

steps:
- task: UseDotNet@2
inputs:
packageType: 'sdk'
version: '6.0.101'

- task: DotNetCoreCLI@2
displayName: Install EF Tool
inputs:
command: custom
custom: 'tool'
arguments: 'install -g dotnet-ef'

- script: dotnet build --configuration $(buildConfiguration)
displayName: 'dotnet build $(buildConfiguration)'

- task: DotNetCoreCLI@2
inputs:
command: 'publish'
publishWebProjects: true
arguments: '--configuration $(BuildConfiguration) --output $(Build.ArtifactStagingDirectory)'
displayName: 'dotnet publish $(buildConfiguration)'

- task: Bash@3
displayName: Add migrations directory
inputs:
targetType: 'inline'
script: 'mkdir $(Build.ArtifactStagingDirectory)/bundles'

- task: DotNetCoreCLI@2
displayName: Generate migration bundle - MyApiDbContext
inputs:
command: custom
custom: 'ef'
arguments: 'migrations bundle --startup-project Api --context MyApiDbContext -r "linux-x64" -o $(Build.ArtifactStagingDirectory)/bundles/myApiDbContext'

- task: CopyFiles@2
inputs:
SourceFolder: '$(System.DefaultWorkingDirectory)'
Contents: 'azure-deploy.sh'
TargetFolder: '$(Build.ArtifactStagingDirectory)'
displayName: "copy build script"



# this code takes all the files in $(Build.ArtifactStagingDirectory) and uploads them as an artifact of your build.
- task: PublishBuildArtifacts@1
inputs:
pathtoPublish: '$(Build.ArtifactStagingDirectory)'
artifactName: 'MyApi'
displayName: 'dotnet publish artifact'

Configure release pipeline

  • Copy the generated files to where other dlls are present. (this part is not documented, but it always failed if being run from anywhere else even with--connection argument).
  • Modify the permissions to run them as executable
  • Run it. Here is a sample bash script to do all this.
#stop service if you are running api as service
sudo systemctl stop kestral-myApi.service
sudo rm -r -f /var/www/MyApi/
sudo mkdir /var/www/MyMApi/
sudo 7za x Api.zip -o/var/www/MyApi/
#copy the migration bundle to Api directory
sudo cp -a bundles/. /var/www/MyApi/
pushd /var/www/MyApi/ || exit 1
sudo chmod a+x myApiDbContext
sudo ./myApiDbContext
popd || exit 1
#restart the service
sudo systemctl start kestral-myApi.service

--

--

--

Software consultant

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Cross-Platform App vs. Native App Development in the ordinary life of a dev

Native App on a Smartphone

Introducing Stickers: A Gmail Label Management Tool for Google Workspace

Share Texts, URLs, or Files Using the Navigator Share API

My №1 Productivity Tool: g

Kubernetes for dev infrastructure

Why Entity Framework isn’t a Good Model, Part I: Lazy Loading

A Day in the Life of a Support Analyst

Defining motion paths in MotionLayout

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Prashant Choudhary

Prashant Choudhary

Software consultant

More from Medium

Working effectively with coordinates in Entity Framework

Transactionally update Azure App Configuration

.NET Code coverage on GitLab

Troubleshooting CORS Errors