{"id":2292,"date":"2021-01-13T17:29:07","date_gmt":"2021-01-13T14:29:07","guid":{"rendered":"https:\/\/artem.services\/?p=2133"},"modified":"2022-06-08T22:48:52","modified_gmt":"2022-06-08T19:48:52","slug":"2292","status":"publish","type":"post","link":"https:\/\/artem.services\/?p=2292&lang=en","title":{"rendered":"\u00a0Jenkins &#8212; Active Choice: PostgreSQL &#8212; Return result of SELECT query"},"content":{"rendered":"<p><img loading=\"lazy\" class=\"aligncenter size-full wp-image-819\" src=\"https:\/\/artem.services\/wp-content\/uploads\/2019\/02\/Jenkins-Logo.png\" alt=\"\" width=\"1280\" height=\"412\" srcset=\"https:\/\/artem.services\/wp-content\/uploads\/2019\/02\/Jenkins-Logo.png 1280w, https:\/\/artem.services\/wp-content\/uploads\/2019\/02\/Jenkins-Logo-300x97.png 300w, https:\/\/artem.services\/wp-content\/uploads\/2019\/02\/Jenkins-Logo-768x247.png 768w, https:\/\/artem.services\/wp-content\/uploads\/2019\/02\/Jenkins-Logo-1024x330.png 1024w, https:\/\/artem.services\/wp-content\/uploads\/2019\/02\/Jenkins-Logo-954x307.png 954w\" sizes=\"(max-width: 1280px) 100vw, 1280px\" \/><\/p>\n<p>For a parameterized assembly with an image tag selection, you will need the <a href=\"https:\/\/plugins.jenkins.io\/uno-choice\/\" target=\"_blank\" rel=\"noopener\">Active Choices<\/a> plugin<\/p>\n<p>Go to &quot;<strong>Manage Jenkins<\/strong>&quot;<\/p>\n<p><img loading=\"lazy\" class=\"aligncenter size-full wp-image-1782\" src=\"https:\/\/artem.services\/wp-content\/uploads\/2020\/03\/001.png\" alt=\"\" width=\"349\" height=\"383\" srcset=\"https:\/\/artem.services\/wp-content\/uploads\/2020\/03\/001.png 349w, https:\/\/artem.services\/wp-content\/uploads\/2020\/03\/001-273x300.png 273w\" sizes=\"(max-width: 349px) 100vw, 349px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>Section &quot;<strong>Manage Plugins<\/strong>&quot;<\/p>\n<p><img loading=\"lazy\" class=\"aligncenter size-full wp-image-1783\" src=\"https:\/\/artem.services\/wp-content\/uploads\/2020\/03\/002.png\" alt=\"\" width=\"927\" height=\"493\" srcset=\"https:\/\/artem.services\/wp-content\/uploads\/2020\/03\/002.png 927w, https:\/\/artem.services\/wp-content\/uploads\/2020\/03\/002-300x160.png 300w, https:\/\/artem.services\/wp-content\/uploads\/2020\/03\/002-768x408.png 768w\" sizes=\"(max-width: 927px) 100vw, 927px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>Go to the &quot;<strong>Available<\/strong>&quot; tab and select &quot;<strong>Active Choices<\/strong>&quot; in the search.<\/p>\n<p><img loading=\"lazy\" class=\"aligncenter size-full wp-image-1796\" src=\"https:\/\/artem.services\/wp-content\/uploads\/2020\/04\/Screenshot-2020-04-01-at-10.55.37.png\" alt=\"\" width=\"1412\" height=\"130\" srcset=\"https:\/\/artem.services\/wp-content\/uploads\/2020\/04\/Screenshot-2020-04-01-at-10.55.37.png 1412w, https:\/\/artem.services\/wp-content\/uploads\/2020\/04\/Screenshot-2020-04-01-at-10.55.37-300x28.png 300w, https:\/\/artem.services\/wp-content\/uploads\/2020\/04\/Screenshot-2020-04-01-at-10.55.37-1024x94.png 1024w, https:\/\/artem.services\/wp-content\/uploads\/2020\/04\/Screenshot-2020-04-01-at-10.55.37-768x71.png 768w, https:\/\/artem.services\/wp-content\/uploads\/2020\/04\/Screenshot-2020-04-01-at-10.55.37-954x88.png 954w, https:\/\/artem.services\/wp-content\/uploads\/2020\/04\/Screenshot-2020-04-01-at-10.55.37-1354x125.png 1354w\" sizes=\"(max-width: 1412px) 100vw, 1412px\" \/><\/p>\n<p>Install it. <span class=\"VIiyi\" lang=\"en\"><span class=\"JLqJ4b ChMk0b\" data-language-for-alternatives=\"en\" data-language-to-translate-into=\"ru\" data-phrase-index=\"1\" data-number-of-phrases=\"2\"><span class=\"Q4iAWc\">You also need plugins:<\/span><\/span><\/span><\/p>\n<ul>\n<li><a href=\"https:\/\/plugins.jenkins.io\/database\" target=\"_blank\" rel=\"noopener noreferrer\">Database<\/a><\/li>\n<li><a href=\"https:\/\/plugins.jenkins.io\/database-postgresql\/\" target=\"_blank\" rel=\"noopener noreferrer\">PostgreSQL Database<\/a><\/li>\n<\/ul>\n<p>Create a &quot;<strong>New Item<\/strong>&quot; \u2013 &quot;<strong>Pipeline<\/strong>&quot;, indicate that it will be a parameterized assembly, and add the parameter &quot;<strong>Active Choices Parameter<\/strong>&quot;<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" class=\"aligncenter wp-image-2134 size-full\" src=\"https:\/\/artem.services\/wp-content\/uploads\/2021\/01\/Screenshot-2021-01-13-at-14.46.06.png\" alt=\"\" width=\"946\" height=\"502\" srcset=\"https:\/\/artem.services\/wp-content\/uploads\/2021\/01\/Screenshot-2021-01-13-at-14.46.06.png 946w, https:\/\/artem.services\/wp-content\/uploads\/2021\/01\/Screenshot-2021-01-13-at-14.46.06-300x159.png 300w, https:\/\/artem.services\/wp-content\/uploads\/2021\/01\/Screenshot-2021-01-13-at-14.46.06-768x408.png 768w\" sizes=\"(max-width: 946px) 100vw, 946px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>We indicate that this is &quot;<strong>Groovy Script<\/strong>&quot; and paste the following into it:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nimport groovy.sql.Sql\r\nimport java.sql.Driver\r\n\r\ncredentialsId = &#039;artem-services-rds-credentials&#039;\r\nurl = &#039;artem-services-rds.xxxxxxxxxxxx.eu-central-1.rds.amazonaws.com:5432\/postgres&#039;\r\n\r\ndef creds = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(\r\n  com.cloudbees.plugins.credentials.common.StandardUsernameCredentials.class, Jenkins.instance, null, null ).find{\r\n    it.id == credentialsId\r\n  }\r\n\r\ndef driver = Class.forName(&#039;org.postgresql.Driver&#039;).newInstance() as Driver \r\n\r\ndef props = new Properties()\r\nprops.setProperty(&quot;user&quot;, &quot;${creds.username}&quot;) \r\nprops.setProperty(&quot;password&quot;, &quot;${creds.password}&quot;)\r\n\r\ndef conn = driver.connect(&quot;jdbc:postgresql:\/\/${url}&quot;, props) \r\ndef sql = new Sql(conn)\r\n\r\ndef artifact = []\r\n\r\nartifact.add(&quot;Not selected&quot;)\r\n\r\ndef rows = sql.rows(&quot;select * from users&quot;).each { row -&gt;\r\n  artifact.add(&quot;$row.first_name&quot;)\r\n}\r\n\r\nreturn artifact\r\n<\/pre>\n<p>&nbsp;<\/p>\n<blockquote><p><span class=\"VIiyi\" lang=\"en\"><span class=\"JLqJ4b ChMk0b\" data-language-for-alternatives=\"en\" data-language-to-translate-into=\"ru\" data-phrase-index=\"0\" data-number-of-phrases=\"1\"><span class=\"Q4iAWc\">Where is the value of the variables, &quot;<strong>credentialsId<\/strong>&quot; &#8212; Jenkins Credentials ID with login and password to connect to the database;<\/span><\/span><\/span><\/p>\n<p>&quot;<strong>url<\/strong>&quot; &#8212; <span class=\"VIiyi\" lang=\"en\"><span class=\"JLqJ4b ChMk0b\" data-language-for-alternatives=\"en\" data-language-to-translate-into=\"ru\" data-phrase-index=\"0\" data-number-of-phrases=\"1\"><span class=\"Q4iAWc\">database connection string (endpoint + port + db name);<\/span><\/span><\/span><\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<p><span class=\"VIiyi\" lang=\"en\"><span class=\"JLqJ4b ChMk0b\" data-language-for-alternatives=\"en\" data-language-to-translate-into=\"ru\" data-phrase-index=\"0\" data-number-of-phrases=\"1\"><span class=\"Q4iAWc\">This <strong>Active Choice<\/strong> makes a <strong>SELECT<\/strong> query on the &quot;<strong>users<\/strong>&quot; table and returns only the values of the &quot;<strong>first_name<\/strong>&quot; fields, as well as adding &quot;<strong>Not selected<\/strong>&quot; to the first position of the result.<\/span><\/span><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>For a parameterized assembly with an image tag selection, you will need the Active Choices plugin Go to &quot;Manage Jenkins&quot; &nbsp; Section &quot;Manage Plugins&quot; &nbsp; Go to the &quot;Available&quot; tab and select &quot;Active Choices&quot; in the search. Install it. You also need plugins: Database PostgreSQL Database Create a &quot;New Item&quot; \u2013 &quot;Pipeline&quot;, indicate that it &hellip; <a href=\"https:\/\/artem.services\/?p=2292&#038;lang=en\" class=\"more-link\">\u041f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u0442\u044c \u0447\u0438\u0442\u0430\u0442\u044c<span class=\"screen-reader-text\"> &quot;\u00a0Jenkins &#8212; Active Choice: PostgreSQL &#8212; Return result of SELECT query&quot;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[613],"tags":[1465,617,1247,1089,1091],"_links":{"self":[{"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts\/2292"}],"collection":[{"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2292"}],"version-history":[{"count":2,"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts\/2292\/revisions"}],"predecessor-version":[{"id":2296,"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts\/2292\/revisions\/2296"}],"wp:attachment":[{"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2292"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2292"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2292"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}