Deploy Azure Functions with Jenkins
Tags: serverless, azure, functions, jenkins
Published: 2020-06-01 18:07:08

There are multiple ways to continuously deploy the latest code to Azure Functions. 

Since at [OpenAnalytics](https://www.openanalytics.eu/) we mostly use Jenkins for CI/CD purposes, my preference was to stick to Jenkins. I tried multiple options and the one which worked perfectly on Azure Functions with Linux platform was to do zip deploy using restful API. 

The idea is to first create the zip out of source code and then deploy it:

`curl -X POST https://<user>:<password>@<function-app>.scm.azurewebsites.net/api/zipdeploy -T <zipfile> `

Here's how you can do it:

- Go to your function app on the azure portal then open properties from the sidebar. You should find the **Deployment Trigger URL** there and that's what you need. it should look something like this:
```
https://$<your-functionapp>:<your-functionapp-password><your-functionapp>@.scm.azurewebsites.net/deploy
```

- Go to Jenkins > Credentials > Global Credentials and create global credentials.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/4qumzp0xqpg03l1yz5t4.png)
 
*Note: we need to escape the dollar($) sign that's why using backlash.*

- Finally, this is how your Jenkinsfile should look like:
```
pipeline {
     
        agent any
 
        options {
                buildDiscarder(logRotator(numToKeepStr: '3'))
        }

        environment {
            USER_CREDENTIALS = credentials('<your_azure_function_credentials>')
            FUNCTION_URL     = "<function_name>.scm.azurewebsites.net/api/zipdeploy"
            ARTIFACT         = "functionapp.zip"
        }

        stages {
            
            stage('Build') {
                steps {
                        sh "zip -r $ARTIFACT *"
                        script {  
                            if (fileExists(env.ARTIFACT)) {
                                sh "echo Artifact: $ARTIFACT created successfuflly"
                            } else {
                                error('Failed to create artifact: ' + env.ARTIFACT)
                            }
                        }
                }
            }
            
            stage('Deploy') {
                steps {
                    script {
                        int status = sh(script: "curl -sLI -w '%{http_code}' -X POST https://$USER_CREDENTIALS_USR:$USER_CREDENTIALS_PSW@$FUNCTION_URL -T $ARTIFACT  -o /dev/null", returnStdout: true)

                        if (status != 200 && status != 201) {
                            error("Returned status code = $status when calling $FUNCTION_URL")
                        }
                    }
                }
            }
        }
        
        post {
            success {
                sh 'echo Function deployed successfully.'
            }
        }
}

```

Note: zip deploy by default assumes that your code is ready to deploy. To enable build process (like installing packages etc.) while deployment. Add this to application settings:
`SCM_DO_BUILD_DURING_DEPLOYMENT=true`

Thank you for you time!

---
If you have technical questions and suggestions, let's discuss them here, in [Github](https://github.com/MrHassanMurtaza) or say hi me in [LinkedIn](https://www.linkedin.com/in/mrhassanmurtaza/).