Skip to content

Commit c851f51

Browse files
authored
Merge pull request #216565 from juliakm/users/jukullam/update-postgres-auth
Update PostgreSQL GitHub Actions article to have Open ID Connect option
2 parents 17b55ab + b4b48f6 commit c851f51

File tree

1 file changed

+75
-50
lines changed

1 file changed

+75
-50
lines changed

articles/postgresql/single-server/how-to-deploy-github-action.md

Lines changed: 75 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -35,35 +35,12 @@ The file has two sections:
3535

3636
|Section |Tasks |
3737
|---------|---------|
38-
|**Authentication** | 1. Define a service principal. <br /> 2. Create a GitHub secret. |
38+
|**Authentication** | 1. Generate deployment credentials. |
3939
|**Deploy** | 1. Deploy the database. |
4040

4141
## Generate deployment credentials
4242

43-
You can create a [service principal](../../active-directory/develop/app-objects-and-service-principals.md) with the [az ad sp create-for-rbac](/cli/azure/ad/sp#az-ad-sp-create-for-rbac&preserve-view=true) command in the [Azure CLI](/cli/azure/). Run this command with [Azure Cloud Shell](https://shell.azure.com/) in the Azure portal or by selecting the **Try it** button.
44-
45-
Replace the placeholders `server-name` with the name of your PostgreSQL server hosted on Azure. Replace the `subscription-id` and `resource-group` with the subscription ID and resource group connected to your PostgreSQL server.
46-
47-
```azurecli-interactive
48-
az ad sp create-for-rbac --name {server-name} --role contributor \
49-
--scopes /subscriptions/{subscription-id}/resourceGroups/{resource-group} \
50-
--sdk-auth
51-
```
52-
53-
The output is a JSON object with the role assignment credentials that provide access to your database similar to below. Copy this output JSON object for later.
54-
55-
```output
56-
{
57-
"clientId": "<GUID>",
58-
"clientSecret": "<GUID>",
59-
"subscriptionId": "<GUID>",
60-
"tenantId": "<GUID>",
61-
(...)
62-
}
63-
```
64-
65-
> [!IMPORTANT]
66-
> It is always a good practice to grant minimum access. The scope in the previous example is limited to the specific server and not the entire resource group.
43+
[!INCLUDE [include](~/articles/reusable-content/github-actions/generate-deployment-credentials.md)]
6744

6845
## Copy the PostgreSQL connection string
6946

@@ -81,23 +58,7 @@ You will use the connection string as a GitHub secret.
8158

8259
## Configure the GitHub secrets
8360

84-
1. In [GitHub](https://github.com/), browse your repository.
85-
86-
1. Select **Settings > Secrets > New secret**.
87-
88-
1. Paste the entire JSON output from the Azure CLI command into the secret's value field. Give the secret the name `AZURE_CREDENTIALS`.
89-
90-
When you configure the workflow file later, you use the secret for the input `creds` of the Azure Login action. For example:
91-
92-
```yaml
93-
- uses: azure/login@v1
94-
with:
95-
creds: ${{ secrets.AZURE_CREDENTIALS }}
96-
```
97-
98-
1. Select **New secret** again.
99-
100-
1. Paste the connection string value into the secret's value field. Give the secret the name `AZURE_POSTGRESQL_CONNECTION_STRING`.
61+
[!INCLUDE [include](~/articles/reusable-content/github-actions/create-secrets-with-openid.md)]
10162

10263
## Add your workflow
10364

@@ -112,21 +73,23 @@ You will use the connection string as a GitHub secret.
11273

11374
on:
11475
push:
115-
branches: [ master ]
76+
branches: [ main ]
11677
pull_request:
117-
branches: [ master ]
78+
branches: [ main ]
11879
```
11980
120-
1. Rename your workflow `PostgreSQL for GitHub Actions` and add the checkout and login actions. These actions will checkout your site code and authenticate with Azure using the `AZURE_CREDENTIALS` GitHub secret you created earlier.
81+
1. Rename your workflow `PostgreSQL for GitHub Actions` and add the checkout and login actions. These actions will checkout your site code and authenticate with Azure using the GitHub secret(s) you created earlier.
82+
83+
# [Service principal](#tab/userlevel)
12184

12285
```yaml
12386
name: PostgreSQL for GitHub Actions
12487
12588
on:
12689
push:
127-
branches: [ master ]
90+
branches: [ main ]
12891
pull_request:
129-
branches: [ master ]
92+
branches: [ main ]
13093
13194
jobs:
13295
build:
@@ -137,6 +100,29 @@ You will use the connection string as a GitHub secret.
137100
with:
138101
creds: ${{ secrets.AZURE_CREDENTIALS }}
139102
```
103+
# [OpenID Connect](#tab/openid)
104+
105+
```yaml
106+
name: PostgreSQL for GitHub Actions
107+
108+
on:
109+
push:
110+
branches: [ main ]
111+
pull_request:
112+
branches: [ main ]
113+
114+
jobs:
115+
build:
116+
runs-on: ubuntu-latest
117+
steps:
118+
- uses: actions/checkout@v1
119+
- uses: azure/login@v1
120+
with:
121+
client-id: ${{ secrets.AZURE_CLIENT_ID }}
122+
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
123+
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
124+
```
125+
---
140126

141127
2. Use the Azure PostgreSQL Deploy action to connect to your PostgreSQL instance. Replace `POSTGRESQL_SERVER_NAME` with the name of your server. You should have a PostgreSQL data file named `data.sql` at the root level of your repository.
142128

@@ -150,14 +136,16 @@ You will use the connection string as a GitHub secret.
150136

151137
3. Complete your workflow by adding an action to logout of Azure. Here is the completed workflow. The file will appear in the `.github/workflows` folder of your repository.
152138

139+
# [Service principal](#tab/userlevel)
140+
153141
```yaml
154142
name: PostgreSQL for GitHub Actions
155143
156144
on:
157145
push:
158-
branches: [ master ]
146+
branches: [ main ]
159147
pull_request:
160-
branches: [ master ]
148+
branches: [ main ]
161149
162150
163151
jobs:
@@ -167,7 +155,42 @@ You will use the connection string as a GitHub secret.
167155
- uses: actions/checkout@v1
168156
- uses: azure/login@v1
169157
with:
170-
creds: ${{ secrets.AZURE_CREDENTIALS }}
158+
client-id: ${{ secrets.AZURE_CREDENTIALS }}
159+
160+
- uses: azure/postgresql@v1
161+
with:
162+
server-name: POSTGRESQL_SERVER_NAME
163+
connection-string: ${{ secrets.AZURE_POSTGRESQL_CONNECTION_STRING }}
164+
sql-file: './data.sql'
165+
166+
# Azure logout
167+
- name: logout
168+
run: |
169+
az logout
170+
```
171+
172+
# [OpenID Connect](#tab/openid)
173+
174+
```yaml
175+
name: PostgreSQL for GitHub Actions
176+
177+
on:
178+
push:
179+
branches: [ main ]
180+
pull_request:
181+
branches: [ main ]
182+
183+
184+
jobs:
185+
build:
186+
runs-on: ubuntu-latest
187+
steps:
188+
- uses: actions/checkout@v1
189+
- uses: azure/login@v1
190+
with:
191+
client-id: ${{ secrets.AZURE_CLIENT_ID }}
192+
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
193+
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
171194
172195
- uses: azure/postgresql@v1
173196
with:
@@ -180,6 +203,8 @@ You will use the connection string as a GitHub secret.
180203
run: |
181204
az logout
182205
```
206+
---
207+
183208

184209
## Review your deployment
185210

0 commit comments

Comments
 (0)