Bash script successfully executes locally but not in Azure Devops pipeline - bash

I have a bash script that executes successfully in my local environment:
#!/bin/bash
#build changelog
version="Version: "
current_date=`date`
cat <(printf " \n") <(echo $version $env:BUILD_BUILDNUMBER $current_date) <(tail -n+6 pr-changelog.md) ./CHANGELOG.md > output
mv output ./CHANGELOG.md
However using the the Bash build package in my Azure Devops pipeline, whilst the script succeeds, does not throw any errors and the pipeline completes, the changes in the script do not take place.
The script copies lines from one file to the CHANGELOG.md and a couple of other small things. Locally the text is copied, but after running the pipeline, the CHANGELOG.md file has not changed in my branch.
Can I expect this to work and if not, what further steps should I take?
TIA

Bash script successfully executes locally but not in Azure Devops pipeline
When we execute the build on the Azure pipeline, it will checkout the source from the repo to the build agent and the next operation is also done on the agent. It will not directly affect the repo, this way will protect the security of the source in our repo.
That is the reason why the changes in the script do not take place in your branch.
To resolve this issue, we need to submit this change to the repo by the git command: git add git commit and git push:
git config --global user.email "xxx#xyz.com"
git config --global user.name "Admin"
cd "$(System.DefaultWorkingDirectory)"
version="Version: "
current_date=`date`
cat <(printf " \n") <(echo $version $env:BUILD_BUILDNUMBER $current_date) <(tail -n+6 pr-changelog.md) ./CHANGELOG.md > output
mv output ./CHANGELOG.md
git add CHANGELOG.md
git commit -m "copies lines to CHANGELOG.md"
git push https://<PAT>#dev.azure.com/YourOrganization/YourProject/_git/YourProject HEAD:master
I test it with Azure pipeline, it works fine.

Related

Why all the script in my git hooks (pre-commit, post-commit, pre-receive, pre-push etc) do not run?

Why all the script in my git hooks (pre-commit, post-commit, pre-receive, pre-push etc) do not run?
Note:
this question is not a duplicate;
I have try the answer to each of the other questions but none of them work.
I did chmod +x, added the path to hook. rename script, neither of them solve my issue.
Inside my git
branches config description HEAD hooks info objects refs
Inside hooks:
applypatch-msg.sample fsmonitor-watchman.sample post-update.sample pre-commit prepare-commit-msg.sample pre-rebase.sample update.sample
commit-msg post-merge.sh pre-applypatch.sample pre-commit.sample pre-push pre-receive
I run them manually and they are all working fine.:
$ bash pre-commit
You are about to commit to master
Do you really want to do this? [y/n] y
pre-commit script
#!/bin/sh
#
# An example hook script to verify what is about to be committed.
# Called by "git commit" with no arguments. The hook should
# exit with non-zero status after issuing an appropriate message if
# it wants to stop the commit.
#
# To enable this hook, rename this file to "pre-commit".
echo "You are about to commit" $(git diff --cached --name-only --diff-filter=ACM)
echo "to" $(git branch --show-current)
while : ; do
read -p "Do you really want to do this? [y/n] " RESPONSE < /dev/tty
case "${RESPONSE}" in
[Yy]* ) exit 0; break;;
[Nn]* ) exit 1;;
esac
done
But when i git commit and git push to the repository none of the scripts work.
$git commit -m "Test hooks"
[master] Test hooks 1 file
changed, 1 insertion(+)
My git version is 2.39.1
I created the repository on a VM with Ubuntu 18.04.6 LTS installed
Here was the procedure fro creating the repo.
mkdir project1.git
cd project1.git
git init --bare
After the creation i clone the repo to my local computer (windows).
Clone the git repository
git clone git#{ip}:/home/git/git_repositories/project1.git/
But I want use the scripts in project1.git/hooks to make this work.
A pre-commit hook for instance would not work in a bare repository (which has no working tree).
It would work only in your cloned repository, on Windows, where you can create a myClonedRepo/.git/hook/pre-commit script (no extension, no .sh), which will be run before each commit.
From the comments:
all users could create/clone their repositories from a shared Git template repository. The article "Creating a Custom Git Template" gives an illustration of that approach, but means that:
every user must access the same shared folder
they need to activate the init.templateDir config setting in their global Git configuration
any check which must be enforced for all the team members, especially for a distributed team, is best managed by server-side hooks instead.

