Thursday, 27 October 2022

Securing secrets with Azure Key Vault for GitHub Actions

 If you are following me, I have published two articles before, about CI/CD with GitHub Actions to deploy Application to Azure App Service and CI/CD with GitHub Actions to deploy Application to Azure Kubernetes Cluster.

  1. First thing we would need is, Connectivity to Azure so that pipeline can do the azure login and for this purpose I suggest always to use service principal instead of user id & password. This is the only settings which you need to store as part of GitHub Secrets so that using this you can do the Azure login.
    Here is command to generate the service principal .
az ad sp create-for-rbac --name "{your_serviceprincipal_name}" --scope /subscriptions/{subscription_id}/resourceGroups/{resourceGroupName} --role Contributor --sdk-auth
steps:
- uses: actions/checkout@v2
- uses: Azure/login@v1
with:
creds: ${{ secrets.YourServicePrincipal }}
- uses: Azure/get-keyvault-secrets@v1
with:
keyvault: "{Your_KeyVaultName}"
secrets: 'CONNECTIONSTRING'
id: azKeyVaultSecretAction
- name: Replace token for appsettings.Production.json
uses: cschleiden/replace-tokens@v1.1
with:
files: '["src/MyDemoApp/appsettings.Production.json"]'
env:
ConnectionString: ${{ steps.azKeyVaultSecretAction.outputs.CONNECTIONSTRING }}
secrets: 'CONNECTIONSTRING, OTHERSECRETS1, OTHERSECRETS2'

No comments:

Post a Comment