Skip to content

Commit b62786f

Browse files
zijchendzsquared
andauthored
Introducing sql-action v2 (#122)
* v2 - Use tedious mssql module instead of sqlcmd (#96) * Use tedious mssql library instead of sqlcmd * Fix mssql connection * Fix SqlUtils tests * Use config instead of connection string * Replace conn string builder with mssql config * Connect to master db * Restore connection string validation regex * PR comments, fix error handling * Update main.js * Use try catch for error handling * Fix typo * v2 - Change script action from sqlcmd to mssql query (#99) * Change script action from sqlcmd to mssql query * Update action.yml * Fully qualify Table1 in sql script * Add more debug logging * Clone config before changing db to master * Cleanup * Set TEST_DB name before cleanup * Use runner.temp * Always cleanup * PR comments * Fix database cleanup step in pr-check (#101) * Debug script contents * Fix sed command * Remove debug * v2 - Add support for AAD password, AAD default, and AAD service principal auth (#100) * Use tedious mssql library instead of sqlcmd * Fix mssql connection * Fix SqlUtils tests * Use config instead of connection string * Replace conn string builder with mssql config * Connect to master db * Restore connection string validation regex * AAD auth * Add support for client and tenant IDs * Add more debug messaging * Fix connection string find array * Make client-id and tenant-id action inputs * Fix error handling * More fixes * Use try catch instead * Add tests to pr-check.yml * Change script action from sqlcmd to mssql query * Update action.yml * Fully qualify Table1 in sql script * Add more debug logging * Clone config before changing db to master * Cleanup * Set TEST_DB name before cleanup * Use runner.temp * Always cleanup * Add tests for different auth types * Mask tenant and client IDs * Add AAD password test to pr-check.yml * Fix yaml * Limit max-parallel to 2 * Add test for service principal * PR comments * Fix typo * Cleanup sqlcmd references (#102) * Retry connection with user DB if master connection fails (#104) * Retry with DB connection if master fails * Add tests * Add ConnectionResult interface * Add missing doc comment * PR comments * PR Comments * Download and extract go-sqlcmd before main action (#108) * Add setup script to download go-sqlcmd * Add sqlcmd call to pr-check.yml * Add bz2 specific extract tar * Move setup code to main * Move setup code to main * Fix casing of Setup.ts * Use go-sqlcmd for script action (#113) * call go-sqlcmd for script action * Fix auth options not flowing through * Add test cases * Restore sqlcmd variable in cleanup script * Fix pr-check cleanup * Undo changes to pr-check.yml * Undo pr-check.yml changes * PR comments * Change inputs (#105) * Update SQL API version to 2021-11-01 (#117) * Add support for Script, DeployReport, and DriftReport (#106) * Change inputs * Add other publish like actions * Add tests * Fix test * PR comments * Add mockReturnValue for auth type test * Remove MacOS from ci.yml * links to documentation on each argument type (#123) * v2 - Remove client-id and tenant-id as inputs (#124) * Set tenantId and clientId only when passed in * Default to empty string * Default to empty string * Replace mssql call with go-sqlcmd query * Capture sqlcmd error message * Add more debug * Capture stdout too * Fix config passing * Completely remove client-id and tenant-id * cleanup * Update sqlcmd query * adding connection string format to docs (#138) adding connection string format to docs Co-authored-by: Drew Skwiers-Koballa <dzsquared@users.noreply.github.com>
1 parent eed4942 commit b62786f

26 files changed

+3695
-724
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ jobs:
66
runs-on: ${{ matrix.os }}
77
strategy:
88
matrix:
9-
os: [windows-latest, ubuntu-latest, macos-latest]
9+
os: [windows-latest, ubuntu-latest]
1010
steps:
1111

1212
- name: 'Checking out repo code'

.github/workflows/pr-check.yml

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,18 @@ jobs:
1717
strategy:
1818
matrix:
1919
os: [windows-latest, ubuntu-latest]
20+
auth_type: [sql_login, aad_password, service_principal] # Unique for each parallel job run, part of TEST_DB name to ensure no collisions from parallel jobs
21+
include:
22+
# Includes the connection string Github secret name, effectively a switch statement on auth_type
23+
- auth_type: sql_login
24+
connection_string_secret: AZURE_SQL_CONNECTION_STRING_NO_DATABASE
25+
- auth_type: aad_password
26+
connection_string_secret: AAD_PASSWORD_CONNECTION_STRING_NO_DATABASE
27+
- auth_type: service_principal
28+
connection_string_secret: SERVICE_PRINCIPAL_CONNECTION_STRING_NO_DATABASE
29+
2030
env:
21-
TEST_DB: 'SqlActionTest-${{ matrix.os }}'
31+
TEST_DB: 'SqlActionTest-${{ matrix.os }}-${{ matrix.auth_type }}'
2232

2333
steps:
2434
- name: Checkout from PR branch
@@ -42,26 +52,36 @@ jobs:
4252
- name: Test DACPAC Action
4353
uses: ./
4454
with:
45-
connection-string: '${{ secrets.AZURE_SQL_CONNECTION_STRING_NO_DATABASE }}Initial Catalog=${{ env.TEST_DB }};'
46-
dacpac-package: ./__testdata__/sql-action.dacpac
55+
connection-string: '${{ secrets[matrix.connection_string_secret] }}Initial Catalog=${{ env.TEST_DB }};'
56+
path: ./__testdata__/sql-action.dacpac
57+
action: 'publish'
4758

4859
# Build and publish sqlproj that should create a new view
4960
- name: Test Build and Publish
5061
uses: ./
5162
with:
52-
connection-string: '${{ secrets.AZURE_SQL_CONNECTION_STRING_NO_DATABASE }}Initial Catalog=${{ env.TEST_DB }};'
53-
project-file: ./__testdata__/TestProject/sql-action.sqlproj
63+
connection-string: '${{ secrets[matrix.connection_string_secret] }}Initial Catalog=${{ env.TEST_DB }};'
64+
path: ./__testdata__/TestProject/sql-action.sqlproj
65+
action: 'publish'
5466

55-
# Execute testsql.sql via SQLCMD on server
67+
# Execute testsql.sql via script action on server
5668
- name: Test SQL Action
5769
uses: ./
5870
with:
59-
connection-string: '${{ secrets.AZURE_SQL_CONNECTION_STRING_NO_DATABASE }}Initial Catalog=${{ env.TEST_DB }};'
60-
sql-file: ./__testdata__/testsql.sql
71+
connection-string: '${{ secrets[matrix.connection_string_secret] }}Initial Catalog=${{ env.TEST_DB }};'
72+
path: ./__testdata__/testsql.sql
73+
74+
# Test that go-sqlcmd has been added to runner, this step will be removed in future PR when go-sqlcmd is hooked up
75+
- name: Test go-sqlcmd is setup
76+
run: sqlcmd -?
77+
78+
- name: Set database name for cleanup
79+
if: always()
80+
run: sed 's/$(DbName)/${{ env.TEST_DB }}/' ./__testdata__/cleanup.sql > ${{ runner.temp }}/cleanup.sql
6181

6282
- name: Cleanup Test Database
83+
if: always()
6384
uses: ./
6485
with:
65-
connection-string: '${{ secrets.AZURE_SQL_CONNECTION_STRING_NO_DATABASE }}Initial Catalog=master;'
66-
sql-file: ./__testdata__/cleanup.sql
67-
arguments: '-v DbName="${{ env.TEST_DB }}"'
86+
connection-string: '${{ secrets[matrix.connection_string_secret] }}Initial Catalog=master;'
87+
path: ${{ runner.temp }}/cleanup.sql

README.md

Lines changed: 48 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,23 @@ Get started today with a [free Azure account](https://azure.com/free/open-source
1111
The definition of this GitHub Action is in [action.yml](https://github.com/Azure/sql-action/blob/master/action.yml). Learn more in the [user guide](#📓-user-guide).
1212

1313
```yaml
14-
- uses: azure/sql-action@v1.3
14+
- uses: azure/sql-action@v2
1515
with:
16-
connection-string: # required, connection string incl the database and user authentication information
16+
# required, connection string incl the database and user authentication information
17+
connection-string:
1718

18-
# optional for SQL project deployment - project-file, build-arguments
19-
project-file: # path to a .sqlproj file
20-
build-arguments: # additional arguments applied to dotnet build when building the .sqlproj to .dacpac
19+
# required, path to either a .sql, .dacpac, or .sqlproj file
20+
path:
2121

22-
# optional for dacpac deployment - dacpac-package
23-
dacpac-package: # path to a .dacpac file
22+
# optional when using a .sql script, required otherwise
23+
# sqlpackage action on the .dacpac or .sqlproj file, supported options are: Publish, Script, DeployReport, DriftReport
24+
action:
2425

25-
# optional for SQL scripts deployment - sql-file
26-
sql-file: # path to SQL scripts
26+
# optional additional sqlpackage or go-sqlcmd arguments
27+
arguments:
2728

28-
29-
# optional for all deployments - arguments
30-
arguments: # sqlpackage arguments for .sqlproj or .dacpac deployment or sqlcmd arguments for SQL script deployment
29+
# optional additional dotnet build options when building a database project file
30+
build-arguments:
3131
```
3232
3333
## 🎨 Samples
@@ -45,12 +45,13 @@ jobs:
4545
runs-on: ubuntu-latest
4646
steps:
4747
- uses: actions/checkout@v1
48-
- uses: azure/sql-action@v1.3
48+
- uses: azure/sql-action@v2
4949
with:
5050
connection-string: ${{ secrets.AZURE_SQL_CONNECTION_STRING }}
51-
project-file: './Database.sqlproj'
52-
build-arguments: '-c Release' # Optional arguments passed to dotnet build
53-
arguments: '/p:DropObjectsNotInSource=true' # Optional parameters for SqlPackage Publish
51+
path: './Database.sqlproj'
52+
action: 'publish'
53+
build-arguments: '-c Release' # Optional build options passed to dotnet build
54+
arguments: '/p:DropObjectsNotInSource=true' # Optional properties and parameters for SqlPackage Publish
5455
```
5556
5657
### Deploy SQL scripts to an Azure SQL Database with a temporary firewall rule
@@ -67,10 +68,10 @@ jobs:
6768
- uses: azure/login@v1 # Azure login required to add a temporary firewall rule
6869
with:
6970
creds: ${{ secrets.AZURE_CREDENTIALS }}
70-
- uses: azure/sql-action@v1.3
71+
- uses: azure/sql-action@v2
7172
with:
7273
connection-string: ${{ secrets.AZURE_SQL_CONNECTION_STRING }}
73-
sql-file: './sqlscripts/*.sql'
74+
path: './sqlscripts/*.sql'
7475
```
7576
7677
### Deploy a DACPAC to an Azure SQL database with Allow Azure Services access enabled
@@ -84,27 +85,43 @@ jobs:
8485
runs-on: windows-latest
8586
steps:
8687
- uses: actions/checkout@v1
87-
- uses: azure/sql-action@v1.3
88+
- uses: azure/sql-action@v2
8889
with:
8990
connection-string: ${{ secrets.AZURE_SQL_CONNECTION_STRING }}
90-
dacpac-package: './Database.dacpac'
91-
arguments: '/p:DropObjectsNotInSource=true' # Optional parameters for SqlPackage Publish
91+
path: './Database.dacpac'
92+
action: 'publish'
93+
arguments: '/p:DropObjectsNotInSource=true' # Optional properties parameters for SqlPackage Publish
9294
```
9395
9496
9597
## 📓 User Guide
9698
97-
### Authentication
99+
### Authentication and Connection String
100+
101+
The v1.x version of sql-action supports SQL authentication only in the connection string. Starting in v2, AAD Password, AAD Service Principal, and AAD Default authentications are also supported.
102+
103+
The basic format of a connection string includes a series of keyword/value pairs separated by semicolons. The equal sign (=) connects each keyword and its value. (Ex: Key1=Val1;Key2=Val2) An example connection string template is: `Server=<servername>; User ID=<user_id>; Password=<password>; Initial Catalog=<database>`.
104+
105+
The following rules are to be followed while passing special characters in values:
106+
1. To include values that contain a semicolon, single-quote character, or double-quote character, the value must be enclosed in double quotation marks.
107+
2. If the value contains both a semicolon and a double-quote character, the value can be enclosed in single quotation marks.
108+
3. The single quotation mark is also useful if the value starts with a double-quote character. Conversely, the double quotation mark can be used if the value starts with a single quotation mark.
109+
4. If the value contains both single-quote and double-quote characters, the quotation mark character used to enclose the value must be doubled every time it occurs within the value.
110+
111+
112+
For more information about connection strings, see https://aka.ms/sqlconnectionstring
98113

99-
The v1.x version of sql-action supports SQL authentication only in the connection string.
114+
### Arguments
100115

101-
The `server-name` action YAML key is optional and is only available to provide backward compatibility. It is strongly recommended to put the server name in the connection string as displayed in the examples. The connection string uses this template: `Server=<servername>; User ID=<user_id>; Password=<password>; Initial Catalog=<database>`. In case the server name is put both in the `server-name` and in the `connection-string`, the server name used will be the one specified in the `server-name` YAML key.
116+
sql-action supports passing arguments to SqlPackage, go-sqlcmd, and dotnet build.
117+
- **SqlPackage**: SqlPackage publish properties are passed to the SqlPackage utility from the `arguments` property. More information on these properties is available in the [SqlPackage publish](https://docs.microsoft.com/sql/tools/sqlpackage/sqlpackage-publish#properties-specific-to-the-publish-action) documentation. SqlPackage [parameters](https://docs.microsoft.com/sql/tools/sqlpackage/sqlpackage-publish#parameters-for-the-publish-action) that do not impact the source or target setting are also valid, including `/Profile:` for a publish profile, `/DeployReportPath:` for a deployment report, and `/Variables:` to set SQLCMD variable values.
118+
- **go-sqlcmd**: go-sqlcmd parameters are passed to the go-sqlcmd utility from the `arguments` property. This enables SQLCMD variables `-v` to be passed to scripts as seen in the [sqlcmd documentation](https://docs.microsoft.com/sql/tools/sqlcmd-utility#syntax).
119+
- **dotnet build**: dotnet build options are passed to the SQL project build step from the `build-arguments` property. More information on options is available in the [dotnet build documentation](https://docs.microsoft.com/dotnet/core/tools/dotnet-build#options).
102120

103121
### Environments
104122

105123
sql-action is supported on both Windows and Linux environments. The [default images](https://github.com/actions/virtual-environments) include the prerequisites:
106124

107-
- sqlcmd
108125
- sqlpackage (for sqlproj or dacpac deployment)
109126
- dotnet (for sqlproj build)
110127

@@ -166,10 +183,11 @@ jobs:
166183
runs-on: ubuntu-latest
167184
steps:
168185
- uses: actions/checkout@v1
169-
- uses: azure/sql-action@v1.3
186+
- uses: azure/sql-action@v2
170187
with:
171188
connection-string: ${{ secrets.AZURE_SQL_CONNECTION_STRING }}
172-
project-file: './Database.sqlproj'
189+
path: './Database.sqlproj'
190+
action: 'publish'
173191
```
174192
3. Place the connection string from the Azure Portal in GitHub secrets as `AZURE_SQL_CONNECTION_STRING`. Connection string format is: `Server=<server.database.windows.net>;User ID=<user>;Password=<password>;Initial Catalog=<database>`.
175193
4. Copy the below SQL project template and paste the content in your project repository as `Database.sqlproj`.
@@ -218,10 +236,11 @@ jobs:
218236
runs-on: ubuntu-latest
219237
steps:
220238
- uses: actions/checkout@v1
221-
- uses: azure/sql-action@v1.3
239+
- uses: azure/sql-action@v2
222240
with:
223241
connection-string: ${{ secrets.AZURE_SQL_CONNECTION_STRING }}
224-
dacpac-package: './PreviousDatabase.dacpac'
242+
path: './PreviousDatabase.dacpac'
243+
action: 'publish'
225244
```
226245
4. Place the connection string from the Azure Portal in GitHub secrets as `AZURE_SQL_CONNECTION_STRING`. Connection string format is: `Server=<server.database.windows.net>;User ID=<user>;Password=<password>;Initial Catalog=<database>`.
227246
5. Commit and push your project to GitHub repository, you should see a new GitHub Action initiated in **Actions** tab.

__testdata__/TestProject/sql-action.sqlproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
<Name>sql-action</Name>
66
<DSP>Microsoft.Data.Tools.Schema.Sql.SqlAzureV12DatabaseSchemaProvider</DSP>
77
<ModelCollation>1033, CI</ModelCollation>
8+
<ProjectGuid>{829ec500-6bb6-42c3-a1ab-ab621099d153}</ProjectGuid>
89
</PropertyGroup>
910
</Project>

__testdata__/testsql.sql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
-- This script is used by pr-check.yml to test the SQLCMD action
1+
-- This script is used by pr-check.yml to test the script action
22

33
-- This should successfully insert data into the table created in the DACPAC step
4-
INSERT INTO [Table1] VALUES(1, 'test');
4+
INSERT INTO [dbo].[Table1] VALUES(1, 'test');
55

66
-- This should successfully SELECT from the view created by the sqlproj
7-
SELECT * FROM [View1];
7+
SELECT * FROM [dbo].[View1];

0 commit comments

Comments
 (0)