How to add a Gitlab CI job to validate generated open-api documentation is up-to-date

I would like to add a Gitlab job:
Check if the generated files are different that the ones in git
Could you please help me?
You can use git diff --exit-code after running the generation script to assert whether files have changed.
For example, suppose you modify an existing file then run git diff --exit-code, the command will exit non-zero. In a gitlab job, that means the job will fail.
$ echo "foo" >> existing-file.txt
$ git diff --exit-code # exits non-zero (failure)
So, you could have a gitlab job that runs your generation script then checks if the files have changed. If the files don't change, the command exits 0 and the job passes.
check_openapi:
stage: .pre
# ...
script:
- ./mvnw verify # generate the openapi docs
- git diff --exit-code # fails if the files tracked by git have changed
It's important to note that git diff will only work on tracked git files. Therefore, if your generation code potentially adds new files, you should make sure that you run git add --intent-to-add for any newly created files. Otherwise, you may miss some cases because new files aren't tracked by git by default. You can add this to the CI job or you can incorporate it into your code generation script.
In example:
$ echo "foo" > newfile.txt
$ git diff --exit-code # exits 0 (success?!)
$ echo "foo" > newfile.txt
$ git add --intent-to-add ./newfile.txt
$ git diff --exit-code # exits non-zero (failure)
So, if your generation script doesn't run git add --intent-to-add as part of its process, a complete solution may look like this:
check_openapi:
stage: .pre
# ...
script:
- ./mvnw verify
- git add --intent-to-add . # make sure new files are tracked/diff'd
- git diff --exit-code

Backup using GIT - add, commit and push everything including other GIT repositories

I want to build a backup system. I have a server (an old pc) and I boot it up via magic packets. For this purpose, I've written a batch Script doing this. On the server is a git server running with a couple of repositories. I can easily push to it.
But here's the problem:
My project folder contains itself a couple of git repositories. If I now add all files with git add . an Error is thrown, that I should add the submodules. This is the post talking about it Automatically Add All Submodules to a Repo. I've modified the shell script, to work with spaces, and to automatically commit and push everything while executing.
cd "D:\Projekts"
find . -type d | while read x ; do
if [ -d "${x}/.git" ] ; then
cd "${x}"
origin="$(git config --get remote.origin.url)"
cd - 1>/dev/null
echo ""
echo git submodule add "${origin}" "${x}"
git submodule add "${origin}" "${x}"
fi
done
echo "done adding all submodules"
git add .
git commit -am "new backup"
git push
echo "done pushing"
And this doesn't throw an error so far. But still the folders containing all those repositories are empty, if I clone the repository.
sidenote: I add the remote to the backup repository like this: $ git remote add --mirror=fetch origin git#<ip>:backup.git
Thanks in advance for your time,
Hellow2 :)

unity cloud build Post-Build Script upload to github pages

