{"id":408,"date":"2023-01-05T14:41:55","date_gmt":"2023-01-05T14:41:55","guid":{"rendered":"https:\/\/computeoncloud.eu\/?p=408"},"modified":"2023-01-05T16:31:54","modified_gmt":"2023-01-05T16:31:54","slug":"deploy-azure-functions-with-jenkins","status":"publish","type":"post","link":"https:\/\/computeoncloud.eu\/index.php\/2023\/01\/05\/deploy-azure-functions-with-jenkins\/","title":{"rendered":"Deploy Azure Functions with Jenkins"},"content":{"rendered":"\n<p>There are multiple ways to continuously deploy the latest code to Azure Functions.<\/p>\n\n\n\n<p>Since at&nbsp;<a href=\"https:\/\/www.openanalytics.eu\/\">OpenAnalytics<\/a>&nbsp;we mostly used Jenkins for CI\/CD purposes, so 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.<\/p>\n\n\n\n<p>The idea is to first create the zip out of source code and then deploy it:<\/p>\n\n\n\n<p><code>curl -X POST https:\/\/&lt;user&gt;:&lt;password&gt;@&lt;function-app&gt;.scm.azurewebsites.net\/api\/zipdeploy -T &lt;zipfile&gt;<\/code><\/p>\n\n\n\n<p>Here&#8217;s how you can do it:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Go to your function app on the azure portal then open properties from the sidebar. You should find the&nbsp;<strong>Deployment Trigger URL<\/strong>&nbsp;there and that&#8217;s what you need. it should look something like this:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>https:&#47;&#47;$&lt;your-functionapp&gt;:&lt;your-functionapp-password&gt;&lt;your-functionapp&gt;@.scm.azurewebsites.net\/deploy\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Go to Jenkins &gt; Credentials &gt; Global Credentials and create global credentials.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/res.cloudinary.com\/practicaldev\/image\/fetch\/s--yVGMLRvZ--\/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880\/https:\/\/dev-to-uploads.s3.amazonaws.com\/i\/4qumzp0xqpg03l1yz5t4.png\"><img decoding=\"async\" src=\"https:\/\/res.cloudinary.com\/practicaldev\/image\/fetch\/s--yVGMLRvZ--\/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880\/https:\/\/dev-to-uploads.s3.amazonaws.com\/i\/4qumzp0xqpg03l1yz5t4.png\" alt=\"Alt Text\"\/><\/a><\/figure>\n\n\n\n<p><em>Note: we need to escape the dollar($) sign that&#8217;s why using backlash.<\/em><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Finally, this is how your Jenkinsfile should look like:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>pipeline {\n\n        agent any\n\n        options {\n                buildDiscarder(logRotator(numToKeepStr: '3'))\n        }\n\n        environment {\n            USER_CREDENTIALS = credentials('&lt;your_azure_function_credentials&gt;')\n            FUNCTION_URL     = \"&lt;function_name&gt;.scm.azurewebsites.net\/api\/zipdeploy\"\n            ARTIFACT         = \"functionapp.zip\"\n        }\n\n        stages {\n\n            stage('Build') {\n                steps {\n                        sh \"zip -r $ARTIFACT *\"\n                        script {  \n                            if (fileExists(env.ARTIFACT)) {\n                                sh \"echo Artifact: $ARTIFACT created successfuflly\"\n                            } else {\n                                error('Failed to create artifact: ' + env.ARTIFACT)\n                            }\n                        }\n                }\n            }\n\n            stage('Deploy') {\n                steps {\n                    script {\n                        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)\n\n                        if (status != 200 &amp;&amp; status != 201) {\n                            error(\"Returned status code = $status when calling $FUNCTION_URL\")\n                        }\n                    }\n                }\n            }\n        }\n\n        post {\n            success {\n                sh 'echo Function deployed successfully.'\n            }\n        }\n}\n\n<\/code><\/pre>\n\n\n\n<p>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:<br><code>SCM_DO_BUILD_DURING_DEPLOYMENT=true<\/code><\/p>\n\n\n\n<p>I hope my experience will help you!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are multiple ways to continuously deploy the latest code to Azure Functions. Since at&nbsp;OpenAnalytics&nbsp;we mostly used Jenkins for CI\/CD purposes, so 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 &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"\" href=\"https:\/\/computeoncloud.eu\/index.php\/2023\/01\/05\/deploy-azure-functions-with-jenkins\/\"> <span class=\"screen-reader-text\">Deploy Azure Functions with Jenkins<\/span> Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,12,5],"tags":[],"class_list":["post-408","post","type-post","status-publish","format-standard","hentry","category-azure","category-functions","category-serverless"],"_links":{"self":[{"href":"https:\/\/computeoncloud.eu\/index.php\/wp-json\/wp\/v2\/posts\/408","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/computeoncloud.eu\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/computeoncloud.eu\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/computeoncloud.eu\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/computeoncloud.eu\/index.php\/wp-json\/wp\/v2\/comments?post=408"}],"version-history":[{"count":2,"href":"https:\/\/computeoncloud.eu\/index.php\/wp-json\/wp\/v2\/posts\/408\/revisions"}],"predecessor-version":[{"id":439,"href":"https:\/\/computeoncloud.eu\/index.php\/wp-json\/wp\/v2\/posts\/408\/revisions\/439"}],"wp:attachment":[{"href":"https:\/\/computeoncloud.eu\/index.php\/wp-json\/wp\/v2\/media?parent=408"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/computeoncloud.eu\/index.php\/wp-json\/wp\/v2\/categories?post=408"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/computeoncloud.eu\/index.php\/wp-json\/wp\/v2\/tags?post=408"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}