Fail to update gitlab CICD variable using gitlab API - gitlab

I am exploring Gitlab APIs to work with CICD. For updating gitlab CICD variable, I use following API
PUT /projects/:id/variables/:key
here is the document regarding it. This API works perfect if the variable is unique for CICD. But If I use duplicate name variable with different environment_scope, it some time update my variable but most time fail to update. It throws following error
key: [MY_KEY] has already been taken
I checked in gitlab issues here but didn't found any proper solution regarding it.
For reference, my gitlab version is GitLab Community Edition 12.8.1
Can anybody help me regarding this?.. Thanks in advance.

From the documentation found here, It appers you need to amend the follow to the end of the line:
--form "value=updated value"
So ultimately you will have:
PUT /projects/:id/variables/:key --form "value=updated value"

Related

Issue with build pipeline

I am trying to implement a CI/CD pipeline as mentioned in this video by "tech with nana".
But I always end up with this error which says -make: command not found.
However, I have written in my pipeline code as explained by her in the video.
How do I fix this?
Check first if your YAML is correctly written.
Typically, -make should be - make (note the space between - and make).
The GitLab pipeline editor can help verify the configuration syntax automatically.

Use Gitlab API to create variables on project

I am trying to create a GitLab variable for a project on GitLab using a cURL command.
I am following the API and the relevant paragraph here.
Looking at the example on the docs, I cannot find any evidence on how Gitlab knows which repo I am trying to add the variable to.
I tried adding a private token and runnnig the command and I ended up with a 403.
Could someone please explain how to use the GitLab API to add variables to a repo through a cURL command?
Looking at the example on the docs, I cannot find any evidence on how Gitlab knows which repo I am trying to add the variable to
But... the all idea behind "instance-level" variables is to be "global", not tied to a specific project.
See issue 14108:
Global (instance-level) variables for CI builds
Less of an issue or more or a feature/question perhaps ... It would nice to support global variables in the CI builds; classic use-case being deployment keys / docker registry credentials.
Problem to solve
Without support for global variables users must manually enter the same credentials repeatedly for dozens of projects when migrating to GitLab for details like:
docker private registry credentials
kubernetes credentials)
deployment keys
Proposal
Implement support for global (instance-level) variables for CI builds by:
Re-use the refactor/re-design of CI variables in project/group settings
Place under CI/CD section in the admin settings
A better API for your case would be:
Create variable (project
curl --request POST --header "PRIVATE-TOKEN: <your_access_token>" \
"https://gitlab.example.com/api/v4/projects/1/variables" \
--form "key=NEW_VARIABLE" --form "value=new value"

gitlab API - how can I create a branch from an issue

In git lab, you can create an issue, then within the issue you can create a branch. This branch is linked to the issue (I think because of the issue number at the start of the branch name), such that when you do a merge request on that branch it automatically closes the issue.
So my question is - how can you do this via the API? I can create the issue, but there is no control (as far as I can see) within the issue API to create the related branch.
Is that possible?
It would be nice to be able to create an issue with branch in one go - but I don't think that is possible?
Ok - its not perhaps the best answer, but here is what I came up with for a interim solution (in linux bash):
Raise the issue store response in cmd_resp
Extract the issue ID: echo $cmd_resp | grep -o -P '(?<=iid":).*(?=,"project_id)'.
Where the issue ID is found by looking for: iid":<ISSUE-ID>,"project_id
Create a branch with the name: <ISSUE-ID>-some-branch-name - by having the issue ID at the start of the branch name, gitlab automatically makes a relation to that issue.
So - its quite a simple approach, but it does not feel very integrated. I would still prefer to do that from the point of view of the issue.
It is not possible to create a branch related to an issue via the issues API.
However, this is in line with how RESTful APIs should be designed. If you want to create a branch, you need to make a POST request to the branches API.
POST /projects/:id/repository/branches
As you have already found out, GitLab is quite good at automatically linking issues, MRs and branches together.
For a branch to be linked to an issue, simply start the branch with the issue ID. However, usually it is enough if a merge request is linked to an issue. In my opinion, you shouldn't really be concerned with the branch. You can later access the branch via issue->MR->branch
Merge requests are linked to issues whenever a MR's description text links to an issue (e.g. #1). If you add an issue ID to the Closes statement, the issue will also be closed upon MR resolution.
Therefore, you could simply create a branch via the API, name it however you want. Then, create a MR from that issue and include Closes #1 in your MR description, where 1 is your issue ID.
Further, I would recommend using a more sophisticated REST client. You shouldn't have to parse the issue ID yourself. It is properly set as a field in the JSON response.

Gitlab CI - unique build number

Is there any unique number in Gitlab CI which can be used as build number as we use in Jenkins. I came to know about the variable "CI_PIPELINE_IID" but the problem with this variable is, it updates for all branches and there is no such variable exist for each branch.
We solved the same issue using the variable $CI_PIPELINE_IID, which is defined as
The project-level IID (internal ID) of the current pipeline. This ID is unique only within the current project.
And is quite similar to Jenkins' $BUILD_NUMBER
See also https://docs.gitlab.com/ee/ci/variables/predefined_variables.html#variables-reference
It looks like CI_COMMIT_SHA or CI_COMMIT_SHORT_SHA are great candidates for this as they give you a reference to the commit it was built from.
For example, if you want to use it as a docker image tag, use
docker build . -t myapp:$CI_COMMIT_SHA
Note that earlier versions of Gitlab (version 8.x) use CI_BUILD_TAG
More variables on: https://docs.gitlab.com/ee/ci/variables/predefined_variables.html#variables-reference
You can check if any of the other ids "CI variables" might work in your case:
CI_JOB_ID 9.0 all The unique id of the current job that GitLab CI uses internally
CI_PIPELINE_ID 8.10 all The unique id of the current pipeline that GitLab CI uses internally (one I)
CI_PROJECT_ID all all The unique id of the current project that GitLab CI uses internally
CI_RUNNER_ID 8.10 0.5 The unique id of runner being used
Yeah,it's a reasonable request for many situations. Someone has issued it before, please refer to: https://gitlab.com/gitlab-org/gitlab/-/issues/23844. But it seems that it has not added this so far.

How change the fixed CI/CD variables from repo settings by request?

I'd like to know if is it possible change the CI/CD variables assigned in repo settings by request. I know previously i can pass env variables by curl request to trigger the pipeline.
But now i have another situation which an automatic build and a docker image is pushed to a private registry happen when the master or dev branch suffers a merge. To do this, i use the CI/CD variables already setted.
And oftenly i will create a new registry, which it is done automatically in a bash script. After this what i'd like is setup the new keys accesses to the gitlab repo via request or other automatic way.
If someone could help me with some idea, thanks in advance.
If I am not mistaken this it what you are looking for:
Project-level Variables API
https://docs.gitlab.com/ee/api/project_level_variables.html

Resources