Below is my custom gitp command which works great (with psuedo-code). I would like to add to the script by having it automatically auto-index and checkout to a new branch.
Hoping there is a command-line bash whiz who can figure it out! :)
previous_branch_num = 0;
gitp() {
git add -A &&
git commit -m "${1?'Missing commit message'}" &&
git push
git checkout -b "v{++previous_branch_num}" //<--psuedo code
}
Simply:
#!/bin/bash
previous_branch_num=0
gitp() {
git add -A &&
git commit -m "${1?'Missing commit message'}" &&
git push
git checkout -b "v$((++previous_branch_num))" # <-- real code
}
Related
I have a website running on cloud server. Can I link the related files to my github repository. So whenever I make any changes to my website, it get auto updated in my github repository?
Assuming you have your cloud server running an OS that support bash script, add this file to your repository.
Let's say your files are located in /home/username/server and we name the file below /home/username/server/AUTOUPDATE.
#!/usr/bin/env bash
cd $(dirname ${BASH_SOURCE[0]})
if [[ -n $(git status -s) ]]; then
echo "Changes found. Pushing changes..."
git add -A && git commit -m 'update' && git push
else
echo "No changes found. Skip pushing."
fi
Then, add a scheduled task like crontab to run this script as frequent as you want your github to be updated. It will check if there is any changes first and only commit and push all changes if there is any changes.
This will run every the script every second.
*/60 * * * * /home/username/server/AUTOUPDATE
Don't forget to give this file execute permission with chmod +x /home/username/server/AUTOUPDATE
This will always push the changes with the commit message of "update".
We want to know if it's technically possible like in GitHub, to do a git push using https protocol and not ssh and without using directly an username and password in the curl request.
I have seen people that seem to think it is possible, we weren't able to prove it.
Is there any proof or witness out there than can confirm such a feature that allow you to push using a user access token or the gitlab-ci-token within the CI?
I am giving my before_script.sh that can be used within any .gitlab-ci.yml
before_script:
- ./before_script.sh
All you need is to set a protected environment variable called GL_TOKEN or GITLAB_TOKEN within your project.
if [[ -v "GL_TOKEN" || -v "GITLAB_TOKEN" ]]; then
if [[ "${CI_PROJECT_URL}" =~ (([^/]*/){3}) ]]; then
mkdir -p $HOME/.config/git
echo "${BASH_REMATCH[1]/:\/\//://gitlab-ci-token:${GL_TOKEN:-$GITLAB_TOKEN}#}" > $HOME/.config/git/credentials
git config --global credential.helper store
fi
fi
It doesn't require to change the default git strategy and it will work fine with non protected branch using the default gitlab-ci-token.
On a protected branch, you can use the git push command as usual.
We stopped using SSH keys, Vít Kotačka answers helped us understand why it was failing before.
I was not able to push back via https from a Docker executor when I did changes in the repository which was cloned by gitlab-runner. Therefore, I use following workaround:
Clone a repository to some temporary location via https with a user access token.
Do some Git work (like merging, or tagging).
Push changes back.
I have a job in the .gitlab-ci.yml:
tagMaster:
stage: finalize
script: ./tag_master.sh
only:
- master
except:
- tags
and then I have a shell script tag_master.sh with Git commands:
#!/usr/bin/env bash
OPC_VERSION=`gradle -q opcVersion`
CI_PIPELINE_ID=${CI_PIPELINE_ID:-00000}
mkdir /tmp/git-tag
cd /tmp/git-tag
git clone https://deployer-token:$DEPLOYER_TOKEN#my.company.com/my-user/my-repo.git
cd my-repo
git config user.email deployer#my.company.com
git config user.name 'Deployer'
git checkout master
git pull
git tag -a -m "[GitLab Runner] Tag ${OPC_VERSION}-${CI_PIPELINE_ID}" ${OPC_VERSION}-${CI_PIPELINE_ID}
git push --tags
This works well.
Just FYI, personal access tokens have account level access, which is generally too broad. The Deploy Key is better as it only has project-level access and can be given write permissions at the time of creation. You can provide the public SSH key as the deploy key and the private key can come from a CI/CD variable.
Here's basically the job I use for tagging:
release_tagging:
stage: release
image: ubuntu
before_script:
- mkdir -p ~/.ssh
# Settings > Repository > Deploy Keys > "DEPLOY_KEY_PUBLIC" is the public key of the utitlized SSH pair
# Settings > CI/CD > Variables > "DEPLOY_KEY_PRIVATE" is the private key of the utitlized SSH pair, type is 'File' and ends with empty line
- mv "$DEPLOY_KEY_PRIVATE" ~/.ssh/id_rsa
- chmod 600 ~/.ssh/id_rsa
- 'which ssh-agent || (apt-get update -y && apt-get install openssh-client git -y) > /dev/null 2>&1'
- eval "$(ssh-agent -s)"
- ssh-add ~/.ssh/id_rsa > /dev/null 2>&1
- (ssh-keyscan -H $CI_SERVER_HOST >> ~/.ssh/known_hosts) > /dev/null 2>&1
script:
# .gitconfig
- touch ~/.gitconfig
- git config --global user.name $GITLAB_USER_NAME
- git config --global user.email $GITLAB_USER_EMAIL
# fresh clone
- mkdir ~/source && cd $_
- git clone git#$CI_SERVER_HOST:$CI_PROJECT_PATH.git
- cd $CI_PROJECT_NAME
# Version tag
- git tag -a "v$(cat version)" -m "version $(cat version)"
- git push --tags
I'm playing around with my custom commands, and I'm currently trying to change a remote Git branch programatically using bash.
issue() {
if [ `git branch --list issue_$1` ]
then
git checkout issue_$1
else
git checkout -b issue_$1
git branch -u origin issue_${1}
fi
}
The idea is this function will try to find the branch issue_X, if it does it switches, otherwise it creates and sets the remote origin.
The problem is git branch -u origin issue_${1} I don't know how to do this, and I'm having trouble googling for it because I don't know what this process is called.
Thanks a lot for the help!
I don't know how to do git branch -u origin issue_${1}
If a remote-tracking branch origin/issue_${1} exists you can do git branch -u origin/issue_${1}.
The problem is that in your situation the remote-tracking branch doesn't exit and you have to create it:
git push -u origin issue_${1}
I've been trying to create a new alias that send files to staging area, and at the same time it commit with a message.
I've tried this:
git config --global alias.stagecomm '!git add -A && git commit -m $1'
When I try to run:
git stagecomm "Commit"
It says that it didn't match any files known to git.
Try with a sh: edit your git config --global --edit and type:
!sh -c 'git add -A && git commit -m $1'
Another option: define a script (even on Windows) without extension, called git-stagecomm
In it, put your commands:
git add -A
git commit -m $1
If that script is in your path, you will be able to call it with git stagecomm "mymessage"
Any script called git-xxx will be executed by the git bash, as git xxx.
No alias needed there.
That being said, you could also type (without alias)
git commit -am "My message"
See git commit man page.
-a
--all
Tell the command to automatically stage files that have been modified and deleted, but new files you have not told Git about are not affected.
That would not take new files though.
Now I do this:
git commit -a -m "comment"
then (to bitbucket.org)
git push
then (to hosting via ftp)
git ftp push
I want to run these commands automatically:
git fix "comment"
or so:
gitfix "comment"
Create a bash function:
gitfix() {
git commit -a -m "$1" && git push && git ftp push
}
and put it in your ~/.bashrc file so you can just execute it from the terminal as gitfix "some commit comment"
Update: Concatenated the commands with &&, so, in case of failure, the remaining commands will not be executed. Thanks to burper for this update.