Jenkins pipeline: checkout does not preserve Git timestamps - gitlab

I am trying to build a Jenkins pipeline (with a Windows agent, if this could be relevant) for a project in C/C++. My jenkinsfile contains the following lines:
stage('Clone') {
steps {
[...]
checkout changelog: false, poll: false,
scm: [$class: 'GitSCM', branches: [[name: "$gitlabBranch"]],
doGenerateSubmoduleConfigurations: false,
extensions: [],
submoduleCfg: [],
userRemoteConfigs: [[credentialsId: 'gitlab-credentials', url: "$gitlabSourceRepoHttpUrl"]]]
[...]
And the logfile is:
[Pipeline] checkout (hide)
using credential gitlab-credentials
Fetching changes from the remote Git repository
skipping resolution of commit remotes/origin/master, since it originates from another repository
Checking out Revision ccc3e1e628ffa2a4e98d847ea952f048724ffaf5 (refs/remotes/origin/master)
> git rev-parse --is-inside-work-tree # timeout=10
> git config remote.origin.url [URL REDACTED] # timeout=10
Fetching upstream changes from [URL REDACTED]
> git --version # timeout=10
using GIT_ASKPASS to set credentials gitlab-credentials (https-pipeline)
Setting http proxy: [REDACTED]
> git fetch --tags --progress -- [URL REDACTED] +refs/heads/*:refs/remotes/origin/* # timeout=10
> git rev-parse "refs/remotes/origin/master^{commit}" # timeout=10
> git rev-parse "refs/remotes/origin/origin/master^{commit}" # timeout=10
> git config core.sparsecheckout # timeout=10
> git checkout -f ccc3e1e628ffa2a4e98d847ea952f048724ffaf5 # timeout=10
Commit message: "Update Jenkinsfile"
> git rev-list --no-walk 628bc28a2c03a0f6f10ca1a75420c06efef16177 # timeout=10
The problem is, all data is fetched with timestamp set to the time of fetch, and therefore all files are compiled. What should I change?
I just checked, and I am sure that it is the checkout which retrieves everything: before that line, there are the old files, after that everything is updated.

Related

Handling hotfix in gitversion

I'm trying to understand how GitVersion works.
Here there is a snippet for 'gitversion /showconfig'
branches:
develop:
mode: ContinuousDeployment
main:
mode: ContinuousDelivery
hotfix:
mode: ContinuousDelivery
...
After shipping the release/1.1, my git is in this state:
Master branch has tags 1.0.0 and tag 1.1.0.
Develop branch is in 1.2.0-alpha.1.
Now I tried to to simulate an hotfix in production for the release 1.1.0
> git checkout master
> gitversion /showvariable FullSemVer
1.1.0
> git checkout -b fix/1.1
> gitversion /showvariable FullSemVer
1.1.0
> Add-Content -Name EmptyFile7.txt -Value 'Correction'
> git add --all; git commit -m "fix(gitversion): modified EmptyFile7.txt"
> gitversion /showvariable FullSemVer
1.2.0-fix-1-1.1+1
I expected 1.1.1-fix-.... and now I don't know how to tag this fix.
For example, is this right?
> git checkout master
> git merge hotfix/1.1
> git tag 1.1.1
Should I handle all this not as a bugfix but in develop/release way?
Riccardo
The right branch was hotfix and not fix
> git checkout master
> gitversion /showvariable FullSemVer
1.1.0
> git checkout -b hotfix/1.1.1
# Fix
> Add-Content -Path EmptyFile7.txt -Value 'Correction'
> git add --all; git commit -m "fix(gitversion): modified EmptyFile7.txt"
> gitversion /showvariable FullSemVer
1.1.1-beta.1+1
Now I can switch to master and approve the hotfix
> git checkout master
> git merge hotfix/1.1.1
> git tag 1.1.1
and finally merge back to develop
> git checkout develop
> git merge hotfix/1.1.1

git push failing with Access denied to Gitlab

I am using git to push my code to Gitlab. Following are the commands I am using
git clone https://<mygitlab.com>/gke-app-namespace-deploy.git
cd gke-app-namespace-deploy
git checkout -b master
git branch
main
* master
git commit -m "first commit"
git remote set-url origin https://<mygitlab.com>/askar/gke-app-ns-deploy.git
$ git push -u origin master
Username for 'https://<mygitlab.com>': askar
Password for 'https://askar':
remote: HTTP Basic: Access denied
fatal: Authentication failed for 'https://<mygitlab.com>/askar/gke-app-ns-deploy.git/'
The same credentials works while downloading the code but I am getting Access Denied message while uploading the code.
What I am trying to do is clone code from gke-app-namespace-deploy.git' and push to a new repository 'gke-app-ns-deploy.git. Not sure if git by design will allow this.
Try,
git remote add origin https://<access-token-name>:<access-token>#gitlab.com/myuser/myrepo.git
From: https://stackoverflow.com/a/52074198/2675670

How to add the 'DefaultCollection', when cloning a repo in yaml

I have this YAML snippet, to clone a second repo in my pipeline.
resources:
repositories:
- repository: clone-scripts
type: git
name: 'DevOpsProject/pipeline-scripts'
ref: 'refs/heads/main'
however, when executed, it shows this error
git remote add origin https://dev.visualstudio.com/DevOpsProject/_git/pipeline-scripts
git config gc.auto 0
git config --get-all http.https://dev.visualstudio.com/DevOpsProject/_git/pipeline-scripts.extraheader
git config --get-all http.proxy
git config http.version HTTP/1.1
git -c http.extraheader="AUTHORIZATION: bearer ***" fetch --force --tags --prune --progress --no-recurse-submodules origin
remote: TF401019: The Git repository with name or identifier pipeline-scripts does not exist or you do not have permissions for the operation you are attempting.
fatal: repository 'https://dev.visualstudio.com/DevOpsProject/_git/pipeline-scripts/' not found
what I have seen, is that it is not taking into account the 'DefaultCollection' when executing
git remote add origin https://dev.visualstudio.com/DefaultCollection/DevOpsProject/_git/pipeline-scripts (OK)
But how do you execute that, it's wrong
git remote add origin https://dev.visualstudio.com/DevOpsProject/_git/pipeline-scripts
Any ideas? in the Azure documentation, I have read but i can't find any information about that.
resources:
repositories:
- repository: clone-scripts
type: git
name: 'DefaultCollection/DevOpsProject/pipeline-scripts'
ref: 'refs/heads/main'
Its just to check those permisions in repo config

Commit has bugs

I committed a branch and it is merged with master. Later on production, it is found to be bug in the commit.
I need to raise a PR with previous commit of master against master.
I tried the following
git checkout <specific-commit-id>
git checkout -b <new-branch-name>
git push origin <new-branch-name>
When I try to raise PR against master, it says
The source branch is 2 commits behind the target branch
Expected it to allow me to merge it in master but it didn't allow
How can I do so?
Follow the steps below,
git checkout <branch-name>
git checkout -b <new-branch-name> <properly-working-commit-id-of-current-branch-which-we-want-to-deploy>
git add .
git commit --amend -m 'commit-name'
git push origin <branch-name>
By doing so it allows us to merge in GitLab

Push files to gitlab-ci via CI runner

I am using gitlab CI runner to test my code and generating some files. I just want to push the generated files to gitlab repository via CI runner. Is there any way to do that ?
I have resolved this issue by doing this:
Note: If you want to git push to a non protected branch do not set the runner variable as protected
Generate new gitlab access token with api scope: User Settings > Access Tokens
Add a protected CI variable into your project settings with the new token: Your project > Settings > Secret variable using variable name CI_PUSH_TOKEN
Add another protected CI variable with your username using variable name CI_USERNAME
Then you can use this token instead of the default in you gitlab-ci script. for example:
before_script:
- git remote set-url origin https://${CI_USERNAME}:${CI_PUSH_TOKEN}#gitlab.com/${CI_PROJECT_NAME}.git
- git config --global user.email '${GITLAB_USER_EMAIL}'
- git config --global user.name '${GITLAB_USER_ID}'
...
- git checkout -B branch
- # do the file changes here
- git commit -m '[skip ci] commit from CI runner'
- git push --follow-tags origin branch
Generated a SSH Key in gitlab
--> Profile Settings --> SSH Keys --> Generate It
After generating the SSH Key store that in the gitlab variables named SSH
--> Project Settings --> Variables --> Add Variable
In the .gitlab-ci.yml add the below lines.
before_script:
- mkdir -p ~/.ssh
- echo "$SSH" | tr -d '\r' > ~/.ssh/id_rsa
- chmod 600 ~/.ssh/id_rsa
- ssh-keyscan -H 'Git_Domain' >> ~/.ssh/known_hosts
After that pushed the files to the repository using this below js code.
var child_process = require("child_process");
child_process.execSync("git checkout -B 'Your_Branch'");
child_process.execSync("git remote set-url origin Your_Repository_Git_Url");
child_process.execSync("git config --global user.email 'Your_Email_ID'");
child_process.execSync("git config --global user.name 'Your_User_Name'");
for (var i=0;i<filesToBeAdded.length;i++) {
child_process.execSync("git add "+filesToBeAdded[i]);
}
var ciLog = child_process.execSync("git commit -m '[skip ci]Automated commit for CI'");
var pushLog = child_process.execSync("git push origin Your_Branch");
[skip ci] is most important in commit message. Otherwise it will start a infinity loop of CI process.
You could use of course SSH keys but you could also provide user and password (user with write access) as secret variables and use them.
Example:
before_script:
- git remote set-url origin https://$GIT_CI_USER:$GIT_CI_PASS#gitlab.com/$CI_PROJECT_PATH.git
- git config --global user.email 'myuser#mydomain.com'
- git config --global user.name 'MyUser'
You have to define GIT_CI_USER and GIT_CI_PASS as secret variables (you could always create dedicated user for this purpose).
With this configuration you could normally work with git. I'm using this approach to push the tags after the release (with Axion Release Gradle Pluing - http://axion-release-plugin.readthedocs.io/en/latest/index.html)
Example release job:
release:
stage: release
script:
- git branch
- gradle release -Prelease.disableChecks -Prelease.pushTagsOnly
- git push --tags
only:
- master
Another solution using Gitlab API to commit back a file .terraform.lock.hcl in terraform/ directory on $CI_COMMIT_BRANCH with [skip ci] :
script:
- 'STATUS=$(curl -Ss --head --header "JOB-TOKEN: $CI_JOB_TOKEN" "$CI_API_V4_URL/projects/$CI_PROJECT_ID/repository/files/terraform%2F%2Eterraform%2Elock%2Ehcl?ref=$CI_COMMIT_BRANCH" | grep "HTTP/1.1" | cut -d " " -f2)'
- if [[ $STATUS == "404" ]]; then ACTION="create"; else ACTION="update"; fi
- 'curl --request POST --form "branch=$CI_COMMIT_BRANCH" --form "commit_message=[skip ci] terraform.lock.hcl from pipeline" --form "actions[][action]=$ACTION" --form "actions[][file_path]=terraform/.terraform.lock.hcl" --form "actions[][content]=<.terraform.lock.hcl" --header "JOB-TOKEN: $CI_JOB_TOKEN" "$CI_API_V4_URL/projects/$CI_PROJECT_ID/repository/commits"'
The Feature you are looking for is called Artifacts. Artifacts are files which are attached to a build when they are successful.
To enable an Artifact put this in your .gitlab-ci.yml:
artifacts:
paths:
- dir/
- singlefile
This will upload the dir directory and the file singlefile back to GitLab.

Resources