Skip to content

Commit

Permalink
[#172123296] pipeline support to deploy using staging slot (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
raicastino authored Apr 2, 2020
1 parent f150c4e commit cff0a58
Show file tree
Hide file tree
Showing 2 changed files with 209 additions and 198 deletions.
207 changes: 101 additions & 106 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,69 @@
# - DANGER_GITHUB_API_TOKEN
#
# To enable the deployment in any environment you need to configure the following
# variable otherwise all the deployment jobs will be always skipped:
# global variable otherwise all the deployment jobs will be always skipped:
# - DO_DEPLOY = true
# in case of a manual run, you also need to configure the following additional
# variables based on the environment to update:
# - STAGING_ENABLE_MANUAL_DEPLOY = true
# - PRODUCTION_ENABLE_MANUAL_DEPLOY = true
# you also need to configure the following additional parameters based on the
# environment(s) to update when running the pipeline:
# - PRODUCTION_ENABLE_DEPLOY = true
# - TEST_ENABLE_DEPLOY = true
#
# The following variables are also used when running the deployment jobs:
# - STAGING_DEPLOY_MODE: 'deploy_standard' || 'deploy_to_slots'
# - STAGING_AZURE_SUBSCRIPTION
# - STAGING_RESOURCE_GROUP_NAME
# - STAGING_FUNCTION_APP_NAME
#
# - PRODUCTION_DEPLOY_MODE: 'deploy_standard' || 'deploy_to_slots'
# The following parameter and variables must also be set to run the deployment:
# - PRODUCTION_DEPLOY_TYPE:
# -- 'deployToStagingSlot': deploy to 'staging' slot
# -- 'deployToProductionSlot' (default): deploy to 'production' slot
# -- 'deployToStagingSlotAndSwap': deploy to 'staging' slot and then swap
# - PRODUCTION_AZURE_SUBSCRIPTION
# - PRODUCTION_RESOURCE_GROUP_NAME
# - PRODUCTION_FUNCTION_APP_NAME
# - PRODUCTION_APP_NAME
#
# - TEST_DEPLOY_TYPE:
# -- 'deployToTestSlot' (default): deploy to 'test' slot
# -- 'deployToProductionSlot': deploy to 'production' slot (TEST env)
# - TEST_AZURE_SUBSCRIPTION
# - TEST_RESOURCE_GROUP_NAME
# - TEST_APP_NAME
#
# Please note that the deployment to a slot (e.g. 'staging') could automatically
# trigger the swapping with 'production' slot if "auto swap" has been enabled in
# the App service configuration. This also means that if the "auto swap" is on and
# you select 'deployToStagingSlotAndSwap' to swap 'staging' and 'production' slots
# after deploying to 'staging', the swap task in this pipeline will probably fail
# because conflicting with another (automatic) swap operation in progress.
#

variables:
NODE_VERSION: '10.14.1'
YARN_CACHE_FOLDER: $(Pipeline.Workspace)/.yarn

parameters:
- name: 'TEST_ENABLE_DEPLOY'
displayName: 'Enable deploy in test environment'
type: boolean
default: false

- name: 'TEST_DEPLOY_TYPE'
displayName: 'Method to achieve deployment in Test (if enabled):'
type: string
default: deployToTestSlot
values:
- deployToTestSlot
- deployToProductionSlot

- name: 'PRODUCTION_ENABLE_DEPLOY'
displayName: 'Enable deploy in production environment'
type: boolean
default: true

- name: 'PRODUCTION_DEPLOY_TYPE'
displayName: 'Method to achieve deployment in Production (if enabled):'
type: string
default: deployToProductionSlot
values:
- deployToStagingSlot
- deployToProductionSlot
- deployToStagingSlotAndSwap

# This pipeline can be manually run or is automatically triggered whenever one
# of the following conditions is true:
# - a push is made to any branch in the repository (not only 'master')
Expand All @@ -45,7 +85,8 @@ trigger:
# This pipeline has been implemented to be run on hosted agent pools based both
# on 'windows' and 'ubuntu' virtual machine images and using the scripts defined
# in the package.json file. Since we are deploying on Azure functions on Windows
# runtime, the pipeline is currently configured to use a Windows hosted image.
# runtime, the pipeline is currently configured to use a Windows hosted image for
# the build and deploy.
pool:
vmImage: 'windows-2019'

Expand Down Expand Up @@ -128,113 +169,67 @@ stages:
displayName: 'Code coverage'
# C) Deploy to STAGE environment if the following conditions apply:
# C) Deploy to TEST environment if the following conditions apply:
# - continuos deployment (automatic):
# - $DO_DEPLOY == true and
# - $DO_DEPLOY == true and TEST_ENABLE_DEPLOY == true
# - there is a push on 'master' branch
# - manual deployment:
# - $DO_DEPLOY == true and
# - $STAGING_ENABLE_MANUAL_DEPLOY == true
# The following alternative deployment modes are supported:
# a) $STAGING_DEPLOY_MODE == 'deploy_standard': deploy to 'prodution' slot
# b) $STAGING_DEPLOY_MODE == 'deploy_to_slots': deploy to 'staging' slot and
# then swap 'staging' slot with 'production' slot
- stage: Deploy_staging
condition:
and(
succeeded(),
and (
eq(variables['DO_DEPLOY'], true),
or(
and(
# - $DO_DEPLOY == true and TEST_ENABLE_DEPLOY == true
- ${{ if eq(parameters.TEST_ENABLE_DEPLOY, true) }}:
- stage: Deploy_test
condition:
and(
succeeded(),
and (
eq(variables['DO_DEPLOY'], true),
or(
eq(variables['Build.SourceBranch'], 'refs/heads/master'),
ne(variables['Build.Reason'], 'Manual')
),
and(
eq(variables['STAGING_ENABLE_MANUAL_DEPLOY'], true),
eq(variables['Build.Reason'], 'Manual')
)
)
)
)
dependsOn:
- Build
- Test
jobs:
# Option 1: deploy directly to 'production' slot
- job: deploy_standard
condition: and(succeeded(), eq(variables['STAGING_DEPLOY_MODE'], 'deploy_standard'))
steps:
- template: azure-templates/deploy-steps.yml
parameters:
deployType: 'deployToProductionSlot'
azureSubscription: '$(STAGING_AZURE_SUBSCRIPTION)'
resourceGroupName: '$(STAGING_RESOURCE_GROUP_NAME)'
appName: '$(STAGING_FUNCTION_APP_NAME)'

# Option 2: deploy to staging slot and then swap with 'production' slot
- job: deploy_to_slots
condition: and(succeeded(), eq(variables['STAGING_DEPLOY_MODE'], 'deploy_to_slots'))
steps:
- template: azure-templates/deploy-steps.yml
parameters:
deployType: 'deployToStagingSlotAndSwap'
azureSubscription: '$(STAGING_AZURE_SUBSCRIPTION)'
resourceGroupName: '$(STAGING_RESOURCE_GROUP_NAME)'
appName: '$(STAGING_FUNCTION_APP_NAME)'
)
dependsOn:
- Build
- Test
jobs:
- job: '${{ parameters.TEST_DEPLOY_TYPE }}'
steps:
- template: azure-templates/deploy-steps.yml
parameters:
deployType: '${{ parameters.TEST_DEPLOY_TYPE }}'
azureSubscription: '$(TEST_AZURE_SUBSCRIPTION)'
resourceGroupName: '$(TEST_RESOURCE_GROUP_NAME)'
appName: '$(TEST_APP_NAME)'


# D) Deploy to PRODUCTION environment if one of the following conditions apply:
# - continuos deployment (automatic):
# - $DO_DEPLOY == true and
# - $DO_DEPLOY == true and PRODUCTION_ENABLE_DEPLOY == true
# - the 'latest' tag is pushed
# - manual deployment:
# - $DO_DEPLOY == true and
# - $PRODUCTION_ENABLE_MANUAL_DEPLOY == true
# The following alternative deployment modes are supported:
# a) $PRODUCTION_DEPLOY_MODE == 'deploy_standard': deploy to 'prodution' slot
# b) $PRODUCTION_DEPLOY_MODE == 'deploy_to_slots': deploy to 'staging' slot and
# then swap 'staging' slot with 'production' slot
- stage: Deploy_production
condition:
and(
succeeded(),
and (
eq(variables['DO_DEPLOY'], true),
or(
and(
# - $DO_DEPLOY == true and PRODUCTION_ENABLE_DEPLOY == true
- ${{ if eq(parameters.PRODUCTION_ENABLE_DEPLOY, true) }}:
- stage: Deploy_production
condition:
and(
succeeded(),
and (
eq(variables['DO_DEPLOY'], true),
or(
eq(variables['Build.SourceBranch'], 'refs/tags/latest'),
ne(variables['Build.Reason'], 'Manual')
),
and(
eq(variables['PRODUCTION_ENABLE_MANUAL_DEPLOY'], true),
eq(variables['Build.Reason'], 'Manual')
)
)
)
)
dependsOn:
- Build
- Test
jobs:
# Option 1: deploy directly to 'production' slot
- job: deploy_standard
condition: and(succeeded(), eq(variables['PRODUCTION_DEPLOY_MODE'], 'deploy_standard'))
steps:
- template: azure-templates/deploy-steps.yml
parameters:
deployType: 'deployToProductionSlot'
azureSubscription: '$(PRODUCTION_AZURE_SUBSCRIPTION)'
resourceGroupName: '$(PRODUCTION_RESOURCE_GROUP_NAME)'
appName: '$(PRODUCTION_FUNCTION_APP_NAME)'

# Option 2: deploy to staging slot and then swap with 'production' slot
- job: deploy_to_slots
condition: and(succeeded(), eq(variables['PRODUCTION_DEPLOY_MODE'], 'deploy_to_slots'))
steps:
- template: azure-templates/deploy-steps.yml
parameters:
deployType: 'deployToStagingSlotAndSwap'
azureSubscription: '$(PRODUCTION_AZURE_SUBSCRIPTION)'
resourceGroupName: '$(PRODUCTION_RESOURCE_GROUP_NAME)'
appName: '$(PRODUCTION_FUNCTION_APP_NAME)'
dependsOn:
- Build
- Test
jobs:
- job: '${{ parameters.PRODUCTION_DEPLOY_TYPE }}'
steps:
- template: azure-templates/deploy-steps.yml
parameters:
deployType: '${{ parameters.PRODUCTION_DEPLOY_TYPE }}'
azureSubscription: '$(PRODUCTION_AZURE_SUBSCRIPTION)'
resourceGroupName: '$(PRODUCTION_RESOURCE_GROUP_NAME)'
appName: '$(PRODUCTION_APP_NAME)'
Loading

0 comments on commit cff0a58

Please sign in to comment.