loading...

Gitlab CI script to deploy a Azure Function

alandecastros profile image Alan Castro Updated on ・1 min read

Here is the .gitlab-ci.yml that I use to deploy my .NET Core 3.1 azure functions in Gitlab CI/CD.

Variables that I use in the scripts:

  • $APPLICATION_ID
  • $APPLICATION_SECRET
  • $TENANT_ID
  • $FUNCTION_APP your function app's name in azure
stages:
  - deploy

deploy:
  stage: deploy
  image: mcr.microsoft.com/dotnet/core/sdk:3.1
  script:
    - curl -sL https://aka.ms/InstallAzureCLIDeb | bash
    - apt-get install curl && curl -sL https://deb.nodesource.com/setup_12.x | bash -
    - apt-get install nodejs
    - npm install -g azure-functions-core-tools@3 --unsafe-perm true
    - az login --service-principal -u $APPLICATION_ID -p $APPLICATION_SECRET --tenant $TENANT_ID
    - func azure functionapp publish $FUNCTION_APP --csharp
  only:
    - master
Enter fullscreen mode Exit fullscreen mode

Hope it's useful

Discussion

pic
Editor guide
Collapse
alexsorokoletov profile image
Alex Sorokoletov

Thank you for the setup, it worked like a charm.
I had to figure out how to create service principal correctly but it boiled down to 3 things:

  1. Install Azure CLI and login
  2. Select correct subscription
  3. az ad sp create-for-rbac --name YourPrincipalName

For some reason, YAML formatting goes sideways when you copy the snipped so had to use Gitlab CI lint.

Thanks again, this saved me a lot of time.

Collapse
arturoaviles profile image
Arturo Avilés

Very useful, I have been looking for this ci/cd config file for a long time ago, thanks!

I would recommend to use a Service Principal to Login to Azure to deploy with CI/CD:

az login --service-principal -u $AZ_SERVICE_PRINCIPAL_URL -p $AZ_SERVICE_PRINCIPAL_PASSWORD --tenant $AZ_SERVICE_PRINCIPAL_TENANT
Collapse
alandecastros profile image
Alan Castro Author

Nice! I'll update the post! Thanks!

Collapse
thbm profile image
Thibault Barolat-Massole

Hi, thank you for sharing.
What permissions should the service principal have?

Collapse
alandecastros profile image
Alan Castro Author

Sorry for the long delay, but I usually add as a contributor