Jenkins Artifactory Plug-in - jenkins

I am new to Artifactory so I apologize if this question has already been answered. I am using rtPromote to Promote Artifactory from one repo (dev) to another repo (qa). Both dry run and performing promotion completes successful. However, when I check the repo the file wasn't copied over. See snippet below.
Am I missing something with rtPromote or do I need to use something different than rtPromote with Jenkins Atrifactory Plug-in? Thank you in advance.
Performing dry run promotion (no changes are made during dry run)
{
"messages" : [ ]
}
Dry run finished successfully.
Performing promotion
{
"messages" : [ ]
}
Promotion completed successfully!

Related

Build job when certain files have been updated

I am writing a Jenkinsfile to do a number of checks on a repo. The check runs everytime a PR has been created. The goal of my check is to make sure that in my PR, 2 particularly files (a.txt) and (b.txt) have been changed.
The issue is that these 2 files always must be changed when a PR is issued and I dont want my developers to forget about updating these files. So I want to write a check that fails the build if these 2 files have not been updated. I tried something like this
stage('Check if file changed'){
when {
allOf {
changeset "a.txt"
changeset "b.txt"
}
}
steps {
// Do something
}
}
But the issue with this is that it wants the change to happen in the commit. A developer could create a PR with one file updated and then add another commit to update the other file but this check will always want both files updated in the latest commit. Can someone suggest a way for me to accomplish what I need ? Also in the above case, it doest fail the build if a file is not updated. It just skips the stage. I want the build to fail. Any suggestions ?
Locally running gives me the following output
git diff --name-status master
M CHANGELOG.md
M Jenkinsfile
I want to use this in my jenkins build but there I get the error
+ git diff --name-status master
fatal: ambiguous argument 'master': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
script returned exit code 128
The pattern you have used in allOf{} inside when{} DSL is wrong. It would be something like this
pipeline {
agent any;
stages {
stage('build') {
when {
allOf{
changeset "README.md"; changeset "README-one.md"
}
}
steps {
echo "from build"
}
}
}
}
note : both changeset has to be seperate with ; not , or space. I can see In Jenkins documentation they have mention , - might be thats a mistake

How to disable "build now" option?

