Uw GitHub-opslagplaats synchroniseren met App Configuration
Teams die hun bestaande procedures voor broncodebeheer willen blijven gebruiken, kunnen GitHub Actions gebruiken om hun GitHub-opslagplaats automatisch te synchroniseren met hun App Configuration Store. Op deze manier kunt u wijzigingen aanbrengen in uw configuratiebestanden zoals u gewend bent, terwijl u App Configuration voordelen krijgt, zoals:
• Gecentraliseerde configuratie buiten uw code
• Configuratie bijwerken zonder uw hele app opnieuw te implementeren
• Integratie met services zoals Azure App Service en Functions.
Een GitHub Actions werkstroom definieert een geautomatiseerd proces in een GitHub-opslagplaats. De Azure App Configuration synchronisatieactie activeert updates voor een App Configuration-exemplaar wanneer er wijzigingen worden aangebracht in de bronopslagplaats. Er wordt een YAML-bestand (.yml) in het /.github/workflows/
pad van uw opslagplaats gebruikt om de stappen en parameters te definiëren. U kunt configuratie-updates activeren bij het pushen, controleren of vertakken van app-configuratiebestanden, net als bij app-code.
De GitHub-documentatie biedt een uitgebreid overzicht van GitHub-werkstromen en -acties.
GitHub Actions inschakelen in uw opslagplaats
Als u deze GitHub Action wilt gaan gebruiken, gaat u naar uw opslagplaats en selecteert u het tabblad Acties . Selecteer Nieuwe werkstroom en vervolgens Zelf een werkstroom instellen. Zoek ten slotte in marketplace naar 'Azure App Configuration Sync'.
Configuratiebestanden synchroniseren na een push
Met deze actie worden Azure App Configuration bestanden gesynchroniseerd wanneer een wijziging wordt gepusht naar appsettings.json
. Wanneer een ontwikkelaar een wijziging pusht naar appsettings.json
, werkt de actie App Configuration Synchroniseren het App Configuration-exemplaar bij met de nieuwe waarden.
De eerste sectie van deze werkstroom geeft aan dat de actie wordt geactiveerd bij een push die appsettings.json
naar de hoofdbranch gaat. De tweede sectie bevat de taken die worden uitgevoerd zodra de actie is geactiveerd. De actie controleert de relevante bestanden en werkt het App Configuration-exemplaar bij met behulp van de connection string die zijn opgeslagen als geheim in de opslagplaats. Zie het artikel van GitHub over het maken en gebruiken van versleutelde geheimen voor meer informatie over het gebruik van geheimen in GitHub.
on:
push:
branches:
- 'main'
paths:
- 'appsettings.json'
jobs:
syncconfig:
runs-on: ubuntu-latest
steps:
# checkout done so that files in the repo can be read by the sync
- uses: actions/checkout@v1
- uses: azure/appconfiguration-sync@v1
with:
configurationFile: 'appsettings.json'
format: 'json'
# Replace <ConnectionString> with the name of the secret in your
# repository
connectionString: ${{ secrets.<ConnectionString> }}
separator: ':'
Strikte synchronisatie gebruiken
Standaard schakelt de GitHub Action de strikte modus niet in, wat betekent dat de synchronisatie alleen sleutelwaarden uit het configuratiebestand toevoegt aan het App Configuration exemplaar (er worden geen sleutel-waardeparen verwijderd). Als u de strikte modus inschakelt, worden sleutel-waardeparen die zich niet in het configuratiebestand bevinden, verwijderd uit het App Configuration-exemplaar, zodat deze overeenkomt met het configuratiebestand. Als u vanuit meerdere bronnen synchroniseert of Azure Key Vault gebruikt met App Configuration, kunt u het beste verschillende voorvoegsels of labels gebruiken met strikte synchronisatie om te voorkomen dat configuratie-instellingen uit andere bestanden worden verwijderd (zie de voorbeelden hieronder).
on:
push:
branches:
- 'main'
paths:
- 'appsettings.json'
jobs:
syncconfig:
runs-on: ubuntu-latest
steps:
# checkout done so that files in the repo can be read by the sync
- uses: actions/checkout@v1
- uses: azure/appconfiguration-sync@v1
with:
configurationFile: 'appsettings.json'
format: 'json'
# Replace <ConnectionString> with the name of the secret in your
# repository
connectionString: ${{ secrets.<ConnectionString> }}
separator: ':'
label: 'Label'
prefix: 'Prefix:'
strict: true
Meerdere bestanden in één actie synchroniseren
Als uw configuratie zich in meerdere bestanden bevindt, kunt u het onderstaande patroon gebruiken om een synchronisatie te activeren wanneer een van beide bestanden wordt gewijzigd. Dit patroon maakt gebruik van de glob-bibliotheek https://www.npmjs.com/package/glob . Als de naam van uw configuratiebestand een komma bevat, kunt u een backslash gebruiken om de komma te laten ontsnappen.
on:
push:
branches:
- 'main'
paths:
- 'appsettings.json'
- 'appsettings2.json'
jobs:
syncconfig:
runs-on: ubuntu-latest
steps:
# checkout done so that files in the repo can be read by the sync
- uses: actions/checkout@v1
- uses: azure/appconfiguration-sync@v1
with:
configurationFile: '{appsettings.json,appsettings2.json}'
format: 'json'
# Replace <ConnectionString> with the name of the secret in your repository
connectionString: ${{ secrets.<ConnectionString> }}
separator: ':'
Synchroniseren op voorvoegsel of label
Als u voorvoegsels of labels in uw synchronisatieactie opgeeft, wordt alleen die specifieke set gesynchroniseerd. Dit is belangrijk voor het gebruik van strikte synchronisatie met meerdere bestanden. Afhankelijk van hoe de configuratie is ingesteld, kan aan elk bestand een voorvoegsel of een label worden gekoppeld. Vervolgens kan elk voorvoegsel of label afzonderlijk worden gesynchroniseerd, zodat er niets wordt overschreven. Doorgaans worden voorvoegsels gebruikt voor verschillende toepassingen of services en labels worden gebruikt voor verschillende omgevingen.
Synchroniseren op voorvoegsel:
on:
push:
branches:
- 'main'
paths:
- 'appsettings.json'
jobs:
syncconfig:
runs-on: ubuntu-latest
steps:
# checkout done so that files in the repo can be read by the sync
- uses: actions/checkout@v1
- uses: azure/appconfiguration-sync@v1
with:
configurationFile: 'appsettings.json'
format: 'json'
# Replace <ConnectionString> with the name of the secret in your repository
connectionString: ${{ secrets.<ConnectionString> }}
separator: ':'
prefix: 'Prefix::'
Synchroniseren op label:
on:
push:
branches:
- 'main'
paths:
- 'appsettings.json'
jobs:
syncconfig:
runs-on: ubuntu-latest
steps:
# checkout done so that files in the repo can be read by the sync
- uses: actions/checkout@v1
- uses: azure/appconfiguration-sync@v1
with:
configurationFile: 'appsettings.json'
format: 'json'
# Replace <ConnectionString> with the name of the secret in your repository
connectionString: ${{ secrets.<ConnectionString> }}
separator: ':'
label: 'Label'
Een dynamisch label gebruiken bij synchronisatie
Met de volgende actie wordt een dynamisch label voor elke synchronisatie ingevoegd, zodat elke synchronisatie uniek kan worden geïdentificeerd en codewijzigingen kunnen worden toegewezen aan configuratiewijzigingen.
De eerste sectie van deze werkstroom geeft aan dat de actie wordt geactiveerd bij een push die appsettings.json
naar de hoofdbranch gaat. In de tweede sectie wordt een taak uitgevoerd waarmee een uniek label voor de configuratie-update wordt gemaakt op basis van de doorvoer-hash. De taak werkt vervolgens het App Configuration-exemplaar bij met de nieuwe waarden en het unieke label voor deze update.
on:
push:
branches:
- 'main'
paths:
- 'appsettings.json'
jobs:
syncconfig:
runs-on: ubuntu-latest
steps:
# Creates a label based on the branch name and the first 8 characters
# of the commit hash
- id: determine_label
run: echo ::set-output name=LABEL::"${GITHUB_REF#refs/*/}/${GITHUB_SHA:0:8}"
# checkout done so that files in the repo can be read by the sync
- uses: actions/checkout@v1
- uses: azure/appconfiguration-sync@v1
with:
configurationFile: 'appsettings.json'
format: 'json'
# Replace <ConnectionString> with the name of the secret in your
# repository
connectionString: ${{ secrets.<ConnectionString> }}
separator: ':'
label: ${{ steps.determine_label.outputs.LABEL }}
Azure Key Vault gebruiken met GitHub Action
Ontwikkelaars die Azure Key Vault met AppConfiguration gebruiken, moeten twee afzonderlijke bestanden gebruiken, meestal appsettings.json en secretreferences.json. Secretreferences.json bevat de URL naar het sleutelkluisgeheim.
{ "mySecret": "{"uri":"https://myKeyVault.vault.azure.net/secrets/mySecret"}" }
De GitHub Action kan vervolgens worden geconfigureerd om een strikte synchronisatie uit te voeren op de appsettings.json, gevolgd door een niet-strikte synchronisatie op secretreferences.json. In het volgende voorbeeld wordt een synchronisatie geactiveerd wanneer een van de bestanden wordt bijgewerkt:
on:
push:
branches:
- 'main'
paths:
- 'appsettings.json'
- 'secretreferences.json'
jobs:
syncconfig:
runs-on: ubuntu-latest
steps:
# checkout done so that files in the repo can be read by the sync
- uses: actions/checkout@v1
- uses: azure/appconfiguration-sync@v1
with:
configurationFile: 'appsettings.json'
format: 'json'
# Replace <ConnectionString> with the name of the secret in your repository
connectionString: ${{ secrets.<ConnectionString> }}
separator: ':'
strict: true
- uses: azure/appconfiguration-sync@v1
with:
configurationFile: 'secretreferences.json'
format: 'json'
# Replace <ConnectionString> with the name of the secret in your repository
connectionString: ${{ secrets.<ConnectionString> }}
separator: ':'
contentType: 'application/vnd.microsoft.appconfig.keyvaultref+json;charset=utf-8'
Maximale diepte gebruiken om GitHub Action te beperken
Het standaardgedrag voor geneste JSON-kenmerken is om het hele object plat te maken. De onderstaande JSON definieert dit sleutel-waardepaar:
Sleutel | Waarde |
---|---|
Object:Inner:InnerKey | InnerValue |
{ "Object":
{ "Inner":
{
"InnerKey": "InnerValue"
}
}
}
Als het geneste object is bedoeld als de waarde die naar het configuratie-exemplaar wordt gepusht, kunt u de dieptewaarde gebruiken om het afvlakken op de juiste diepte te stoppen.
on:
push:
branches:
- 'main'
paths:
- 'appsettings.json'
jobs:
syncconfig:
runs-on: ubuntu-latest
steps:
# checkout done so that files in the repo can be read by the sync
- uses: actions/checkout@v1
- uses: azure/appconfiguration-sync@v1
with:
configurationFile: 'appsettings.json'
format: 'json'
# Replace <ConnectionString> with the name of the secret in your
# repository
connectionString: ${{ secrets.<ConnectionString> }}
separator: ':'
depth: 2
Met een diepte van 2 retourneert het bovenstaande voorbeeld nu het volgende sleutel-waardepaar:
Sleutel | Waarde |
---|---|
Object:Binnenste | {"InnerKey":"InnerValue"} |
Informatie over actie-invoer
Invoerparameters geven gegevens op die tijdens runtime door de actie worden gebruikt. De volgende tabel bevat invoerparameters die worden geaccepteerd door App Configuration Sync en de verwachte waarden voor elke. Zie de documentatie van GitHub voor meer informatie over actie-invoer voor GitHub Actions.
Notitie
Invoer-id's zijn niet hoofdlettergevoelig.
Invoernaam | Vereist? | Waarde |
---|---|---|
configurationFile | Yes | Relatief pad naar het configuratiebestand in de opslagplaats. Glob-patronen worden ondersteund en kunnen meerdere bestanden bevatten. |
indeling | Yes | Bestandsindeling van het configuratiebestand. Geldige notaties zijn: JSON, YAML, eigenschappen. |
connectionString | Yes | Lees-schrijf-connection string voor het App Configuration-exemplaar. De connection string moet als geheim worden opgeslagen in de GitHub-opslagplaats en alleen de geheime naam mag in de werkstroom worden gebruikt. |
scheidingsteken | Yes | Scheidingsteken dat wordt gebruikt bij het afvlakken van het configuratiebestand naar sleutel-waardeparen. Geldige waarden zijn: . , ; : - _ __ / |
Voorvoegsel | No | Voorvoegsel dat moet worden toegevoegd aan het begin van sleutels. |
label | No | Label dat wordt gebruikt bij het instellen van sleutel-waardeparen. Als dit niet wordt opgegeven, wordt een null-label gebruikt. |
Strikte | No | Een booleaanse waarde die bepaalt of de strikte modus is ingeschakeld. De standaardwaarde is false. |
Diepte | No | Maximale diepte voor het afvlakken van het configuratiebestand. Diepte moet een positief getal zijn. De standaardwaarde heeft geen maximale diepte. |
tags | No | Hiermee geeft u de tag die is ingesteld op sleutel-waardeparen. De verwachte indeling is een tekenreeksvorm van een JSON-object met de volgende shape: { [propertyName: string]: string; } Elke eigenschapsnaamwaarde wordt een tag. |
Volgende stappen
In dit artikel hebt u geleerd over de App Configuration GitHub-actie synchroniseren en hoe deze kan worden gebruikt om updates voor uw App Configuration-exemplaar te automatiseren. Als u wilt weten hoe Azure App Configuration reageert op wijzigingen in sleutel-waardeparen, gaat u verder met het volgende artikel.