I work on a git repository where we build an php community but i need to show it somewhere so I am looking for a way to automatic upload my files to a remote http server when i push to the repository.
Thanks /Victor
Like Subversion Git offers a hook mechanism, too. Check out the githooks man page. Basically you just need to write a checkout and deploy script for your PHP application as a post-commit hook.
For github you should have a look at their webhooks mechanism.
If there is no separate git repo on the second server, I would export files from archive:
git checkout-index -a -f --prefix=/target/path/
And then used sftp to synchronize with remote server:
#!/bin/bash
HOST="ftp.example.com"
USER="user"
PASS="pass"
LCD="/var/www/yourdir"
RCD="/www/"
lftp -c "
#debug;
open ftp://$USER:$PASS#$HOST;
lcd $LCD;
cd $RCD;
mirror --only-newer \
--reverse \
--verbose \
--exclude-glob somepattern ";
You may automate this process as a build script (e.g. Phing),
our bind as a post commit git hook, like it has been mentioned before.
Related
I am new for using Github. I want upload my full site in new Repository in Github.com. But Github just can upload not more than 100file, and I'm download codeigniter-3 it's have 253file (original without my new file).
Can I upload my project in to Github?
This is likely a limitation of the drag and drop interface. I'd suggest to get familiar with the command line client instead. It's much more convenient than using the web interface and you'll need it anyway if you want to do serious work on that project from your dev machine.
First clone your project to a local directory:
$ git clone https://github.com/YOUR-USERNAME/YOUR-REPOSITORY
Then from your project directory, add all the files:
$ git add .
$ git commit -m "initial commit"
$ git push origin master
Alternatively, consider using https://desktop.github.com/
Reference:
https://help.github.com/articles/cloning-a-repository/
https://help.github.com/articles/adding-a-file-to-a-repository-from-the-command-line/
GitHub upload file step by step follow.
Now first create account on github.
https://github.com/login
create repository in your GitHub account.
setup git directory on your project directory by GIT Clone.
after git clone setup .GIT directory on your project with config.
Example: ##Config git repository##
git clone https://github.com/username/repository
##upload project on GIT##
git add /your-folder
git commit -m 'your first commit'
git push -u origin master
after push successfully upload your project.
I have create a webhook in my github repository which post on the hook url on my live server to run pull command for update my repo files on the server.
The problem is the hook file which i have created is in the /var/www/site/web/hookfile.php (the post request is going there. i am getting the body response also)
and my repo files are in /var/www/git-repo/
its not updating the git-repo when i push anything to my github repository.
I run this command using terminal and its working.
cd /var/www/git-repo && git pull
But through my php file its not working
shell_exec('cd /var/www/git-repo && git pull')
shell_exec() fail silently because only report STDOUT and not STDERR.
Try with:
echo shell_exec("cd /var/www/git-repo && /full/path/to/bin/git pull 2>&1");
Normally is a permission error, and could be fixed adding permission to the user that execute php (apache?)
chown -R www-agent:www-agent repository/
But could be also a connection error to the remote repository (authentication, ssh-keys, ...).
First of all in your php file run a test against your server instance to get any error messages output on screen because the exec() family of functions simply fail silently and only report STDOUT and not STDERR:
echo shell_exec("cd /website/root/htdocs && git checkout . && git status 2>&1");
In my case this threw an error that it could not find git command due to lack of binary path defined for apache user. Therefore, a full path needs to be provided to git's binary. It can be obtained by finding it manually or running in shell:
'which git'
It returned (further called YOU_FULL_GIT_BINARY_PATH_HERE):
/usr/local/git/bin/git
A full path with git command e.g. '/usr/local/git/bin/git status' now runs git commands nicely.
Another thing is to ensure your web server user has enough permissions to read/write to your repo folder/files. I have set mine to be owned by the apache user (Centos 6.8; other releases might be www:www or www-data:www-data etc.):
chown -R apache:apache YOUR_WEB_OR_REPO_FOLDER
In order to ensure any newly added files inherit correct permissions run:
chmod -R g+s YOUR_WEB_OR_REPO_FOLDER
The above should get your script to run commands now. Though it doesn't overcome git password prompt to use 'git pull' command for a git user set in YOUR_WEB_OR_REPO_FOLDER/.git/config file. Running below command inside repo:
git config credential.helper store
command will prompt for password and let you store it locally. Please note your stored password will be unencrypted and protected only by file system e.g. in /root/.git-credentials. This will allow to run 'git pull' without prompting for password.
It's not ideal for my fully automated continuous integration environment deploying test VPS on demand as it requires to manually enter git user (defined in repo's .git/config git) password at least once.
Since my environment should always run on code from remote's origin/master copy I am also running
/YOU_FULL_GIT_BINARY_PATH_HERE/git checkout .
before invoking 'git pull' to ensure any local changes are lost forever alternatively do a hard reset instead using:
/YOU_FULL_GIT_BINARY_PATH_HERE/git fetch origin
/YOU_FULL_GIT_BINARY_PATH_HERE/git reset --hard origin/master
I would like to clone a Laravel 4 from Git HERE and after using a Composer to install all dependencies, I wish to create a new Git on my HDD where I have my Dropbox synchronized.
Is it even possible ?
After getting Laravel 4 being cloned into C:/www/laravel-project/, I would like to commit the whole project into my localhost REPO seating in D:/Dropbox/REPOS/Git/ so it will become D:/Dropbox/REPOS/Git/laravel-project/
Thanks
Two possible fixes:
Edit .git/config so that your origin is your repo (replace https://github.com/laravel/laravel.git with file:///D:/Dropbox/REPOS/Git/laravel-project/).
Delete the .git directory and then run git init and git remote add file:///D:/Dropbox/REPOS/Git/laravel-project/ origin
The problem I've seen with local directory repos is that the D:/Dropbox/REPOS/Git/laravel-project/ repo will need to be on a branch that will not be used (i.e. A DoNotUse branch).
There are two paths you can take to do this:
Change the origin to your "remote" and remove or rename the Laravel origin.
Preserve the Laravel origin and use a second "remote" for your files.
Option 1 is probably the easiest.
Go to D:/Dropbox/REPOS/Git/laravel-project/ and do git init to make an empty git repo.
Go to your C:/www/laraval-project/ folder and do git remote -v and save the URL for the current origin if you want to keep it.
Run the command git remote origin set-url file:///D:/Dropbox/REPOS/Git/laravel-project
Optional: run git remote add github url-you-saved-from-step-2 so you can do git pull github if you want to update from the Laravel github
Run git push -u origin master and it should push into your Dropbox's git repo.
For Option 2 you just skip steps 2 through 4 and - run git remote add dropbox file:///whatever and change step 5 to git push -u dropbox master or whatever branch you want. Once you use -u once you can just do git push and it should push to whatever you set as your upstream with -u.
I'm trying to use PHPseclib to SSH and run commands on the remote server. I want to change directory and commands like git pull or clone. Is there a way to do this? I know that "cd" doesn't work well with exec. So any alternatives to this?
Thanks
You don't need to change folder, only to specify it to your git command.
git --git-dir=/path/to/repo/.git --work-tree=/path/to/repo remote add xxx
git --git-dir=/path/to/repo/.git --work-tree=/path/to/repo pull
Since git 1;8.5 (if your server hasd a recent enough git version installed), you even can use the short version (detailed here)
git -C /path/to/repo remote add xxx
git -C /path/to/repo pull
I'm in the process of trying to set up automatic deployments when I make a git push to my bitbucket repository. I have a php deploy script that I leveraged from this blog but when the script runs it is logging that it's only updating from a previous commit.
Here is an example. Let say I log into my server and type git pull. The server will update with the latest changes and lets say the hash for that commit was 001. However if I make several commits lets call them 002, 003, and 004 my script should run every time assuming I pushed those changes to bitbucket after every commit. The script runs but every time it will keep the changes from 001. Only when I log into my server and type git pull, will the server update to 004. Do you know what would cause this?
// Make sure we're in the right directory
exec('cd '.$this->_directory, $output);
$this->log('Changing working directory... '.implode(' ', $output));
// Discard any changes to tracked files since our last deploy
exec('git reset --hard HEAD', $output);
$this->log('Reseting repository... '.implode(' ', $output));
// Update the local repository
exec('git pull '.$this->_remote.' '.$this->_branch, $output);
$this->log('Pulling in changes... '.implode(' ', $output));
// Secure the .git directory
exec('chmod -R og-rx .git');
$this->log('Securing .git directory... ');
if (is_callable($this->post_deploy))
{
call_user_func($this->post_deploy, $this->_data);
}
$this->log('Deployment successful.');
What I would recommend is to release not based on latest version in your master, but a latest tag.
/home/my-user/my-application/1.0.12/www
/home/my-user/my-application/1.0.13/www
etc. This provides rollback functionality. You could make a PHP script that connects to your server over SSH and makes a new clone based on that tag. If you use Composer, you can use this to execute commands. If not, you can do it with a makefile.
Edit: I have forgot to mention how you actually link it.
You have a symlink
/home/my-user/my-application/www -> /home/my-user/my-application/1.0.12/www
When your entire deployment script is finished without errors, you switch the symlink to:
/home/my-user/my-application/www -> /home/my-user/my-application/1.0.13/www
Your application is now live without downtime.
A very simple and efficient approach is to use bitbucket pipelines, you can create a YAML script to compile your dependences and push the code to a server on each commit, automatimagicaly or manually.
Here is an example of a Bitbucket Pipelines YAML script:
image: php:7.1.1
pipelines:
default:
- step:
caches:
- composer
script:
- apt-get update && apt-get install -y unzip
- curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
- composer install
- step:
deployment: staging
name: Deploy to Staging Server
script:
- apt-get update
- apt-get -qq install rsync
- echo "Upload Project files..."
- rsync -avH * -e "ssh" root#$IP_SERVER:/var/www/html/
This script installs the Composer dependencies inside a Docker instance and pushes the projects files to server.
The problem is file permissions.
I was following the same link to that blogpost. I found that the 'www-data' user that is used by php and nginx processes, does not have write permissions to your repository code. It cannot even use git.
For verifying this try doing a 'git pull' on the server as 'www-user'. You can switch to it by 'sudo su www-data'. You will find that it does not even recognize that as a valid git repo and is unable to run your 'deploy.php' script without errors.
You need to set proper permissions to your repository so that www-data can access it.
Or you change the whole approach. Follow this post http://toroid.org/ams/git-website-howto which I feel is a much better method than the above. I ended up using this method.
You want to set a post-update hook in the remote you push to.
In the default case git will not checkout any data you push to a remote. That's why the default git configuration refuses to push to the checked out branch as the checked out files aren't up-to-date with HEAD anymore, when you push to the checkout-out branch.
When the remote receives a push to a branch you can react on that in the post-update hook, though. To see what happens you should first start with some logging:
echo "post update `date`: $*" >> /home/ingo/test/githooks.out
When I push to the branch new, for example I get the line
post update Mi 24. Jun 13:01:14 CEST 2015: refs/heads/new
, where $* contains the branches I pushed to.
With this you can write simply write a script to checkout that branch. I prefer checking out to detached heads, as its much simpler to combine work from several module repositories (no submodules) into a deployed version. See my answer to the source code deployment on how to checkout code outside of the git repository and work with that.
For example you could do
for b in $*
do B=`basename $b`
if [ "$B" = "publish" ] # react on changes to the publish branch
then git --work-tree "PATH TO THE PUBLISHED WORK TREE" checkout publish
do_some_extra_work # to change permissions or inform your team leader
fi done
Of course you can do the checkout step too, that is done on a pull
if [ "`cat HEAD`" = "ref: refs/heads/master" ]
# only do that checkout if the repository is in a sane state
then git merge new # I assume you push to new
fi
The cool thing is that you will see the output of your hook on the remote in the terminal where you executed the push:
# git push remote master:new
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 296 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: Updating f81ba5b..a99a710
remote: Fast-forward
remote: a.txt | 2 ++
remote: 1 file changed, 2 insertions(+)
To ../remote
db48da1..a99a710 master -> new