For a given scripted pipeline(jenkins), the pipeline should only get triggered through webhook from GitLab
Build Now option should be disabled for that pipeline.
Can we configure Jenkins, to disable Build Now option for a specific pipeline script job in jenkins?
EDIT: Here the solution with an scripted Pipeline:
node {
def userIdCause = currentBuild.getBuildCauses('hudson.model.Cause$UserIdCause')
stage("Authorize Usage") {
if (userIdCause.size()) {
error('Aborting Build due to manual start - thats not permitted!')
}
}
}
How about the following solution without any extra plugin on an declarative pipeline:
pipeline {
...
stages {
stage ("Authorize Usage") {
when { expression { getCause() == "USER" } }
steps {
currentBuild.description = 'Aborting Build due to manual start - thats not permitted!'
error('Aborting Build due to manual start - thats not permitted!')
}
}
...
}
Have taken a look at this plug-in supplied on the Jenkin's site? Matrix Authorization Strategy Plugin :
Matrix Strategy
Specifically this sectionL Allow configuring per-agent permissions. This allows e.g. restricting per-agent build permissions when using the Authorize Project plugin (JENKINS-46654)
Not ideal, but if this is a 'freestyle pipeline job'
a quick workaround is to add a build step "Execute shell" as first step. You can use this to prevent a build, when noting has changed.
Every time your sources changes and you push to your repo, a build will have been triggered and as there are changes this script will not exit.
When you click the 'Build now', nothing should have changed in your repo (as the only way it can is through a push which would then trigger a build) it will causes an exit, and fail the build.
if [[ $GIT_COMMIT -eq $GIT_PREVIOUS_COMMIT ]]
then
echo "Exiting build - Nothing has changed"
echo "This is to prevent the usage of Jenkins 'build now'"
exit 1
fi
EDIT: This is the answer to the question of user #mohet in the comments of my other answer because it was to long for the comment section (https://stackoverflow.com/a/55058788/7746963).
The currentBuild variable, which is of type RunWrapper, may be used to refer to the currently running build...
Source: https://opensource.triology.de/jenkins/pipeline-syntax/globals .
hudson.model is the package name of most corresponding core jenkins classes. 'Hudson' because jenkins was once cloned from the codebase of his ancestor named 'hudson'.
You can look up them here: https://javadoc.jenkins.io/hudson/model/package-summary.html .
There you will also find https://javadoc.jenkins.io/hudson/model/Cause.UserIdCause.html . To specify directly the package$classname in some methods like getbuildcauses is the straightforward thought of jenkins dev Team. This reduces the failure potential and makes the code better readable and understandable.

Jenkins Artifactory Plugin - Fail build if upload fails

When configuring the Artifactory Plugin for "Generic-Artifactory" integration, someone changed the artifact name without updating the Jenkins2 plan and the upload no longer worked. Unfortunately, the Jenkins build never failed or warned us.
Is there an option in the specs that I have yet to find that would allow it to fail the build in this case?
I'm sure there's an obvious answer here somewhere, but I'm missing it. I don't want to write a script that checks for the artifact and exits if it's not there, although it would work. I'm looking for the right way to do this.
https://www.jfrog.com/confluence/display/RTF/Using+File+Specs
{
"files": [
{
"pattern": "$WORKSPACE/foobar.jar",
"target": "libs-release-local/com/mycompany/foo-1.1.jar"
}
]
}
Your desired functionality is the fail-no-op flag, which will fail the build if no files were affected (uploaded/downloaded) during the process.
The fail-no-op flag is only available in pipeline jobs, both in declarative and scripted syntax.

Jenkins : Build not failing even the code is incorrect syntax wise

I missed one of the semicolon to see the red build on the jenkins, while using the Git
public class SpringbootController {
public void callSerivce() {
System.out.println("to check se changes");
System.out.println("to check se changes")
}
}
but still on the jenkins, it is showing the build is successful.
don't know what exactly i missed,please help
newbie in jenkins
is there anything i need to add in the shell to make it work, right now it is empty.
To sum up the conversation in the comments: in order for Jenkins to build your code you need to tell it how to build your code. By default Jenkins doesn't do anything beyond the SCM checkout on its own.
Since you're building a Java project with Maven you should add a Maven build step after your SCM checkout. You will need the Maven Project Plugin.
In your post-build steps you should add an "Archive the artifacts" step to gather up anything built during your job you want to keep since your workspace will get potentially wiped out next run.

How can I create a jenkins job, who must execute test on commit?

I'm beginning with Jenkins.
I want, that each time I do a git commit (or push?), that the jasmine test of my ionic project was executed and must work before the commit can be done.
In reality, it has 2 questions:
How execute jasmine test with Jenkins?
In this moment I execute the test with:
npm test
How can I do for executing this tests with a commit (or a push)?
Thanks
Best regards
You have two ways to achieve the task.
GIT Hook: From GIT after commit or push execute the Jasmine test
Jenkins Trigger with GIT Hook: From Jenkins check the repo and execute the Jasmine test
Hooks from GIT
Look for the hidden directory in your git repo, you'll find a directory called "hooks" and inside it many examples of hooks:
First list the content of your repo main directory:
ls -ltra
You should see something like:
m.ortiz.montealegre#CPX-XYR3G1DTHBU ~/-argentina/.git
$ vim hooks/
applypatch-msg.sample pre-applypatch.sample pre-push.sample update.sample
commit-msg.sample pre-commit.sample pre-rebase.sample
post-update.sample prepare-commit-msg.sample pre-receive.sample
You have a whole guide of how to setup hooks here.
In your case maybe update would do the thing:
update The update script is very similar to the pre-receive script,
except that it’s run once for each branch the pusher is trying to
update.
Triggers from Jenkins with GIT Hooks
In this one you'll setup your Jenkins project build trigger with "Poll SCM" but do not specify a schedule.
Then with a post-receive hook from GIT notify the Jenkins Job about the changes:
http://yourserver/jenkins/git/notifyCommit?url=<URL of the Git repository>?token=<get token from git to build remotely>
I found that example here.
Run the Jasmine tests
I don't know which O.S you're using but I hope it's a beautiful Linux box.
You can achieve pretty much the same with Jenkins. You need to consider the user (your user) and its permissions and check if the user which runs the Jenkins instance is allowed to execute the same.
Just create a new Jenkins Project and add a shell execution step with the test just like you said:
npm test
There are many questions regarding your particular environment, but I think that this will be a good guide for you.
There was a ticket about adding this functionality.
Finally the ticket was closed
slackersoft commented on 2 Dec 2016
At this point, I think it makes more sense to leave the code to do the watching of your specs and production code to one of the many external libraries that are built specifically for that.
The relate external library can be:
jasmine-node
nodemon
mochajs
gaze

Resources