DEV Community

Alan Castro
Alan Castro

Posted on • Updated on

Gitlab CI script to deploy a Azure Function

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

Top comments (9)

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
 
derkcrezee profile image
Derk Crezee

Thanks for the info. Might be useful to have a look at the available docker images. The core-tools versions contain the azure cli and azure function core tools.

Collapse
 
alandecastros profile image
Alan Castro

Thanks! I'll update the article asap

Collapse
 
arturoaviles profile image
Arturo Avilés • Edited

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

Nice! I'll update the post! Thanks!

Collapse
 
ablob profile image
a blob

Pardon my ignorance, I'm new to all this and have a lot to learn.
With that being said, it looks to me, like you run the curl command and then you try to install curl.
If this is not correct can someone explain what is actually happening here?

Collapse
 
oskarspakers profile image
Oskars

Thanks for the article, it helped a lot.
Here`s my sample with update dependencies dotnet 6, nodejs 16 and azure function core tools 4


deploy_prod:
  stage: deploy
  image: mcr.microsoft.com/dotnet/sdk:6.0
  script:
    - curl -sL https://aka.ms/InstallAzureCLIDeb | bash
    - apt-get install curl && curl -sL https://deb.nodesource.com/setup_16.x | bash -
    - apt-get install nodejs
    - npm install -g azure-functions-core-tools@4 --unsafe-perm true
    - az login --service-principal -u $APPLICATION_ID -p $APPLICATION_SECRET --tenant $TENANT_ID
    - func azure functionapp publish $FUNCTION_APP --nodejs    
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

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