I am trying to edit the given groovy template file given for the emailext plugin to display Jenkins environment variables. The code I am experimenting is:
pipeline {
agent {
label 'main'
}
environment {
test = "This is a test line."
}
stages {
stage('Debug') {
steps {
sh 'printenv'
sh "echo ${env.test}"
bat 'echo %test%'
}
}
}
post {
always {
echo "Emailing.."
emailext body: '''${SCRIPT, template="groovy-html-edited.template"}''',
subject: currentBuild.currentResult + " : " + env.JOB_NAME,
to: 'example#email.com'
}
}
}
What I want is to be able to display the value of the pipeline environment variable within the email sent to the email address. Within the groovy template, I have tried using the following in an edited groovy template file:
${env.test}
${ENV.test}
${ENV,var="test"}
${env,var="test"}
Within the groovy-html-edited.template, the portion I've added for testing is:
<!-- TEST SECTION-->
<table class="section">
<tr class="tr-title">
<td class="td-title-main" colspan=2>
TEST SECTION
</td>
</tr>
<tr>
<td>${ENV,var='test'}</td>
</tr>
</table>
<br/>
What is the correct variable name to get a pipeline environment variable within the groovy template?
Here is an example of getting env vars from a pipeline within script template:
<%
import hudson.Util
import hudson.Functions
import hudson.model.Result;
import hudson.model.Build
//get data from pipeline
def envOverrides = it.getAction("org.jenkinsci.plugins.workflow.cps.EnvActionImpl").getOverriddenEnvironment()
%>
<!-- TEST SECTION-->
<table class="section">
<tr class="tr-title">
<td class="td-title-main" colspan=2>
TEST SECTION
</td>
</tr>
<tr>
<td>${envOverrides["test"]}</td>
</tr>
</table>
<br/>
Related
When I'm trying to run this in Jenkins automation project,
I got this error,
WorkflowScript: 552: unexpected token: JELLY_SCRIPT # line 552, column 27.
${JELLY_SCRIPT,template="testResults.jelly"}
I have added,to the email body ${JELLY_SCRIPT,template="testResults.jelly"} inside success stage.
Why is this problem occuring?
post {
always {
junit '**/results/*.xml'
}
success {
emailext (
subject: "SUCCESSFUL: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'", to: '$EMAIL_LIST', attachmentsPattern: '**/results/*',attachLog: true,
body: """<p>UNSTABLE: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'</p>
<p>Check console output at ;<a href='${env.BUILD_URL}'>${env.JOB_NAME} [${env.BUILD_NUMBER}]</a></p> <br> Unit test: ${env.UNIT_TEST} <br> Integration test: ${env.INTEGRATION_TEST} <br> Upgrader test: ${env.UPGRADER_TEST} <br>
<h1>Unit Test Report</h1>
${env.UNIT_TEST_REPORT}
<br> ${env.UPGRADER_REPORT}
<br> <h3>Find Upgrader Logs in following location : </h3><br>
${env.FTP_PATH}
<br> ${env.FTP_LOCATION} <br> ${env.EMAIL_CONTENT}""",
recipientProviders: [[$class: 'UpstreamComitterRecipientProvider']]
)
cleanWs()
}
unstable {
emailext (
subject: "UNSTABLE: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'", to: '$EMAIL_LIST', attachmentsPattern: '**/results/*',attachLog: true,
body: """<p>UNSTABLE: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'</p>
<p>Check console output at ;<a href='${env.BUILD_URL}'>${env.JOB_NAME} [${env.BUILD_NUMBER}]</a></p> <br> Unit test: ${env.UNIT_TEST} <br> Integration test: ${env.INTEGRATION_TEST} <br> Upgrader test: ${env.UPGRADER_TEST} <br>
<h1>Unit Test Report</h1>
${env.UNIT_TEST_REPORT}
<br> ${env.UPGRADER_REPORT}
<br> <h3>Find Upgrader Logs in following location : </h3><br>
${env.FTP_PATH}
<br> ${env.FTP_LOCATION} <br> ${env.EMAIL_CONTENT}""",
recipientProviders: [[$class: 'UpstreamComitterRecipientProvider']]
)
cleanWs()
}
}
above is the full code of email template. when add above ${JELLY_SCRIPT,template="testResults.jelly"} inside email body, this error occured.
With Jenkins pipeline you are able to set any environment variable through Global Variable called - env.
Jelly template in it's turn gives you ability to access Jenkins API including hudson.model.AbstractBuild and hudson.model.AbstractProject objects.
Here are the snippets that I use:
Jenkinsfile:
node {
env.MYVAR = 'My variable'
emailext body: ${JELLY_SCRIPT, template="myTemplate"}, subject: 'MySubject', to: 'me'
}
Jelly template (myTemplate):
<?jelly escape-by-default='true'?>
<!DOCTYPE html [
<!ENTITY nbsp "&nbsp;">
]>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define">
<head>
<style>
body table, td, th, p, h1, h2 {
margin:0;
font:normal normal 100% Georgia, Serif;
background-color: #ffffff;
}
</style>
</head>
<body>
<j:set var="buildEnv" value="${build.getEnvironment(listener)}" />
<j:set var="myVar" value="${buildEnv.get('MYVAR')}" />
<table>
<tr>
<td>Variable</td>
<td>
<p>${myVar}</p>
</td>
</tr>
</table>
</div>
</body>
</j:jelly>
The problem is that I couldn't get access to my custom variable from the Jelly template.
I have tried a lot of possible ana impossible options (withEnv pipeline step, call several other methods from AbstractBuild class (getEnvironments, getBuildVariables), but nothing.
The only solution that I found is:
<j:set var="myvar" value="${it.getAction('org.jenkinsci.plugins.workflow.cps.EnvActionImpl').getOverriddenEnvironment()}"/>
<p>My var: ${myvar}</p>
I can't seem to make the whenever gem work with my actionmailer. I am trying to run the application in development wherein an email would be sent to a specific individual at a specific time.
In my schedule.rb file I have the following:
every :day, :at => '12:48pm' do
runner "FoodPlan.food_email"
end
In my controller called food_plans_controller.rb I have:
def self.food_email
#food_plans = FoodPlan.where(food_plan_date: Date.today).order('meal ASC')
UserMailer.food_email(#food_plans).deliver_now
end
In user_mailer.rb I have (take note I removed the email for privacy reasons) :
def food_email (food)
#food_plans = food
mail(to: '---------#yahoo.com', subject: 'Food Orders for #{Date.today}')
end
I have a folder in the views called user_mailer, inside it is a file called food_email.html.erb with the ff:
<!DOCTYPE html>
<html>
</head>
<body>
<h1>title</h1>
<p>
Good morning, -----!
<br><br>
Meal Plans for <%=Date.today%>:
<br><br>
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>#</th>
<th>Meal Plan</th>
<th>Ordered by</th>
<th>Room Number</th>
<th>Received by</th>
<th>Signature</th>
</tr>
</thead>
<tbody>
<%countint=1%>
<% #food_plans.each do |food_plan| %>
<tr>
<td><%=countint%></td>
<td><%= food_plan.meal %></td>
<td><%=food_plan.applicant.first_name%>
<%=food_plan.applicant.last_name%></td>
<td><%=food_plan.applicant.room_number%></td>
<%countint+=1%>
<td></td>
<td></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<br>
---.
<br>
<br>
If you have any concerns, don't hesitate to call us at ------.
<br>
<br>
Thanks, <br>----
</p>
</body>
</html>
In my development config I have (I removed the email and password):
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
domain: 'example.com',
user_name: '-------',
password: '-------',
authentication: 'plain',
enable_starttls_auto: true }
I have tried reading this guide but I cannot still get the gem to work with actionmailer. I have also read up on the documentation of the whenever gem but I still can't figure out what I'm doing wrong. :(
I'm not getting any errors, it's just that the email is not sending.
I guess your whenever rules did not ever get to the local crontab and thus are never actually run. The whenever gem rules do not make the system run the commands by theselves, they are only a ruby notation of the cron rules that reside in the /etc/crontab (or similar) on unix-like systems.
The whenever gem automatically updates the crontab during deployment (using the capistrano plugin), so your rules should work on the production server.
On the development host however, you need to update your crontab manually (credits):
whenever --update-crontab --set environment='development'
To view what's currently inside your crontab, use cat /etc/crontab. Your rules, defined in whenever, should be present in the file after you've updated it.
I'm using Stripe grails plugin in my application and I'm getting the below error:
Class:groovy.lang.MissingPropertyExceptionMessage:No such property: Stripe for class: com.myApp.app.SubscriptionRequestController
Here is my action:
def charge(String stripeToken, Double amount) {
Stripe.apiKey = grailsApplication.config.grails.plugins.stripe.secretKey
def amountInCents = (amount * 100) as Integer
def chargeParams = [
'amount': amountInCents,
'currency': 'usd',
'card': stripeToken,
'description': 'customer#sample.org'
]
def status
try {
Charge.create(chargeParams)
status = 'Your purchase was successful.'
} catch(CardException) {
status = 'There was an error processing your credit card.'
}
redirect(action: "confirmation", params: [msg: status])
return
}
i'm also getting the below error since i installed the plugin when i remove it i don't see it, it occurs while trying to access or refresh any view :
java.lang.RuntimeException: It looks like you are missing some calls to the r:layoutResources tag. After rendering your page the following have not been rendered: [head]
Try this:
in your main layout file e.g.: main.gsp, have the two additional both in body and head. Also, loads the stripe-v2 manually. No need to add it in web-app/js as the plugin itself already has it.
<html>
<head>
<g:javascript src="stripe-v2.js" />
<r:layoutResources/>
</head>
<body>
<g:layoutBody/>
<r:layoutResources/>
</body>
</html>
in config.groovy add:
grails.resources.modules = {
stripe {
dependsOn 'jquery'
resource url:'/js/stripe-v2.js', disposition: 'head', exclude:'minify'
}
}
Sample gsp (taken from the docs) but i added payment-errors as the source use it:
<stripe:script formName="payment-form"/>
<g:form controller="checkout" action="charge" method="POST" name="payment-form">
<div class="payment-errors"></div>
<div class="form-row">
<label>Amount (USD)</label>
<input type="text" size="20" autocomplete="off" id="amount" name="amount"/>
</div>
<stripe:creditCardInputs cssClass="form-row"/>
<button type="submit">Submit Payment</button>
</g:form>
I think the plugin itself did not support the current implementation of Grails Asset Pipeline.I think it is not compatible for grails 2.4.3 and above.
As it stands, you probably aren't importing the com.stripe.Stripe class which is why you're getting that particular message.
You don't need to attempt to manually assign the secret key to the Stripe class. Just define grails.plugins.stripe.secretKey in your Config.groovy and the plugin will handle the rest, as you can see in the plugin source.
I'm working up an ember-rails app that precompiles Handlebars assets through the rails pipeline. Everything is updated to the latest versions via bundle update.
When we load the index, the ember data is not rendered to the list, despite (app/views/gigs/index.html.erb):
<h1>Gigs</h1>
<script type="text/x-handlebars">
{{ view App.ListGigsView }}
</script>
<script type="text/javascript">
$(function() {
App.gigsController.loadAll(<%= #gigs.to_json.html_safe %>);
});
</script>
So, I checked the ListGigsView, which contains the following (app/assets/javascripts/app/views/gigs/list.js):
App.ListGigsView = Ember.View.extend({
templateName: 'app/templates/gigs/list',
gigsBinding: 'App.gigsController',
showNew: function() {
this.set('isNewVisible', true);
},
hideNew: function() {
this.set('isNewVisible', false);
},
refreshListing: function() {
App.gigsController.findAll()
}
});
Seems like the templateName is pointing fine (this is similar in, say, new.js). Nonetheless, the browser console (Google Chrome Version 25.0.1364.172) keeps returning the following:
Uncaught Error: assertion failed: You specified the templateName app/templates/gigs/list for <App.ListGigsView:ember194>, but it did not exist.
Here is the file at app/assets/javascripts/app/templates/gigs/list.handlebars:
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
</tr>
</thead>
<tbody>
{{#each gigs}}
{{view App.ShowGigView gigBinding="this"}}
{{/each}}
{{#if isNewVisible}}
<tr>
<td>*</td>
<td>
{{view App.NewGigView}}
</td>
</tr>
{{/if}}
</tbody>
</table>
<div class="commands">
<a href="#" {{action "showNew"}}>New Gig</a>
<a href="#" {{action "refreshListing"}}>Refresh Listing</a>
</div>
Why is the template not rendering? Running Ember.TEMPLATES in the console returns all the other view template links intact:
> Ember.TEMPLATES
=> Object {app/templates/gigs/edit: function, app/templates/gigs/event: function, app/templates/gigs/show: function, application: function}
But not the one for list. Why is that template AWOL?
For reference, I am running the following according to the browser console:
Ember.VERSION : 1.0.0-rc.1 ember.js:339
Handlebars.VERSION : 1.0.0-rc.3 ember.js:339
jQuery.VERSION : 1.9.1
-Edit: I have just discovered that including the Handlebars reference to the Event view in index.html.erb causes that template to disappear,too:
<h1>Gigs</h1>
<script type="text/x-handlebars">
{{ view App.ListGigsView }}
</script>
<script type="text/x-handlebars">
{{ view App.EventView }}
</script>
<script type="text/javascript">
$(function() {
App.gigsController.loadAll(<%= #gigs.to_json.html_safe %>);
});
</script>
Why does this happen? And how can I avoid it?
app/templates/ gets stripped from the template name by default. Try gigs/list.
For me it worked when stripping out templates, e.g. app/gigs/list.