Conditional path to deploy in cpanel.yml file configuration - linux

I need to deploy the same repository in different paths based on the current path of this file
Example:
If this .Cpanel.yml is in the repository located at "/ home / foo / repository / foo_prod /", implement it at / home / foo / public_html / production_folder / but if the file location binder . cpanel.yml is in "/ home / foo / repository / foo_pre_prod /" so implement it in / home / foo / public_html / pre_prod_folder /
Current file .cpanel.yml
---
deployment:
tasks:
- export DEPLOYPATH=/home/foo/public_html/xyz/
- /bin/cp -R * $DEPLOYPATH

Maybe try something like this:
deployment:
tasks:
- current_branch=$(git branch --show-current) # set current branch to variable
- if [ $current_branch == "master" ]; then export DEPLOYPATH=/home/foo/public_html/xyz/; fi;
- if [ $current_branch == "develop" ]; then export DEPLOYPATH=/home/foo/public_html/xyz_dev/; fi;
- /bin/cp -R * $DEPLOYPATH

Solved my previous question! I share what I did. Thanks #Salines your
anwer help me!
---
deployment:
tasks:
- current_path=`pwd` # set current path to variable
- if [ $current_path == "/home/user/repositories/p" ]; then export DEPLOYPATH=/home/user/public_html/p/; fi;
- if [ $current_path == "/home/user/repositories/p_pre" ]; then export DEPLOYPATH=/home/user/public_html/p_pre/; fi;
- /bin/cp -R * $DEPLOYPATH

Related

Sanitizing the source branch name to use as azure pipeline hostname

I am trying to dynamically create kubernetes pods when deploying branches for testing. I would like to use the branchname as part of the host name, for example my ingress controller would look like this.
spec: rules: - host: builder-${Build.SourceBranchName}.somedomian.com http:
But I am not sure how to sanitze the branch name to exclude these characters, ! * ' ( ) ; : # & = + $ , / ? % # [ ]
I saw something like this but have not been able to get it to work.
a="$(echo $(Build.SourceBranchName) | tr -d .)"
echo "##vso[task.setvariable variable=BranchName]$a"

Error when trying to pass variables between jobs using artifacts

I have the fallowing CI parent pipeline and I used artifacts in order to pass variables to a child pipeline:
before-script:
script:
- terraform --version
- export AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID}
- export AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY}
- export TF_VAR_API_KEY=${TF_VAR_API_KEY}
- export GIT_TOKEN=${GIT_TOKEN}
prepare-infra-code:
stage: build
script:
- cd iac/
- terraform init -force-copy -backend-config="address=https://gitlab.com/api/v4/projects/XXX/terraform/state/main" -backend-config="lock_address=https://gitlab.com/api/v4/projects/XX/terraform/state/main/lock" -backend-config="unlock_address=https://gitlab.com/api/v4/projects/XXX/terraform/state/main/lock" -backend-config="username=XXX" -backend-config="password=$GIT_TOKEN" -backend-config="lock_method=POST" -backend-config="unlock_method=DELETE" -backend-config="retry_wait_min=5"
- terraform plan
...
deploy-prod:
stage: deploy
script:
- cd iac/
- terraform init -force-copy -backend-config="address=https://gitlab.com/api/v4/projects/XXX/terraform/state/main" -backend-config="lock_address=https://gitlab.com/api/v4/projects/XXX/terraform/state/main/lock" -backend-config="unlock_address=https://gitlab.com/api/v4/projects/XXX/terraform/state/main/lock" -backend-config="username=XXX" -backend-config="password=$GIT_TOKEN" -backend-config="lock_method=POST" -backend-config="unlock_method=DELETE" -backend-config="retry_wait_min=5"
- terraform apply --auto-approve
- URL=$(terraform output api_url | cut -c2- | sed 's/.$//')
- S3=$(terraform output bucket_name | cut -c2- | sed 's/.$//')
- echo "REACT_APP_PORTFOLIO_API=$URL/portfolio" >> deploy-prod.env
- echo "REACT_APP_TRANSACTION_API=$URL/transaction" >> deploy-prod.env
- echo "FRONTEND_BUCKET=$S3" >> deploy-prod.env
artifacts:
reports:
- dotenv: infra.env
However, I keep getting this error:
jobs:deploy-prod:artifacts reports should be a hash
Can someone please help me identify the issue ?

How change symlink path for many files?

