We can easily set-up the CI/CD pipelines in Azure Devops either by utilizing the YAML file or by writing a PowerShell script to define the individual deployment subtasks. In this blog, we will unravel the step-by-step process to structure the Azure Devops by making use of YAML files.
-
Developers
should login to their SF environment by using sfdx force:
auth:web:login command and set the alias of the environment(for ex:
AVCS-DEV) and generate the SFDX authURL through following verbose
flag commands sfdx force:org:display -u AVCS-DEV –verbose.
Copy
the SFDX authURL value generated in previous step, it usually starts
with force://PlatformCLI for ex: force://PlatformCLI::
@business-ruby-2985-dev-ed.cs73.my.salesforce.com
Salesforce Authentication (using SFDX CLI):
-
Go
to your Azure project > Pipelines > Environments
Click
on new environment and use the standard naming convention for the
environment. For ex:
- For adding multiple approvers to manage the release, click on the 3 dots.i.e. AVCS-DEV
Azure Config for creating new pipelines:
- Go to your Azure project > Pipelines > Library > Variable Group
Setting-up Environment variables:
-
Name
of the variable group should follow a specific syntax:
-config- Navigate to variable section(see the screenshot below) and create a new variable named as authURL with value SfdxAuth Url (force://PlatformCLI::, i.e.: AVCS-config-DEV. @business-ruby-2985-dev- ed.cs73.my.salesforce.com) copied from the previous
-
Download the yml templates below and copy them in the root directory of your project repository. Make sure that the sfdx-deploy,yml is added under the "templates" folder.
Adding the YAML file in Repository (GIT)
# File: azure-pipelines.yml
stages:
- stage: Build
jobs:
- job: Build
pool:
vmImage: 'macOS-10.14'
steps:
- task: CopyFiles@2
inputs:
SourceFolder: '$(Build.SourcesDirectory)'
Contents: '**'
TargetFolder: '$(Build.ArtifactStagingDirectory)'
displayName: "Copy $(sourceFolder) to artifact staging directory"
- task: PublishBuildArtifacts@1
inputs:
PathtoPublish: '$(Build.ArtifactStagingDirectory)'
ArtifactName: 'app'
publishLocation: 'Container'
displayName: "Publish Azure artifact"
########DEV#######################
- stage: Deploy_DEV
condition: and(succeeded(), eq(variables['Build.SourceBranch'],
'refs/heads/master'))
jobs:
- template: templates/sfdx-deploy.yml
parameters:
authUrl: $(brocode-dev-authurl)
targetEnv: 'brocode_dev_ed'
config: brocode-config
compareWith: 'origin/master'
testlevel: 'NoTestRun'
########SIT#######################
- stage: Deploy_SIT
condition: and(succeeded(), eq(variables['Build.SourceBranch'],
'refs/heads/master'))
jobs:
- template: templates/sfdx-deploy.yml
parameters:
authUrl: $(brocode-sit-authurl)
targetEnv: 'brocode_sit_ed'
config: brocode-config
compareWith: 'origin/master'
testlevel: 'RunLocalTests'
########UAT#######################
- stage: Deploy_UAT
condition: and(succeeded(), eq(variables['Build.SourceBranch'],
'refs/heads/release'))
jobs:
- template: templates/sfdx-deploy.yml
parameters:
authUrl: $(brocode-uat-authurl)
targetEnv: 'brocode_uat_ed'
config: brocode-config
compareWith: 'origin/master'
testlevel: 'RunSpecifiedTests'
# File: templates/sfdx-steps.yml
parameters:
authUrl: ''
targetEnv: ''
config: ''
compareWith: 'origin/master'
testlevel: ''
jobs:
- deployment: $ # only alphanumeric plus underscores allowed, may not
start with a number
displayName: $ #friendly name
variables:
- group: $
pool:
vmImage: 'Ubuntu-16.04'
###### creates an environment if it doesn’t exist
environment: $
strategy:
###### default deployment strategy
runOnce:
deploy:
steps:
- task: DownloadBuildArtifacts@0
inputs:
buildType: 'current'
downloadType: 'single'
artifactName: 'app'
downloadPath: '$(System.ArtifactsDirectory)'
- task: UseNode@1
inputs:
checkLatest: true
displayName: "Node 10.x install"
- script: |
npm install -g sfdx-cli
displayName: "npm install sfdx-cli"
- script: |
echo $ > credentials.txt
sfdx force:auth:sfdxurl:store -f credentials.txt -a $(Environment.Name)
displayName: "Authorise sfdx"
- script: |
cd "$(System.ArtifactsDirectory)/app"
echo sfdx force:mdapi:deploy
sfdx force:mdapi:deploy -d ./src -u $(Environment.Name) -l $ -w 600 -c
displayName: "sfdx deploy"