Gitlab API: Job is not retryable - api

I want to automate building and deploying of projects. In this regard, I want to use GItlab API to retry particular jobs in Gitlab Pipeline.
The API call I make as follows:
curl --request POST --header "PRIVATE-TOKEN: xxx" "https://gitlab.example.com/api/v4/projects/1/jobs/1/retry"
My Job otherwise can be triggered manually and my gitlab-ci.yml file looks like this:
execute_octoDeploy:
tags:
- windows
stage: build
when: manual
script:
- .\BuildScripts\octodeploy.ps1
I get 403 error, which according to https://docs.gitlab.com/ee/api/README.html means that I am not authorized.
Whereas I have all the rights of the project and I am the owner of the same.
To add:
I can trigger other non-manual jobs but not this.
I also referred:
https://gitlab.com/gitlab-org/gitlab-ce/issues/22824
What might be wrong? How can I solve this?

I seem to have found the solution for this:
You need to add scope of the jobs in the API call
Also Retry method will work only when you have canceled your job in that pipeline.
You can try:
curl --header "PRIVATE-TOKEN: xxxxx" 'https://gitlab.example.com/api/v4/projects/1/jobs?scope[]=pending&scope[]=running'
and then specify if you want to play/retry a job.

Related

gitlab CI/CD pipelines: how to search by execution date

Is it possible to search for pipeline by execution date?
Example, I need to fine results of pipelines that was executed at 2023-01-31.
I have look around of net, but could not find any suggestion how to do this.
I do not know a GUI way, but you can use GitLab Pipelines API to search and filter pipelines programmatically.
Use updated_after or/and updated_before fields when requesting GET /projects/:id/pipelines API endpoint. Example from documentation without filtering:
curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/1/pipelines"
For full accepted parameters reference please follow the official documentation: List project pipelines.

Update file and commit with gitlab

I want to change the context of file and commit by using gitlab api. I looked the api documentation and couldn't find a way to do it.
The documentation link is here;
https://docs.gitlab.com/ee/api/
Is it possible to do change the file and commit it? Do you have any example?
Here is the official documentation for updating a file via the api: https://docs.gitlab.com/ee/api/repository_files.html#update-existing-file-in-repository
Example:
curl --request PUT --header 'PRIVATE-TOKEN: 9koXpg98eAheJpvBs5tK' 'https://gitlab.example.com/api/v4/projects/13083/repository/files/app%2Fproject%2Erb?branch=master&author_email=author%40example.com&author_name=Firstname%20Lastname&content=some%20other%20content&commit_message=update%20file'
Related question: Updating and committing file by using gitlab api
You should use git for this use case, not the API which is not designed for this!

How to run a rundeck job like a rest call from application?

I created a simple job of mkdir in rundeck.Now I wanted to run that job some node application.So how can I get the api for the job so that I can call that rest call from my application and run the job.
I just tried a post call for this from my postman but didnt worked.
http://rundeckhost:4440/api/1/job/uuid/run
Gave the following error:
(unauthenticated) is not authorized for: /api/1/job/ec0852b7-222a-4372-ad4b-808892777019/executions
Can someone point me to any references or any info on how can we run the job through a rest call from our application.Basically how to get the rest url for the job to run?
You have one of two ways to authenticate: http://rundeck.org/docs/api/#authentication
For your purpose it will probably be easier to use the authtoken type. See here for your choices for authtoken types: http://rundeck.org/docs/administration/access-control-policy.html#api-token-authorization
Roughly, you will do something like this:
curl -H "X-Rundeck-Auth-Token: $API_TOKEN" \
--data-urlencode "${NODEFILTER:-}" \
--data-urlencode "argString=${JOB_OPTIONS:-}" \
-X POST "${RD_URL}/api/12/job/$JOB_UUID/run"

autoregister new teamcity build agent via API

