Skip to content

Commit

Permalink
[#175476527] refactor pipelines (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
balanza authored Dec 2, 2020
1 parent a7ec4d2 commit c97a8e8
Show file tree
Hide file tree
Showing 6 changed files with 268 additions and 565 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,16 @@ parameters:
- name: 'cache_version_id'
type: string
default: $(CACHE_VERSION_ID)

# the branch, tag or commit to deploy
- name: 'gitReference'
type: string
default: '$(Build.SourceVersion)'


steps:
- checkout: self
displayName: 'Checkout'

- task: Cache@2
inputs:
key: 'yarn-${{ parameters.cache_version_id }} | "$(Agent.OS)" | yarn.lock'
Expand All @@ -31,21 +30,23 @@ steps:
path: $(YARN_CACHE_FOLDER)
displayName: 'Cache yarn packages'


- task: UseNode@1
inputs:
version: $(NODE_VERSION)
displayName: 'Set up Node.js'

# This is needed because the pipeline may point to a different commit than expected
# The common case is when the previous stage pushed another commit
- ${{ if ne(parameters.gitReference, variables['Build.SourceVersion']) }}:
- script: |
git fetch && git checkout ${{ parameters.gitReference }}
displayName: 'Checkout reference'
- script: |
yarn install --frozen-lockfile --no-progress --non-interactive --network-concurrency 1
displayName: 'Install node modules'
condition: ne(variables.CACHE_RESTORED, 'true')

- ${{ if eq(parameters.make, 'build') }}:
- bash: yarn build
Expand Down
96 changes: 96 additions & 0 deletions .devops/code-review-pipelines.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# Azure DevOps pipeline to build, check source codes and run tests.
#
# To make Danger JS run on a pull request you need to add the following pipeline
# variable and set it with a GitHub access token (scope public_repo); otherwise
# set its value to 'skip' without marking it secret:
# - DANGER_GITHUB_API_TOKEN
#

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

# Automatically triggered on PR
# https://docs.microsoft.com/en-us/azure/devops/pipelines/yaml-schema?view=azure-devops&tabs=schema%2Cparameter-schema#pr-trigger
trigger: none

# Execute agents (jobs) on latest Ubuntu version.
# To change OS for a specific, ovverride "pool" attribute inside the job definition
pool:
vmImage: 'ubuntu-latest'

stages:
- stage: Build
dependsOn: []
jobs:
- job: make_build
pool:
# As we deploy on Wondows machines, we use Windows to build
vmImage: 'windows-2019'
steps:
- template: azure-templates/make-build-steps.yml
parameters:
make: build

- stage: Static_analysis
dependsOn: []
jobs:

- job: lint
steps:
- template: azure-templates/make-build-steps.yml
parameters:
make: install_dependencies
- script: |
yarn lint
displayName: 'Lint'
- job: validate_api_specification
steps:
- template: azure-templates/make-build-steps.yml
parameters:
make: install_dependencies

- script: |
yarn lint-api
displayName: 'Validate API specification'
- job: danger
condition:
and(
succeeded(),
ne(variables['DANGER_GITHUB_API_TOKEN'], 'skip')
)
steps:
- template: azure-templates/make-build-steps.yml
parameters:
make: install_dependencies

- bash: |
yarn danger ci
env:
DANGER_GITHUB_API_TOKEN: '$(DANGER_GITHUB_API_TOKEN)'
displayName: 'Danger CI'
# B) Run unit tests if there is a push or pull request on any branch.
- stage: Test
dependsOn: []
jobs:
- job: unit_tests
steps:
- template: azure-templates/make-build-steps.yml
parameters:
make: install_dependencies

- script: |
yarn generate
displayName: 'Generate definitions'
- script: |
yarn test:coverage
displayName: 'Unit tests exec'
- bash: |
bash <(curl -s https://codecov.io/bash)
displayName: 'Code coverage'
166 changes: 166 additions & 0 deletions .devops/deploy-pipelines.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
# Azure DevOps pipeline to release a new version and deploy to production.

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

parameters:
- name: 'RELEASE_SEMVER'
displayName: 'When packing a release, define the version bump to apply'
type: string
values:
- major
- minor
- patch
default: minor

# Only manual activations are intended
trigger: none
pr: none

# 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 for
# the build and deploy.
pool:
vmImage: 'windows-2019'

resources:
repositories:
- repository: pagopaCommons
type: github
name: pagopa/azure-pipeline-templates
ref: refs/tags/v3
endpoint: 'pagopa'

stages:

# Create a relase
# Activated when ONE OF these are met:
# - is on branch master
# - is a tag in the form v{version}-RELEASE
- stage: Release
condition:
and(
succeeded(),
or(
eq(variables['Build.SourceBranch'], 'refs/heads/master'),
and(
startsWith(variables['Build.SourceBranch'], 'refs/tags'),
endsWith(variables['Build.SourceBranch'], '-RELEASE')
)
)
)
pool:
vmImage: 'ubuntu-latest'
jobs:
- job: make_release
steps:
- ${{ if eq(variables['Build.SourceBranch'], 'refs/heads/master') }}:
- template: templates/node-github-release/template.yaml@pagopaCommons
parameters:
semver: '${{ parameters.RELEASE_SEMVER }}'
gitEmail: $(GIT_EMAIL)
gitUsername: $(GIT_USERNAME)
gitHubConnection: $(GITHUB_CONNECTION)
nodeVersion: $(NODE_VERSION)
pkg_cache_version_id: $(CACHE_VERSION_ID)
pkg_cache_folder: $(YARN_CACHE_FOLDER)

- ${{ if ne(variables['Build.SourceBranch'], 'refs/heads/master') }}:
- script: |
echo "We assume this reference to be a valid release: $(Build.SourceBranch). Therefore, there is no need to bundle a new release."
displayName: 'Skip release bundle'
# Prepare Artifact
- stage: Deploy_staging
dependsOn:
- Release
jobs:
- job: 'prepare_artifact_and_deploy'
steps:
# Build application
- template: azure-templates/make-build-steps.yml
parameters:
make: predeploy_build
# On the assumption that this stage is executed only when Relase stage is,
# with this parameter we set the reference the deploy script must pull changes from.
# The branch/tag name is calculated from the source branch
# ex: Build.SourceBranch=refs/heads/master --> master
# ex: Build.SourceBranch=refs/tags/v1.2.3-RELEASE --> v1.2.3-RELEASE
gitReference: ${{ replace(replace(variables['Build.SourceBranch'], 'refs/tags/', ''), 'refs/heads/', '') }}

# Install functions extensions
- task: DotNetCoreCLI@2
inputs:
command: "build"
arguments: "-o bin"
# Copy application to
- task: CopyFiles@2
inputs:
SourceFolder: '$(System.DefaultWorkingDirectory)'
TargetFolder: '$(Build.ArtifactStagingDirectory)'
Contents: |
**/*
!.git/**/*
!**/*.js.map
!**/*.ts
!.vscode/**/*
!azure-templates/**/*
!azure-pipelines.yml
!.prettierrc
!.gitignore
!README.md
!jest.config.js
!local.settings.json
!test
!tsconfig.json
displayName: 'Copy deploy files'

- task: AzureFunctionApp@1
inputs:
azureSubscription: '$(PRODUCTION_AZURE_SUBSCRIPTION)'
resourceGroupName: '$(PRODUCTION_RESOURCE_GROUP_NAME)'
appType: 'functionApp'
appName: '$(PRODUCTION_APP_NAME)'
package: '$(Build.ArtifactStagingDirectory)/'
deploymentMethod: 'auto'
deployToSlotOrASE: true
slotName: 'staging'
displayName: Deploy to staging slot

# Check that the staging instance is healthy
- stage: Healthcheck
dependsOn:
- Deploy_staging
jobs:
- job: 'do_healthcheck'
steps:
- template: templates/rest-healthcheck/template.yaml@pagopaCommons
parameters:
azureSubscription: '$(PRODUCTION_AZURE_SUBSCRIPTION)'
appName: '$(PRODUCTION_APP_NAME)'
endpoint: 'https://$(PRODUCTION_APP_NAME)-staging.azurewebsites.net/info'
endpointType: 'private'
containerInstanceResourceGroup: 'io-p-rg-common'
containerInstanceVNet: 'io-p-vnet-common'
containerInstanceSubnet: 'azure-devops'

# Promote the staging instance to production
- stage: Deploy_production
dependsOn:
- Healthcheck
- Deploy_staging
jobs:
- job: 'do_deploy'
steps:
- task: AzureAppServiceManage@0
inputs:
azureSubscription: '$(PRODUCTION_AZURE_SUBSCRIPTION)'
resourceGroupName: '$(PRODUCTION_RESOURCE_GROUP_NAME)'
webAppName: '$(PRODUCTION_APP_NAME)'
sourceSlot: staging
swapWithProduction: true
displayName: Swap with production slot
Loading

0 comments on commit c97a8e8

Please sign in to comment.