I was changed directory name.
In this directory thousands of files.
Some projects use this files, projects have got symlinks on it.
How to find all symlinks, which have got folder name in their address?
how to change all this symlinks to another path in automatic mode?
if 2 only bash scripting with deleting and creating new - i will do it, but may be you know more easy way?
It's a bit complicated, but it can be done with find, readlink, a check to test whether the symlink is relative or not, and sed to get rid of .. in path names (copied 1:1 from this answer).
(Note that most convenient methods (such as readlink -f) are not available due to the symlinks targets not existing anymore.)
Assuming your old path is /var/lib/old/path:
oldpath='/var/lib/old/path';
find / -type l -execdir bash -c 'p="$(readlink "{}")"; if [ "${p:0:1}" != "/" ]; then p="$(echo "$(pwd)/$p" | sed -e "s|/\./|/|g" -e ":a" -e "s|/[^/]*/\.\./|/|" -e "t a")"; fi; if [ "${p:0:'${#oldpath}'}" == "'"$oldpath"'" ]; then ...; fi;' \;
Now replace the ... from above with ln -sf (-f to override the existing link).
Assuming your new path is /usr/local/my/awesome/new/path:
oldpath='/var/lib/old/path';
newpath='/usr/local/my/awesome/new/path';
find / -type l -execdir bash -c 'p="$(readlink "{}")"; if [ "${p:0:1}" != "/" ]; then p="$(echo "$(pwd)/$p" | sed -e "s|/\./|/|g" -e ":a" -e "s|/[^/]*/\.\./|/|" -e "t a")"; fi; if [ "${p:0:'${#oldpath}'}" == "'"$oldpath"'" ]; then ln -sf "'"$newpath"'${p:'${#oldpath}'}" "{}"; fi;' \;
Note that oldpath and newpath have to be absolute paths.
Also note that this will convert all relative symlinks to absolute ones.
It would be possible to keep them relative, but only with a lot of effort.
Breaking it down
For those of you who care what that one-line-inferno actually means:
find - a cool executable
/ - where to search, in this case the system root
-type l - match symbolic links
-execdir - for every match run the following command in the directory of the matched file:
bash - well, bash
-c - execute the following string (leading and trailing ' removed):
p="$(readlink "{}")"; - starting with the most inner:
" - start a string to make sure no expansion happens
{} - placeholder for the matched file's name (feature of -execdir)
" - end the string
readlink ... - find out where the symlink points to
p="$(...)" - and store the result in $p
if [ "${p:0:1}" != "/" ]; then - if the first character of $p is / (i.e. the symlink is absolute), then...
p="$(echo "$(pwd)/$p" | sed -e "s|/\./|/|g" -e ":a" -e "s|/[^/]*/\.\./|/|" -e "t a")"; - convert the path to an absolute one:
$(pwd) - the current directory (where the matched file lies, because we're using -execdir)
/$p - append a slash and the target of the symlink to the path of the working directory
echo "$(pwd)/$p" | - pipe the above to the next command
sed ... - resolve all ..'s, see here
p="$(...)" and store the result back into $p.
fi; - end if
if [ "${p:0:'${#oldpath}'}" == "'"$oldpath"'" ]; - if $p starts with $oldpath
${p:0:'${#oldpath}'} - substring of $p, starting at position 0, with length of $oldpath:
${#oldpath} - length of variable $oldpath
'...' - required because we're inside a '-quoted string
then - then...
ln -sf - link symbolically and override existing file, with arguments:
"'"$newpath"'${p:'${#oldpath}'}" - replace the $oldpath part of $p with $newpath (actually remove as many characters from $p as $oldpath long is, and prepend $newpath to it):
" - start a string
' - end the '-string argument to bash -c
" - append a "-string to it (in which variable expansion happens), containing:
$newpath - the value of $newpath
" - end the "-string argument to bash -c
' - append a '-string to it, containing:
${p: - a substring of p, starting at:
' - end the argument to bash -c
${#oldpath} - append the length of $oldpath to it
' - append another '-string to it
} - end substring
" - end string
"{}" - the link file, whose path stays the same
fi; - end if
\; - delimiter for -execdir

Rsync - split source as multiple sets

I had the source directory :
root/sourceDir1/sourceDir11
root/sourceDir1/sourceDir12
root/sourceDir1/sourceDir13
root/sourceDir1/sourceDir14
root/sourceDir2/SourceDir21
root/sourceDir2/SourceDir22
root/sourceDir2/SourceDir23
root/sourceDir2/SourceDir24
root/sourceDir2/SourceDir25
root/sourceDir2/SourceDir26
How can I use rsync command to sync these directories as specified in below:
rootTarget1/sourceDir11
rootTarget1/sourceDir12
rootTarget1/sourceDir13
rootTarget1/sourceDir14
rootTarget2/sourceDir21
rootTarget2/sourceDir22
rootTarget2/sourceDir23
rootTarget2/sourceDir24
rootTarget2/sourceDir25
rootTarget2/sourceDir26
Basically I would like to split the source directories into 2 sets, one set of dirs should sync to rootTarget1 and another set should sync to rootTarget2
try this script:
#!/bin/bash
for ((x=1;x<=2;x++));
do if [ x = 1 ] ;
then for ((y=1;y<=4;y++));
do rsync root/sourceDir${x}/SourceDir${x}${y} rootTarget${x}/Sourcedir${x}${y}" ;
done ;
else for ((y=1;y<=6;y++)) ;
do rsync root/sourceDir${x}/SourceDir${x}${y} rootTarget${x}/SourceDir${x}${y}" ;
done ;
fi;
done

Creating a cronjob from a makefile?

I am trying to let my makefile setup a cronjob for my application. Unfortunately it appears to not be working as the $CRONENTRY variable seems to be empty. What am I doing wrong here?
addcron:
CRONENTRY="*/2 * * * * /usr/bin/node cronapp.js >> logfile.log"
crontab -l | { cat; echo ${CRONENTRY}; } | crontab -
Each command in a rule executes in its own subshell; variables do not survive from one command to the next. So if you want to use a variable this way, you have to string the commands together.
addcron:
CRONENTRY="whatever" ; \
do_something_with $(CRONENTRY)
What about
addcron:
CRONENTRY=
{ crontab -l; echo "*/2 * * * * /usr/bin/node cronapp.js >> logfile.log" } | crontab -
there you have one less pipe element...

Resources