I run my game on my Github pages.
and recently started using unity cloud builds.
I managed to get the post-build script running.
but have no clue on how to get it to pull the repository, apply the new build, and push it to GitHub.
So far I have tried this script, without much luck
#!/bin/sh
set -x
ssh-agent $(ssh-add ./id_rsa; git clone git#github.com:triktron/Road-Rage.git tmpResp; cp -r ./WebGL/Build ./tmpResp/;cd ./tmpResp/; git add Build; git commit -m "auto build"; git push)
the logs responded with this
2838: Executing Post-Build Script at upload.sh
2839: ###########################################################
2840: # WARNING: UNPROTECTED PRIVATE KEY FILE! #
2841: ###########################################################
2842: It is required that your private key files are NOT accessible by others.
2843: This private key will be ignored.
2844: ++ git clone git#github.com:triktron/Road-Rage.git tmpResp
2845: Cloning into 'tmpResp'...
2846: ++ cp -r ./WebGL/Build ./tmpResp/
2847: cp: ./WebGL/Build: No such file or directory
2848: ++ cd ./tmpResp/
2849: ++ git add Build
2850: ++ git commit -m 'auto build'
2851: *** Please tell me who you are.
2852: Run
2853: to set your account's default identity.
2854: Omit --global to set the identity only in this repository.
2855: ++ git push
2856: Everything up-to-date
2857: + ssh-agent
2858: SSH_AGENT_PID=5387; export SSH_AGENT_PID;
2859: WORKSPACESIZE | ARTIFACTSSIZE
2860: --------------|--------------
2861: 154.10 MiB | 12.79 MiB
thanks already for your time!
[Update 23 march 2020]
so i've spent more time on this then i'm willing to say but i figured it out.
here is my solution if anyone ever wants to do the same.
i placed this script in my files
#!/bin/sh
set -x
export buildfolder="$(find . -regex '.\/temp[^\/]*\/WebGL\/Build' -print -quit)"
if [ -z "$buildfolder" ]; then
echo "Could not find build folder"
exit 1
fi
if [ ! -d ./tmp ]; then
git clone "https://${nickname}:${githubkey}#github.com/${nickname}/${repositorie}" ./tmp
fi
cp -r "$buildfolder" ./tmp
cd ./tmp
git add Build
git config --global user.email "$githubemail"
git config --global user.name "$nickname"
git commit -m "unity cloud build"
git push --force
and added these envirement varibles in my unity cloud build console
picture of envirement vars

Multiple git commands in single command executed in order they are encountered by compiler

I have following list of commands that I run in respective order so that a source project can be committed and pushed to the repository on Bitbucket:
git init
git remote add origin https://[BitBucket Username]#bitbucket.org/[BitBucket Username]/[BitBucket Repository Name].git
git config user.name "[BitBucket Username]"
git config user.email "[BitBucket Email ID]"
## if email doesn't work then use below ##
git config --global user.email \<\>
git add *
git commit -m "[Comment]"
git push -u origin master
Now instead of putting each and every line at their respective time and order, I want to know, if there is a possibility that I can chain all these into single git command and maintain the same order, something like below ?
git init remote add origin https://[BitBucket Username]#bitbucket.org/[BitBucket Username]/[BitBucket Repository Name].git config user.name "[Username]" ....
Or atleast combine multiple same category params like below ?
git config user.name "[BitBucket Username]" user.email "[BitBucket Email ID]"
I need to know possibility of both scenarios with examples.
We can use list off command in single command for example:
git add . && git commit -m "updated pom file" && git push
or:
git stash pop && git add . && git commit -m "updated pom file" && git push
&& -> if 1st command successfully executes then execute next command else it won't execute next command.
& - it executes all the command
|| - execute next command if 1st one failed
If you are in a Windows Powershell:
git add . ; git commit -m "Testing one line command" ; git push
I have gitbash on Windows system and I am not as good with Win batch as with Linux shell.
You still can write a bash script (interpreted by the msys2 bash embedded with Git for Windows).
As mentioned in the comments by Lasse V. Karlsen, and as I mentioned before in "Which shell used for git '!' aliases?", you can write a shell script in a file (in your %PATH%) named git-xxx, and call it with git xxx.
That script would begin with:
#!/bin/bash
I created a file called reset.txt and in that file I have the commands
git reset --hard
git clean -d -f
[this is a newline - very important to have it]
I just copy and paste this into my terminal and it executes the commands in order.

Resources