We use Ansible to configure build agent for different technology stacks like: frontend builder(Nodejs, libs, Dart SDK), backend builder(JDK), etc with Ansible orchestration tool and that easy to replace one linux with another be configuring new one from zero with orchestration except new teamcity agent registration.
Is it piossible to generate new authorizationToken for new agent with API call which can be used in programming language or register new agent via API call to be able to connect new linux box without Admin/human ?
There's REST API call to achieve this, just pass true or false string as request data via PUT request to the /httpAuth/app/rest/agents/<agentLocator>/authorized, <agentLocator> syntax is described here.
Here's an example of a curl command:
curl -X PUT "http://teamcity/httpAuth/app/rest/agents/id:3/authorized" --data true --header "Content-Type: text/plain" -u user:pass
PUT method should be used and Content-Type: text/plain header should be provided.

How can a Jenkins user authentication details be "passed" to a script which uses Jenkins API to create jobs?

I have a script that delete and re-create jobs through curl HTTP-calls and I want to get rid of any hard-coded "username:password".
E.g. curl -X POST $url --user username:password
Considerations:
Jenkins CLI (probably not an option).
One should be able to achieve the same with the CLI as with Jenkins API (creating jobs etc) but as far as I understand Jenkins CLI is not a good alternative for me since jobs created with will only appear in Jenkins after restarting or a "Reload Configuration from Disk", and that would cancel any other running jobs.
API token. Can't find out how to get the user token and then pass it
as a parameter to the script, but that may be a solution..
Try this way: (for example delete the job)
curl --silent --show-error http://<username>:<api-token>#<jenkins-server>/job/<job-name>/doDelete
The api-token can be obtained from http://<jenkins-server>/user/<username>/configure.
This worked for me:
curl -u $username:$api_token -FSubmit=Build 'http://<jenkins-server>/job/<job-name>/buildWithParameters?environment='
API token can be obtained from Jenkins user configuration.
With Jenkins CLI you do not have to reload everything - you just can load the job (update-job command). You can't use tokens with CLI, AFAIK - you have to use password or password file.
Token name for user can be obtained via http://<jenkins-server>/user/<username>/configure - push on 'Show API token' button.
Here's a link on how to use API tokens (it uses wget, but curl is very similar).
I needed to explicitly add POST in the CURL command:
curl -X POST http://<user>:<token>#<server>/safeRestart
I also have the SafeRestart Plugin installed, in case that makes a difference.
If you want to write a script to automate creation of jobs using the Jenkins API, you can use one of the API clients to do that. A ruby client for Jenkins is available at https://github.com/arangamani/jenkins_api_client
gem install jenkins_api_client
require "rubygems"
require "jenkins_api_client"
# Initialize the client by passing in the server information
# and credentials to communicate with the server
client = JenkinsApi::Client.new(
:server_ip => "127.0.0.1",
:username => "awesomeuser",
:password => "awesomepassword"
)
# The following block will create 10 jobs in Jenkins
# test_job_0, test_job_1, test_job_2, ...
10.times do |num|
client.job.create_freestyle(:name => "test_job_#{num}")
end
# The jobs in Jenkins can be listed using
client.job.list_all
The API client can be used to perform a lot of operations.
API token is the same as password from API point of view, see source code uses token in place of passwords for the API.
See related answer from #coffeebreaks in my question python-jenkins or jenkinsapi for jenkins remote access API in python
Others is described in doc to use http basic authentication model
In order to use API tokens, users will have to obtain their own tokens, each from https://<jenkins-server>/me/configure or https://<jenkins-server>/user/<user-name>/configure. It is up to you, as the author of the script, to determine how users supply the token to the script. For example, in a Bourne Shell script running interactively inside a Git repository, where .gitignore contains /.jenkins_api_token, you might do something like:
api_token_file="$(git rev-parse --show-cdup).jenkins_api_token"
api_token=$(cat "$api_token_file" || true)
if [ -z "$api_token" ]; then
echo
echo "Obtain your API token from $JENKINS_URL/user/$user/configure"
echo "After entering here, it will be saved in $api_token_file; keep it safe!"
read -p "Enter your Jenkins API token: " api_token
echo $api_token > "$api_token_file"
fi
curl -u $user:$api_token $JENKINS_URL/someCommand