{"id":11092,"date":"2024-03-21T18:00:52","date_gmt":"2024-03-21T22:00:52","guid":{"rendered":"https:\/\/www.redline13.com\/blog\/?p=11092"},"modified":"2024-03-21T18:00:53","modified_gmt":"2024-03-21T22:00:53","slug":"setting-k6-environment-variables","status":"publish","type":"post","link":"https:\/\/www.redline13.com\/blog\/2024\/03\/setting-k6-environment-variables\/","title":{"rendered":"Setting k6 Environment Variables"},"content":{"rendered":"\n<p><\/p>\n\n\n<p><a id=\"post-11092-_9p3vncourjhk\"><\/a> <img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-11093\" src=\"https:\/\/www.redline13.com\/blog\/wp-content\/uploads\/2024\/03\/word-image-11092-1.png\" alt=\"Setting k6 Environment Variables\" width=\"400\" height=\"300\" srcset=\"https:\/\/www.redline13.com\/blog\/wp-content\/uploads\/2024\/03\/word-image-11092-1.png 400w, https:\/\/www.redline13.com\/blog\/wp-content\/uploads\/2024\/03\/word-image-11092-1-300x225.png 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/p>\n<p>Building on the concept of extensibility, <a href=\"https:\/\/k6.io\/\" target=\"_blank\" rel=\"noopener\">k6<\/a> has the ability to read variables set in the local environment and apply them to test scripts.\u00a0 Reusable scripts often only require minor changes to be used in different contexts, and k6 environment variables provide this.\u00a0 In addition, there are some k6 <a href=\"https:\/\/k6.io\/docs\/extensions\/\" target=\"_blank\" rel=\"noopener\">extensions<\/a> that depend on environment variables to set their configuration.\u00a0 In this post, we will show you how to set k6 environment variables both in your local development environment, and in tests that run on RedLine13.<\/p>\n<h3><a id=\"post-11092-_jk09bit29zcg\"><\/a>Local Environment<\/h3>\n<p>Natively, k6 supports environment variables set at the command line level. There is a concise syntax described in the <a href=\"https:\/\/grafana.com\/docs\/k6\/latest\/using-k6\/environment-variables\/\" target=\"_blank\" rel=\"noopener\">official documentation<\/a>, which is as follows:<\/p>\n<pre><strong>$ k6 run -e MY_VARIABLE=my_value script.js<\/strong><\/pre>\n<p>In this format, an environment variable will be set with the name \u201c<code>MY_VARIABLE<\/code>\u201d, and it will contain \u201c<code>my_value<\/code>\u201d. If you want to use this variable directly within your test script, you can use the <code>${__ENV.MY_VARIABLE}<\/code> convention to insert your value. For example, if you set <code>-e MY_HOSTNAME=api.my-domain.com<\/code> at the command line, you can access the value as follows within your test:<\/p>\n<pre><strong>export default function () {<br \/><\/strong><strong>\u00a0 \u00a0 const result = http.get(`http:\/\/${__ENV.MY_HOSTNAME}\/get-widget`);<br \/><\/strong><strong>}<\/strong><\/pre>\n<p>This will direct the request to <code>api.my-domain.com\/get-widget<\/code> without any modification of the actual test script. Test designers familiar with <a href=\"https:\/\/jmeter.apache.org\/usermanual\/best-practices.html#user_variables\" target=\"_blank\" rel=\"noopener\">JMeter user variables<\/a> will find this syntax similar, and it functions in much the same way.<\/p>\n<h3><a id=\"post-11092-_7gm6x9q7d5d3\"><\/a>Tests on RedLine13<\/h3>\n<p>If you are running your k6 test in the cloud using RedLine13, as an alternative to setting at the command line environment variables can instead be set using the <code><a href=\"https:\/\/github.com\/szkiba\/xk6-dotenv\" target=\"_blank\" rel=\"noopener\">xk6-dotenv<\/a><\/code> extension. You can enable this extension for your test by selecting it from the list of available extensions under \u201c<em>Advanced k6 Test Options<\/em>\u201d.<\/p>\n<figure id=\"attachment_11094\" class=\"wp-caption aligncenter\" style=\"max-width: 867px\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-11094\" src=\"https:\/\/www.redline13.com\/blog\/wp-content\/uploads\/2024\/03\/word-image-11092-2.png\" alt=\"Selecting the xk6-env extension from Advanced k6 Test Options\" width=\"867\" height=\"546\" srcset=\"https:\/\/www.redline13.com\/blog\/wp-content\/uploads\/2024\/03\/word-image-11092-2.png 867w, https:\/\/www.redline13.com\/blog\/wp-content\/uploads\/2024\/03\/word-image-11092-2-300x189.png 300w, https:\/\/www.redline13.com\/blog\/wp-content\/uploads\/2024\/03\/word-image-11092-2-768x484.png 768w, https:\/\/www.redline13.com\/blog\/wp-content\/uploads\/2024\/03\/word-image-11092-2-425x268.png 425w\" sizes=\"auto, (max-width: 867px) 100vw, 867px\" \/><figcaption class=\"wp-caption-text\">Selecting the xk6-env extension from \u201cAdvanced k6 Test Options\u201d.<\/figcaption><\/figure>\n<p>In a separate file, we can specify any number of environment variables in the same <code>MY_VARIABLE=my_value<\/code> format explained above. Each assignment within this file should go on its own line. The <a href=\"https:\/\/github.com\/MStoykov\/xk6-dotenv\/blob\/master\/docs\/README.md\" target=\"_blank\" rel=\"noopener\">documentation<\/a> for this extension specifies naming conventions for <code>.env<\/code> files, however it will look for a file named \u201c<code>.env.development.local<\/code>\u201d first. Let\u2019s go ahead and save our file with this name.<\/p>\n<p>To upload the file with your test, we can simply attach it as an <a href=\"https:\/\/www.redline13.com\/blog\/docs\/attaching-extra-files\/\" target=\"_blank\" rel=\"noopener\">extra file<\/a>:<\/p>\n<figure id=\"attachment_11095\" class=\"wp-caption aligncenter\" style=\"max-width: 652px\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-11095\" src=\"https:\/\/www.redline13.com\/blog\/wp-content\/uploads\/2024\/03\/word-image-11092-3.png\" alt=\"Uploading the environment configuration as an extra file attachment\" width=\"652\" height=\"93\" srcset=\"https:\/\/www.redline13.com\/blog\/wp-content\/uploads\/2024\/03\/word-image-11092-3.png 652w, https:\/\/www.redline13.com\/blog\/wp-content\/uploads\/2024\/03\/word-image-11092-3-300x43.png 300w, https:\/\/www.redline13.com\/blog\/wp-content\/uploads\/2024\/03\/word-image-11092-3-425x61.png 425w\" sizes=\"auto, (max-width: 652px) 100vw, 652px\" \/><figcaption class=\"wp-caption-text\">Uploading the environment configuration as an extra file attachment.<\/figcaption><\/figure>\n<p>The environment configuration file will be uploaded to your load generator servers and read alongside your k6 test plan.<\/p>\n<hr \/>\n<p>Did you know that RedLine13 offers a full-featured, time-limited free trial? <a href=\"http:\/\/www.redline13.com\/Service\" target=\"_blank\" rel=\"noopener\">Sign up now<\/a>, and start testing today!<\/p>","protected":false},"excerpt":{"rendered":"<p>Building on the concept of extensibility, k6 has the ability to read variables set in the local environment and apply them to test scripts.\u00a0 Reusable scripts often only require minor changes to be used in different contexts, and k6 environment variables provide this.\u00a0 In addition, there are some k6 extensions that depend on environment variables to set their configuration.\u00a0 In this post, we will show you how to set k6 environment variables both in your local<a class=\"more-link\" href=\"https:\/\/www.redline13.com\/blog\/2024\/03\/setting-k6-environment-variables\/\">Read More &rarr;<\/a><\/p>\n","protected":false},"author":11,"featured_media":11093,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,1],"tags":[128,700,596,318,424,701],"class_list":{"0":"entry","1":"post","2":"publish","3":"author-dkoziel","4":"post-11092","6":"format-standard","7":"has-post-thumbnail","8":"category-blog","9":"category-uncategorized","10":"post_tag-cloud-load-testing","11":"post_tag-environment-variables","12":"post_tag-k6","13":"post_tag-load-testing","14":"post_tag-redline13","15":"post_tag-test-configuration"},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.12 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Setting k6 Environment Variables - RedLine13<\/title>\n<meta name=\"description\" content=\"Reusable scripts often only require minor changes to be used in different contexts, and k6 environment variables provide this.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.redline13.com\/blog\/2024\/03\/setting-k6-environment-variables\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Setting k6 Environment Variables - RedLine13\" \/>\n<meta property=\"og:description\" content=\"Reusable scripts often only require minor changes to be used in different contexts, and k6 environment variables provide this.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.redline13.com\/blog\/2024\/03\/setting-k6-environment-variables\/\" \/>\n<meta property=\"og:site_name\" content=\"RedLine13\" \/>\n<meta property=\"article:published_time\" content=\"2024-03-21T22:00:52+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-21T22:00:53+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.redline13.com\/blog\/wp-content\/uploads\/2024\/03\/word-image-11092-1.png\" \/>\n\t<meta property=\"og:image:width\" content=\"400\" \/>\n\t<meta property=\"og:image:height\" content=\"300\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"David Koziel\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"David Koziel\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.redline13.com\/blog\/2024\/03\/setting-k6-environment-variables\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.redline13.com\/blog\/2024\/03\/setting-k6-environment-variables\/\"},\"author\":{\"name\":\"David Koziel\",\"@id\":\"https:\/\/www.redline13.com\/blog\/#\/schema\/person\/51d282221e3230ab35f964f98ada9b20\"},\"headline\":\"Setting k6 Environment Variables\",\"datePublished\":\"2024-03-21T22:00:52+00:00\",\"dateModified\":\"2024-03-21T22:00:53+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.redline13.com\/blog\/2024\/03\/setting-k6-environment-variables\/\"},\"wordCount\":406,\"publisher\":{\"@id\":\"https:\/\/www.redline13.com\/blog\/#organization\"},\"keywords\":[\"Cloud Load Testing\",\"environment variables\",\"k6\",\"Load Testing\",\"RedLine13\",\"test configuration\"],\"articleSection\":[\"Blog\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.redline13.com\/blog\/2024\/03\/setting-k6-environment-variables\/\",\"url\":\"https:\/\/www.redline13.com\/blog\/2024\/03\/setting-k6-environment-variables\/\",\"name\":\"Setting k6 Environment Variables - RedLine13\",\"isPartOf\":{\"@id\":\"https:\/\/www.redline13.com\/blog\/#website\"},\"datePublished\":\"2024-03-21T22:00:52+00:00\",\"dateModified\":\"2024-03-21T22:00:53+00:00\",\"description\":\"Reusable scripts often only require minor changes to be used in different contexts, and k6 environment variables provide this.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.redline13.com\/blog\/2024\/03\/setting-k6-environment-variables\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.redline13.com\/blog\/2024\/03\/setting-k6-environment-variables\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.redline13.com\/blog\/2024\/03\/setting-k6-environment-variables\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.redline13.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Setting k6 Environment Variables\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.redline13.com\/blog\/#website\",\"url\":\"https:\/\/www.redline13.com\/blog\/\",\"name\":\"RedLine13\",\"description\":\"(Almost) Free Load Testing in the Cloud\",\"publisher\":{\"@id\":\"https:\/\/www.redline13.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.redline13.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.redline13.com\/blog\/#organization\",\"name\":\"RedLine13\",\"url\":\"https:\/\/www.redline13.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.redline13.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.redline13.com\/blog\/wp-content\/uploads\/2013\/06\/cropped-rl13-header-logo.jpg\",\"contentUrl\":\"https:\/\/www.redline13.com\/blog\/wp-content\/uploads\/2013\/06\/cropped-rl13-header-logo.jpg\",\"width\":300,\"height\":68,\"caption\":\"RedLine13\"},\"image\":{\"@id\":\"https:\/\/www.redline13.com\/blog\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.redline13.com\/blog\/#\/schema\/person\/51d282221e3230ab35f964f98ada9b20\",\"name\":\"David Koziel\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.redline13.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/2babf644e0993fc86893c24d7525f1e3be114a8746c01249797f25587ae1697a?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/2babf644e0993fc86893c24d7525f1e3be114a8746c01249797f25587ae1697a?s=96&d=mm&r=g\",\"caption\":\"David Koziel\"},\"url\":\"https:\/\/www.redline13.com\/blog\/author\/dkoziel\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Setting k6 Environment Variables - RedLine13","description":"Reusable scripts often only require minor changes to be used in different contexts, and k6 environment variables provide this.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.redline13.com\/blog\/2024\/03\/setting-k6-environment-variables\/","og_locale":"en_US","og_type":"article","og_title":"Setting k6 Environment Variables - RedLine13","og_description":"Reusable scripts often only require minor changes to be used in different contexts, and k6 environment variables provide this.","og_url":"https:\/\/www.redline13.com\/blog\/2024\/03\/setting-k6-environment-variables\/","og_site_name":"RedLine13","article_published_time":"2024-03-21T22:00:52+00:00","article_modified_time":"2024-03-21T22:00:53+00:00","og_image":[{"width":400,"height":300,"url":"https:\/\/www.redline13.com\/blog\/wp-content\/uploads\/2024\/03\/word-image-11092-1.png","type":"image\/png"}],"author":"David Koziel","twitter_card":"summary_large_image","twitter_misc":{"Written by":"David Koziel","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.redline13.com\/blog\/2024\/03\/setting-k6-environment-variables\/#article","isPartOf":{"@id":"https:\/\/www.redline13.com\/blog\/2024\/03\/setting-k6-environment-variables\/"},"author":{"name":"David Koziel","@id":"https:\/\/www.redline13.com\/blog\/#\/schema\/person\/51d282221e3230ab35f964f98ada9b20"},"headline":"Setting k6 Environment Variables","datePublished":"2024-03-21T22:00:52+00:00","dateModified":"2024-03-21T22:00:53+00:00","mainEntityOfPage":{"@id":"https:\/\/www.redline13.com\/blog\/2024\/03\/setting-k6-environment-variables\/"},"wordCount":406,"publisher":{"@id":"https:\/\/www.redline13.com\/blog\/#organization"},"keywords":["Cloud Load Testing","environment variables","k6","Load Testing","RedLine13","test configuration"],"articleSection":["Blog"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.redline13.com\/blog\/2024\/03\/setting-k6-environment-variables\/","url":"https:\/\/www.redline13.com\/blog\/2024\/03\/setting-k6-environment-variables\/","name":"Setting k6 Environment Variables - RedLine13","isPartOf":{"@id":"https:\/\/www.redline13.com\/blog\/#website"},"datePublished":"2024-03-21T22:00:52+00:00","dateModified":"2024-03-21T22:00:53+00:00","description":"Reusable scripts often only require minor changes to be used in different contexts, and k6 environment variables provide this.","breadcrumb":{"@id":"https:\/\/www.redline13.com\/blog\/2024\/03\/setting-k6-environment-variables\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.redline13.com\/blog\/2024\/03\/setting-k6-environment-variables\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.redline13.com\/blog\/2024\/03\/setting-k6-environment-variables\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.redline13.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Setting k6 Environment Variables"}]},{"@type":"WebSite","@id":"https:\/\/www.redline13.com\/blog\/#website","url":"https:\/\/www.redline13.com\/blog\/","name":"RedLine13","description":"(Almost) Free Load Testing in the Cloud","publisher":{"@id":"https:\/\/www.redline13.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.redline13.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.redline13.com\/blog\/#organization","name":"RedLine13","url":"https:\/\/www.redline13.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.redline13.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.redline13.com\/blog\/wp-content\/uploads\/2013\/06\/cropped-rl13-header-logo.jpg","contentUrl":"https:\/\/www.redline13.com\/blog\/wp-content\/uploads\/2013\/06\/cropped-rl13-header-logo.jpg","width":300,"height":68,"caption":"RedLine13"},"image":{"@id":"https:\/\/www.redline13.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/www.redline13.com\/blog\/#\/schema\/person\/51d282221e3230ab35f964f98ada9b20","name":"David Koziel","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.redline13.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/2babf644e0993fc86893c24d7525f1e3be114a8746c01249797f25587ae1697a?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2babf644e0993fc86893c24d7525f1e3be114a8746c01249797f25587ae1697a?s=96&d=mm&r=g","caption":"David Koziel"},"url":"https:\/\/www.redline13.com\/blog\/author\/dkoziel\/"}]}},"_links":{"self":[{"href":"https:\/\/www.redline13.com\/blog\/wp-json\/wp\/v2\/posts\/11092","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.redline13.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.redline13.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.redline13.com\/blog\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/www.redline13.com\/blog\/wp-json\/wp\/v2\/comments?post=11092"}],"version-history":[{"count":5,"href":"https:\/\/www.redline13.com\/blog\/wp-json\/wp\/v2\/posts\/11092\/revisions"}],"predecessor-version":[{"id":11100,"href":"https:\/\/www.redline13.com\/blog\/wp-json\/wp\/v2\/posts\/11092\/revisions\/11100"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.redline13.com\/blog\/wp-json\/wp\/v2\/media\/11093"}],"wp:attachment":[{"href":"https:\/\/www.redline13.com\/blog\/wp-json\/wp\/v2\/media?parent=11092"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.redline13.com\/blog\/wp-json\/wp\/v2\/categories?post=11092"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.redline13.com\/blog\/wp-json\/wp\/v2\/tags?post=11092"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}