Skip to content

Commit 2a97a13

Browse files
authored
Merge pull request #113563 from nalogan/patch-7
Added code samples for strict sync
2 parents 21ebaf3 + 22cb396 commit 2a97a13

File tree

1 file changed

+140
-16
lines changed

1 file changed

+140
-16
lines changed

articles/azure-app-configuration/concept-github-action.md

Lines changed: 140 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,8 @@ jobs:
5757
separator: ':'
5858
```
5959

60-
## Use a dynamic label on sync
61-
The previous action updates the App Configuration instance whenever `appsettings.json` is updated. This action inserts a dynamic label on each sync, ensuring that each sync can be uniquely identified and allowing code changes to be mapped to config changes.
62-
63-
The first section of this workflow specifies that the action triggers *on* a *push* containing `appsettings.json` to the *master* branch. The second section runs a job that creates a unique label for the config update based on the commit hash. The job then updates the App Configuration instance with the new values and the unique label for this update.
60+
## Use strict sync
61+
By default the GitHub action does not enable strict mode, meaning that the sync will only add key-values from the configuration file to the App Configuration instance (no key-value pairs will be deleted). Enabling strict mode will mean key-value pairs that aren't in the configuration file are deleted from the App Configuration instance, so that it matches the configuration file. If you are syncing from multiple sources or using Azure Key Vault with App Configuration, you'll want to use different prefixes or labels with strict sync to avoid wiping out configuration settings from other files (see samples below).
6462

6563
```json
6664
on:
@@ -74,10 +72,6 @@ jobs:
7472
syncconfig:
7573
runs-on: ubuntu-latest
7674
steps:
77-
# Creates a label based on the branch name and the first 8 characters
78-
# of the commit hash
79-
- id: determine_label
80-
run: echo ::set-output name=LABEL::"${GITHUB_REF#refs/*/}/${GITHUB_SHA:0:8}"
8175
# checkout done so that files in the repo can be read by the sync
8276
- uses: actions/checkout@v1
8377
- uses: azure/appconfiguration-sync@v1
@@ -88,13 +82,98 @@ jobs:
8882
# repository
8983
connectionString: ${{ secrets.<ConnectionString> }}
9084
separator: ':'
91-
label: ${{ steps.determine_label.outputs.LABEL }}
85+
label: 'Label'
86+
prefix: 'Prefix:'
87+
strict: true
9288
```
89+
## Sync multiple files in one action
9390

94-
## Use strict sync
95-
When strict mode is enabled, the sync ensures that the App Configuration instance matches the configuration file for the given prefix and label exactly. Key-value pairs with the same prefix and label that aren't in the configuration file are deleted.
96-
97-
If strict mode isn't enabled, the sync will only set key-values from the configuration file. No key-value pairs will be deleted.
91+
If your configuration is in multiple files, you can use the pattern below to trigger a sync when either file is modified. This pattern uses the glob library https://www.npmjs.com/package/glob
92+
93+
```json
94+
on:
95+
push:
96+
branches:
97+
- 'master'
98+
paths:
99+
- 'appsettings.json'
100+
- 'appsettings2.json'
101+
102+
jobs:
103+
syncconfig:
104+
runs-on: ubuntu-latest
105+
steps:
106+
# checkout done so that files in the repo can be read by the sync
107+
- uses: actions/checkout@v1
108+
- uses: azure/appconfiguration-sync@v1
109+
with:
110+
configurationFile: '{appsettings.json,appsettings2.json}'
111+
format: 'json'
112+
# Replace <ConnectionString> with the name of the secret in your repository
113+
connectionString: ${{ secrets.<ConnectionString> }}
114+
separator: ':'
115+
```
116+
117+
## Sync by prefix or label
118+
Specifying prefixes or labels in your sync action will sync only that particular set. This is important for using strict sync with multiple files. Depending on how the configuration is set up, either a prefix or a label can be associated with each file and then each prefix or label can be synced separately so that nothing is overwritten. Typically prefixes are used for different applications or services and labels are used for different environments.
119+
120+
Sync by prefix:
121+
122+
```json
123+
on:
124+
push:
125+
branches:
126+
- 'master'
127+
paths:
128+
- 'appsettings.json'
129+
130+
jobs:
131+
syncconfig:
132+
runs-on: ubuntu-latest
133+
steps:
134+
# checkout done so that files in the repo can be read by the sync
135+
- uses: actions/checkout@v1
136+
- uses: azure/appconfiguration-sync@v1
137+
with:
138+
configurationFile: 'appsettings.json'
139+
format: 'json'
140+
# Replace <ConnectionString> with the name of the secret in your repository
141+
connectionString: ${{ secrets.<ConnectionString> }}
142+
separator: ':'
143+
prefix: 'Prefix::'
144+
```
145+
146+
Sync by label:
147+
148+
```json
149+
on:
150+
push:
151+
branches:
152+
- 'master'
153+
paths:
154+
- 'appsettings.json'
155+
156+
jobs:
157+
syncconfig:
158+
runs-on: ubuntu-latest
159+
steps:
160+
# checkout done so that files in the repo can be read by the sync
161+
- uses: actions/checkout@v1
162+
- uses: azure/appconfiguration-sync@v1
163+
with:
164+
configurationFile: 'appsettings.json'
165+
format: 'json'
166+
# Replace <ConnectionString> with the name of the secret in your repository
167+
connectionString: ${{ secrets.<ConnectionString> }}
168+
separator: ':'
169+
label: 'Label'
170+
171+
```
172+
173+
## Use a dynamic label on sync
174+
The following action inserts a dynamic label on each sync, ensuring that each sync can be uniquely identified and allowing code changes to be mapped to config changes.
175+
176+
The first section of this workflow specifies that the action triggers *on* a *push* containing `appsettings.json` to the *master* branch. The second section runs a job that creates a unique label for the config update based on the commit hash. The job then updates the App Configuration instance with the new values and the unique label for this update.
98177

99178
```json
100179
on:
@@ -108,6 +187,10 @@ jobs:
108187
syncconfig:
109188
runs-on: ubuntu-latest
110189
steps:
190+
# Creates a label based on the branch name and the first 8 characters
191+
# of the commit hash
192+
- id: determine_label
193+
run: echo ::set-output name=LABEL::"${GITHUB_REF#refs/*/}/${GITHUB_SHA:0:8}"
111194
# checkout done so that files in the repo can be read by the sync
112195
- uses: actions/checkout@v1
113196
- uses: azure/appconfiguration-sync@v1
@@ -118,9 +201,50 @@ jobs:
118201
# repository
119202
connectionString: ${{ secrets.<ConnectionString> }}
120203
separator: ':'
121-
label: 'Label'
122-
prefix: 'Prefix:'
123-
strict: true
204+
label: ${{ steps.determine_label.outputs.LABEL }}
205+
```
206+
207+
## Use Azure Key Vault with GitHub Action
208+
Developers using Azure Key Vault with AppConfiguration should use two separate files, typically an appsettings.json and a secretreferences.json. The secretreferences.json will contain the url to the key vault secret.
209+
210+
{
211+
"mySecret": "{\"uri\":\"https://myKeyVault.vault.azure.net/secrets/mySecret"}"
212+
}
213+
214+
The GitHub Action can then be configured to do a strict sync on the appsettings.json, followed by a non-strict sync on secretreferences.json. The following sample will trigger a sync when either file is updated:
215+
216+
```json
217+
on:
218+
push:
219+
branches:
220+
- 'master'
221+
paths:
222+
- 'appsettings.json'
223+
- 'secretreferences.json'
224+
225+
jobs:
226+
syncconfig:
227+
runs-on: ubuntu-latest
228+
steps:
229+
# checkout done so that files in the repo can be read by the sync
230+
- uses: actions/checkout@v1
231+
- uses: azure/appconfiguration-sync@v1
232+
with:
233+
configurationFile: 'appsettings.json'
234+
format: 'json'
235+
# Replace <ConnectionString> with the name of the secret in your repository
236+
connectionString: ${{ secrets.<ConnectionString> }}
237+
separator: ':'
238+
strict: true
239+
- uses: azure/appconfiguration-sync@v1
240+
with:
241+
configurationFile: 'secretreferences.json'
242+
format: 'json'
243+
# Replace <ConnectionString> with the name of the secret in your repository
244+
connectionString: ${{ secrets.<ConnectionString> }}
245+
separator: ':'
246+
contentType: 'application/vnd.microsoft.appconfig.keyvaultref+json;charset=utf-8'
247+
124248
```
125249

126250
## Use max depth to limit GitHub Action

0 commit comments

Comments
 (0)