{"id":1652,"date":"2024-10-07T14:34:27","date_gmt":"2024-10-07T13:34:27","guid":{"rendered":"https:\/\/editjournal.redakt.eu\/faxmodem\/?p=1652"},"modified":"2026-06-29T19:11:30","modified_gmt":"2026-06-29T18:11:30","slug":"commit-hash-azure-web-apps-node-js","status":"publish","type":"post","link":"https:\/\/editjournal.redakt.eu\/faxmodem\/blog\/development\/cloud-serverless\/commit-hash-azure-web-apps-node-js\/","title":{"rendered":"Finding deployment ID or commit hash with Azure Web Apps Node.JS"},"content":{"rendered":"<p>You deploy your Node.js code to Azure Web Apps. In your code, you want a way to know the commit ID\/hash of the latest deployment.<\/p>\n<p>Unfortunately, Azure does not keep the information on the latest commit it deployed in environment variables. And <code>git<\/code> is not available either for us to look it up with <code>git rev-parse<\/code>.<\/p>\n<h3>Set up <code>build.js<\/code><\/h3>\n<p>This script saves build information to <code>build.json<\/code> which we can later access at any time after deployment. The script can have any name, not necessarily <code>build.js<\/code>, just make sure it matches what you put into your <code>package.json<\/code> in the next step.<\/p>\n<pre><code>const fs = require(&#039;fs&#039;);\nconst path = require(&#039;path&#039;);\n\n\/\/ Debug info, see Azure Deployment Center logs\nconsole.log(&quot;process.env\\n&quot;, process.env);\n\n\/\/ Define path to build.json where build information will be stored\nconst filePath = path.join(__dirname, &#039;build.json&#039;);\n\ntry {\n    \/\/ Create object with build data\n    const buildData = {commitHash: process.env.SCM_COMMIT_ID};\n    \/\/ Write build data to build.json\n    fs.writeFileSync(filePath, JSON.stringify(buildData), &#039;utf-8&#039;);\n} catch (error) {\n    console.error(&#039;Failed to generate &#039;+filePath, error);\n}<\/code><\/pre>\n<p><code>SCM_COMMIT_ID<\/code> comes from <a href=\"https:\/\/github.com\/projectkudu\/kudu\/\">projectkudu\/kudu<\/a> where it was <a href=\"https:\/\/github.com\/projectkudu\/kudu\/issues\/806\">introduced in 2013<\/a>. Kudu is the engine behind <a href=\"https:\/\/azure.microsoft.com\/en-us\/documentation\/articles\/web-sites-publish-source-control\/\">git deployments in Azure App Service<\/a>. The repository was set to be a read-only public archive in late September 2024, but the tool is said to continue on Azure. There are other environment variables available during deployment, see <a href=\"https:\/\/github.com\/projectkudu\/kudu\/wiki\/Deployment-Environment\">https:\/\/github.com\/projectkudu\/kudu\/wiki\/Deployment-Environment<\/a>.<\/p>\n<h3>Add your <code>build.js<\/code> to <code>package.json<\/code><\/h3>\n<p>Use the <code>scripts<\/code> \u2192 <code>build<\/code> directive in <code>package.json<\/code> to schedule our script to be executed on each build.<\/p>\n<pre><code>{\n  &quot;name&quot;: &quot;example application&quot;,\n  &quot;version&quot;: &quot;1.0.0&quot;,\n  \/\/ ...\n  &quot;scripts&quot;: {\n    &quot;build&quot;: &quot;node build.js&quot;,\n  },\n  \/\/ ...\n}<\/code><\/pre>\n<h3>Read the build information when you need it<\/h3>\n<p>Here's how you can get that information anywhere inside your Node.js application.<\/p>\n<pre><code>const fs = require(&#039;fs&#039;);\nconst path = require(&#039;path&#039;);\n\n\/\/ Read the build information\nconst filePath = path.join(process.cwd(), &#039;build.json&#039;);\nconst buildData = JSON.parse(fs.readFileSync(filePath, &#039;utf-8&#039;));\n\/\/ Here&#039;s the commit hash\nconsole.log(buildData.commitHash);<\/code><\/pre>\n<h3>Bonus points<\/h3>\n<p>If you do have access to <code>git<\/code> during build, then simply do this in your <code>build.js<\/code>:<\/p>\n<pre><code>const { execSync } = require(&#039;child_process&#039;);\n\nconst commitHash = execSync(&#039;git rev-parse --short HEAD&#039;).toString().trim();<\/code><\/pre>\n<p>This way you can get the latest commit hash without relying on Azure\/Kudu-specific environment variables.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You deploy your Node.js code to Azure Web Apps. In your code, you want to know the latest deployment commit ID\/hash. Since Azure does not keep the information in environment variables, we will need to call a custom script on each build from package.json.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[778],"tags":[824,769],"class_list":["post-1652","post","type-post","status-publish","format-standard","hentry","category-cloud-serverless","tag-microsoft-azure","tag-node-js","entry"],"_links":{"self":[{"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/posts\/1652","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/comments?post=1652"}],"version-history":[{"count":4,"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/posts\/1652\/revisions"}],"predecessor-version":[{"id":1836,"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/posts\/1652\/revisions\/1836"}],"wp:attachment":[{"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/media?parent=1652"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/categories?post=1652"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/tags?post=1652"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}