Below is the reusuable workflow where I wish to create a connection string to DB ConnectionString
in one step and use it in all other subsequent steps so there is no redundancy of code.
---
name: executescript
on:
workflow_call:
inputs:
DBport:
description: 'Database Port'
required: true
type: string
default: 27017
outputs:
srcprimarydbhost:
description: 'Source Primary DB Host'
value: "mydbhost"
jobs:
Detect_Environment:
outputs:
recenvironments: "tempvalue"
runs-on:
group: "ARE-Shared"
steps:
- name: Standard inventory DB connection string
id: getdbconnstring
if: always()
run: |
$ServerInstance="MYDBHOSTPRD_DB01,3433"
$Database="util"
$SQlUserName="svc-user"
$SQLPassword="${{ secrets.UTILITY_INV }}"
$ConnectionString = "Server=$ServerInstance;Database=$Database;User Id=$SQlUserName;Password=$SQLPassword;TrustServerCertificate=True"
echo "ConnectionString=$ConnectionString" >> "$env:GITHUB_OUTPUT"
- name: Get data from inventory DB
id: getsnowappname
if: always()
run: |
$squery = "SELECT TOP 1 * FROM util.[dbo].[tbl_inv] (nolock)"
Write-Host "Invoke-Sqlcmd -ConnectionString ${{ steps.getdbconnstring.outputs.ConnectionString }} -query $squery"
$sanList = "Invoke-Sqlcmd -ConnectionString ${{ steps.getdbconnstring.outputs.ConnectionString }} -query $squery"
However, I get the below error:
shell: C:WindowsSystem32WindowsPowerShellv1.0powershell.EXE -command ". '{0}'"
At D:Git-Runnersselfwindowsrunner_work_temp8f233a3a-0af9-4ece-8a6c-2bec58e5f93b.ps1:16 char:137
+ ... 433;Database=util;User Id=svc-user;***;Tr ...
+ ~
The ampersand (&) character is not allowed. The & operator is reserved for future use; wrap an ampersand in double
quotation marks ("&") to pass it as part of a string.
+ CategoryInfo : ParserError: (:) [], ParseException
+ FullyQualifiedErrorId : AmpersandNotAllowed
Note:
-
The error is misleading in a way that there is no
&
used in the code but probably beccause the connection object variable cannot persist its state when access via GITHUB OUTPUTS. -
If I combine both the steps as one then the DB query works fine and it is free of any errors.