Skip to content

Instantly share code, notes, and snippets.

@FilBot3
Created March 30, 2022 15:13
Show Gist options
  • Select an option

  • Save FilBot3/d8184b3c0b1c887e7e99884b051bd73c to your computer and use it in GitHub Desktop.

Select an option

Save FilBot3/d8184b3c0b1c887e7e99884b051bd73c to your computer and use it in GitHub Desktop.
Azure DevOps Pipeline failing to find registered variable in Deployment Job
---
# I am attempting to create a ServiceNow Change Request, register the sys_id to a pipeline var, then use it in
# subsequent steps in the pipeline. I want to use a Deployment Job because it allows me to track uses of the
# environment and pipeline for auditing. Then I can also stick on other "security" mechanisms or gating
# mechanisms to prevent unwanted usage.
# I also wanted to use a Deployment Job because it seemed to allow for the breaking of and catching of failed
# steps. However, it's proving to be more difficult than it really should be and is very unintuitive.
# The Azure DevOps Pipelines documentation has changed a lot and is now missing pieces of docs that were once
# there.
trigger: none
pool:
vmImage: ubuntu-latest
variables:
- group: 'ServiceNow Values'
stages:
- stage: BuildAndPublishStage
displayName: Build and Publish
jobs:
- job: BuildAndPublishJob
displayName: Build and Publish
workspace:
clean: all
steps:
- task: Bash@3
displayName: Install Poetry
inputs:
targetType: inline
script: |
curl -sSL https://install.python-poetry.org | python -
env:
POETRY_HOME: '/opt/poetry'
- task: Bash@3
displayName: Build and Publish
inputs:
targetType: inline
script: |
/opt/poetry/bin/poetry install
/opt/poetry/bin/poetry build
/opt/poetry/bin/poetry publish \
--verbose \
--username azure \
--password $(System.AccessToken) \
--repository azure
- stage: TestInstallStage
displayName: Test the install
jobs:
- job: TestInstallJob
displayName: Test the install
workspace:
clean: all
steps:
- task: Bash@3
displayName: Install ServiceNow
inputs:
targetType: inline
script: |
pip install snow --index-url=https://azure:$(System.AccessToken)@pkgs.dev.azure.com/$(ADO_ORG)/$(ADO_PROJ)/_packaging/python-azure-artifacts/pypi/simple/
servicenow --help
- task: Bash@3
displayName: Run ServiceNow
inputs:
targetType: inline
script: |
servicenow --help
- stage: DeployToProductionStage
displayName: Production Deployment
condition: eq(variables['PhilIsGreat'], 'true')
jobs:
- deployment: BuildPythonApp
displayName: Build Python App
environment: PhilProdEnv
workspace:
clean: all
strategy:
runOnce:
preDeploy:
steps:
- task: Bash@3
displayName: Install ServiceNow
inputs:
targetType: inline
script: |
pip install snow --index-url=https://azure:$(System.AccessToken)@pkgs.dev.azure.com/$(ADO_ORG)/$(ADO_PROJ)/_packaging/python-azure-artifacts/pypi/simple/
- task: Bash@3
name: snow
displayName: Create Standard RFC from Template
# This task, I'm registering the SYS_ID of the RFC being created. I want to use this throughout the rest
# of the Deployment Job.
inputs:
targetType: inline
script: |
export sys_id=$(servicenow standard create template $(std_tmpl_sys_id) --query="result.sys_id.value")
echo "##vso[task.setvariable variable=rfc_sys_id;isoutput=true]$sys_id"
env:
SNOW_USER: '$(SNOW_USER)'
SNOW_PASS: '$(SNOW_PASS)'
- task: Bash@3
displayName: Progress RFC to Scheduled
# This works, for this one task.
inputs:
targetType: inline
script: |
servicenow standard update $(snow.rfc_sys_id) state=Scheduled
env:
SNOW_USER: '$(SNOW_USER)'
SNOW_PASS: '$(SNOW_PASS)'
deploy:
steps:
- task: Bash@3
displayName: Install ServiceNow
inputs:
targetType: inline
script: |
pip install snow --index-url=https://azure:$(System.AccessToken)@pkgs.dev.azure.com/$(ADO_ORG)/$(ADO_PROJ)/_packaging/python-azure-artifacts/pypi/simple/
- task: Bash@3
displayName: Progress RFC to Implement
# Here, I attempt to get the registered variable from the preDeploy "phase" and use it as a Shell Variable
# because otherwise Azure DevOps would try to just execute it as a shell command.
inputs:
targetType: inline
script: |
servicenow standard update ${RFC_SYS_ID} state=Implement
env:
SNOW_USER: '$(SNOW_USER)'
SNOW_PASS: '$(SNOW_PASS)'
RFC_SYS_ID: $[ dependencies.BuildPythonApp.outputs['preDeploy.rfc_sys_id'] ]
- task: Bash@3
displayName: Perform Changes
inputs:
targetType: inline
script: |
echo "Hello, World!"
on:
success:
steps:
- task: Bash@3
displayName: Install ServiceNow
inputs:
targetType: inline
script: |
pip install snow --index-url=https://azure:$(System.AccessToken)@pkgs.dev.azure.com/$(ADO_ORG)/$(ADO_PROJ)/_packaging/python-azure-artifacts/pypi/simple/
- task: Bash@3
displayName: Progress RFC to Review
# This was another method I tired to use to reference the registered variable.
# It did not work.
inputs:
targetType: inline
script: |
servicenow standard update $(snow.rfc_sys_id) state=Review
env:
SNOW_USER: '$(SNOW_USER)'
SNOW_PASS: '$(SNOW_PASS)'
- task: Bash@3
displayName: Progress RFC to Closed
inputs:
targetType: inline
script: |
servicenow standard update $(snow.rfc_sys_id) state=Closed
env:
SNOW_USER: '$(SNOW_USER)'
SNOW_PASS: '$(SNOW_PASS)'
failure:
steps:
- task: Bash@3
displayName: Install ServiceNow
inputs:
targetType: inline
script: |
pip install snow --index-url=https://azure:$(System.AccessToken)@pkgs.dev.azure.com/$(ADO_ORG)/$(ADO_PROJ)/_packaging/python-azure-artifacts/pypi/simple/
- task: Bash@3
displayName: Progress RFC to Canceled
inputs:
targetType: inline
script: |
servicenow standard update $(snow.rfc_sys_id) state=Canceled
env:
SNOW_USER: '$(SNOW_USER)'
SNOW_PASS: '$(SNOW_PASS)'
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment