Generate a version number of my site with git? - php

I am building a PHP/Yii application. While developing and testing on different machines, I'd like to be able to quickly see in the footers if the version I am seeing is the latest. So basically I want to generate some kind of version number every time I modify something.
I was thinking since I use Git (and GitHub) I could use some of the meta data generated at every commit?
How could I achieve this?
Note: I'd like to avoid using command-line stuff as my current hosting won't allow me to do this.

Add a git hook to create a txt file in the root folder of your app to keep track of the version (or tag, or whatever) of the current deployed code.
Just a 30 sec. example (search google for more details and how to use hooks in git) put in the .git/hooks folder a file named pre-commit and add this shell code:
#!/bin/sh
rm version.txt -i
git describe --tags >> version.txt
git add version.txt

If you deploy with Capistrano, one very serious possibility is to read the 'REVISION' file from the base of the site. If you deploy from a git repo, it will be the commit SHA.

Related

GITHUB : I want to add project from local to live server through github [duplicate]

Is there any way to set up git such that it listens for updates from a remote repo and will pull whenever something changes? The use case is I want to deploy a web app using git (so I get version control of the deployed application) but want to put the "central" git repo on Github rather than on the web server (Github's interface is just soooo nice).
Has anyone gotten this working? How does Heroku do it? My Google-fu is failing to give me any relevant results.
Git has "hooks", actions that can be executed after other actions. What you seem to be looking for is "post-receive hook". In the github admin, you can set up a post-receive url that will be hit (with a payload containing data about what was just pushed) everytime somebody pushes to your repo.
For what it's worth, I don't think auto-pull is a good idea -- what if something wrong was pushed to your branch ? I'd use a tool like capistrano (or an equivalent) for such things.
On unix-likes you can create cron job that calls "git pull" (every day or every week or whatever) on your machine. On windows you could use task scheduler or "AT" command to do the same thing.
There are continuous integrations programs like Jenkins or Bamboo, which can detect commits and trigger operations like build, test, package and deploy. They do what you want, but they are heavy with dependencies, hard to configure and in the end they may use periodical check against git repository, which would have same effect like calling git pull by cron every minute.
I know this question is a bit old, but you can use the windows log and git to autopull your project using a webhook and php (assuming your project involves a webserver.
See my gist here :
https://gist.github.com/upggr/a6d92e2808e9628ebe0d01fd93569f4a
As some have noticed after trying this, if you use php exec(), it turns out that solving for permissions is not that simple.
The user that will execute the command might not be your own, but www-data or apache.
If you have root/sudo access, I recommend you read this Jonathan's blog post
When you aren't allowed/can't solve permissions
My solution was a bit creative. I noticed I could create a script under my username with a loop and git pull would work fine. But that, as pointed out by others, bring the question of running a lot of useless git pull every, say, 60 seconds.
So here the steps to a more delicate solution using webhooks:
deploy key: Go to your server and type:
ssh-keygen -t rsa -b 4096 -C "deploy" to generate a new deploy key, no need write-permissions (read-only is safer). Copy the public key to your github repository settings, under "deploy key".
Webhook: Go to your repository settings and create a webhook. Lets assume the payload address is http://example.com/gitpull.php
Payload: create a php file with this code example bellow in it. The purpose of the payload is not to git pull but to warn the following script that a pull is necessary. Here the simple code:
gitpull.php:
<?php
/* Deploy (C) by DrBeco 2021-06-08 */
echo("<br />\n");
chdir('/home/user/www/example.com/repository');
touch(GITPULLMASTER);
?>
Script: create a script in your preferred folder, say, /home/user/gitpull.sh with the following code:
gitpull.sh
#!/bin/bash
cd /home/user/www/example.com/repository
while true ; do
if [[ -f GITPULLMASTER ]] ; then
git pull > gitpull.log 2>&1
mv GITPULLMASTER GITPULLMASTER.`date +"%Y%m%d%H%M%S"`
fi
sleep 10
done
Detach: the last step is to run the script in detached mode, so you can log out and keep the script running in background.
There are 2 ways of doing that, the first is simpler and don't need screen software installed:
disown:
run ./gitpull.sh & to put it in background
then type disown -h %1 to detach and you can log out
screen:
run screen
run ./gitpull.sh
type control+a d to detach and you can log out
Conclusion
This solution is simple and you avoid messing with keys, passwords, permissions, sudo, root, etc., and also you prevent the script to flood your server with useless git pulls.
The way it works is that it checks if the file GITPULLMASTER exists; if not, back to sleep. Only if it exists, then do a git pull.
You can change the line:
mv GITPULLMASTER GITPULLMASTER.date +"%Y%m%d%H%M%S"`
to
rm GITPULLMASTER
if you prefer a cleaner directory. But I find it useful for debug to let the pull date registered (and untracked).
For our on-premises Windows test servers, we use Windows Task Scheduler tasks, set to run every 3 minutes, pulling from Bitbucket Cloud to repositories on those servers. While not instantaneous, it meets our needs, and has proven to be reliable.

How to set up a development environment using git for my wordpress website

I am developing a wordpress website and it is live. I am currently using file-zilla to update my code. Until now I wasn't in need of any version control, but now it seems that I need to manage a repository as well. We have also added two freelancers in a development team. So, it becomes difficult for us to manage development of this website.
I have read several blogs and I came to a decision that I will use Git for version control. However, I am struggling to find proper steps which explain to me about setting up a development environment for a team of freelancers. Also, I got a little information about bitbucket, but I am not sure if that is necessary for me. I am looking for the best answer to get started.
(Assuming you are leaving core Wordpress files alone and working only with themes and / or plugins as is best practice:)
Create a git repository every theme and plugin that you are working on using a server of your choice: I would go with Bitbucket for free private repos that you can invite the devs to.
Since you already have the files pushed by FileZilla, you would go into each of the folders for the themes and plugins you are using and push them as existing repos using SSH like so:
First initilize the repos and commit:
$git init
$git add .
$git commit -a -m "initial commit"
Then push
$git remote add origin git#bitbucket.org:my_org/my_repo.git
$git push -u origin --all # pushes up the repo and its refs for the first time
$git push -u origin --tags # pushes up any tags
If you are editing core files as well which would be highly discouraged, then you would create just one repo for the entire Wordpress folder, and then initialize, add, commit and push as above.
Development environments would be set up by the devs themselves using the platforms of their choice. Or ask them to use Vagrant if you want them to have similar environments.

How to merge a none git directory with 90% same files into a repository

I started a web project without git.
Now I created a project on bitbucket and commited the a backup from 10 days ago. That means a local copy created with git init add and so one but the latest changes were 10 days in the past.
On my webserver is the current version in about 10% of all files have changed but there is no git initialised at the moment.
So now I want to push all files from my server to the master branch so that I can see the differences.
But how do I accomplish this if the new version is neither a clone nor a seperate branch but just the same folder and file structure as my git repos master with a few changes.
Well I like to switch completely to git but I need to visualize the diffrences between the old and the current version for other developers :(
Any idea?
Thank you
Given a working directory with master checked out, you simply copy the files from the webserver onto it. git diff will show you the differences. If you do git add . && git commit "as per webserver", you'll sync your master with the webserver.

Do a "git diff" between two directories due to website owner making live changes

I have a project I'm working on which I've set up in a git repo.
Since I put my latest version live, the website owner has made some changes to the working/content by overwriting it directly.
Obviously these changes were made outside of version control. I suppose I could overwrite the entire contents of my repo, then commit. That should work... but I don't really like the idea of doing that, especially if there's been any replacement of correct code/html-structure with stuff that's incorrect or bad practice.
What I'd like to do is dump the website from live into another directory and do a recursive diff so I can only overwrite those files which have changed (any correct any issues if there are any)
As in if I just overwrite what's in my git repo, then run git status?
No, you don't have to overwrite anything.
You can do a diff between:
the index representing your site in its latest versioned state (in the git repo)
a dump of the current live site (a copy done in a different folder, not under version control)
You can then do (using git options) a:
git diff --git-dir=/path/to/repo/.git --work-tree=/path/to/dump .
You actually can execute that command from any folder: it will look for the git index and for the right working tree.
The OP Tom Busby adds in the comments:
In the end I solved it by simply overwriting the .git folder in my dump directory with the one from my git repo and running git diff.
VonC's answer is excellent, but I believe you could also just use the diff command (a built-in command on most operating systems). This would also ensure that "gitignored" files were diffed (though VonC's might already do that).
Assuming the website in /www/mysite and a fresh clone in /git/mysite you could just do:
diff /www/mysite /git/mysite
Yes We can compare two directories very easily
simply using this command on Shell/Terminal
git diffdir any-branch-name any-other-branch-name

NetBeans + FTP + BitBucket

I know this has been asked before, but I couldn't get the answer I needed.
Currently I'm developing an website using PHP and was using Notepad++ before, and it all worked well because I'm developing with a co-worker so we both keep on changing different files on the FTP.
Switched to NetBeans. All went ok, pulled the entire website via FTP to my local computer and everytime I edited a file and saved it uploaded to the FTP. But, there is a problem. If my colleague updates a file, it doesn't update on my local folder. So, I thought: "Let's try versioning".
Created a team on bitbucket, created a repository. All went ok.
But now, I'm in a struggle to get everything up and running on both NetBeans (mine and colleague's) so that my colleague is editing a file on his NetBeans and constantly saving so that it gets saved on FTP and only when he stops working on that file push it to BitBucket so that I can pull after.
Suggestions?
About setting up your work environment :
In order to set up your bitbucket repository and local clone, go read this link (official doc).
You will need to repeat the cloning part once for each PC (e.g : once on yours, once on your colleague's).
Read the account management part to see how you can tag your actions with your account, and your colleague's action with his own account.
Start using your git workflow ; when you are tired of always typing your password to upload modifications to your bitbucket account, take the time to read the ssh keys setup part - read carefully, you will need to execute the procedure once for you and once for your colleague.
Using your local git repository with Netbeans is pretty straightforward :
From netbeans, run the File > New Project ... command (default: Ctrl+Shift+N),
Select PHP application with Existing Sources and click Next >,
For the Sources Folder: line, select your local git directory,
Fill the remaining fields, and if you want the last Run configuration screen, then click Finish.
After the project is created in netbeans, you can modify the Run configuration part by right clicking on the project's icon, selecting the Properties menu entry, and going to the Run configuration item.
About solving your workflow "problem" :
Your current FTP workflow can lead you to blindly squash your colleague's modifications (when uploading), or have your colleague's modification blindly squash your own local modifications (when downloading). This is bad, and you will generally notice it only after the bad stuff happened - too late.
Correctly using version control allows you to be warned when this could potentially happen, and to keep an almost infinite undo stack on the modifications of the project's files. The cost, however, is that both of you will have to add several actions in your day to day workflow - some choices can not be made automatically.
You may find it cumbersome in the beginning, but it really pays off, and quite quickly - we're talking big bucks here. So use it and learn.
On top of using Ctrl+S to save your modifications on disk, you and your colleague will need to integrate 3 extra commands in your daily work :
Save your work to your local repository (git add / git commit)
Download the latest modifications shared by your colleague (git pull)
Upload your work to the central repository (git push)
You can access these commands :
from a terminal,
from a GUI frontend : you can try TortoiseGit for windows, or gitk for linux,
from Netbeans :
in the contextual menu of the files/folders in the project tree (right click on the item, there is a "Git" entry),
using the Team > Git > ... menu
Since you provided a git tag, I'll describe what's to do for Git.
set up a remote bare repo on a server that you both could access (BitBucket in your case):
http://git-scm.com/book/en/Git-on-the-Server-Getting-Git-on-a-Server
you both clone that remote repo to your local machines:
http://git-scm.com/book/en/Git-Basics-Getting-a-Git-Repository#Cloning-an-Existing-Repository
each of you works in her part of the application. When one is done, publish the work to the server:
http://git-scm.com/book/en/Git-Basics-Working-with-Remotes#Pushing-to-Your-Remotes
By now, the remote server holds the version that was just pushed. What's missing is the deployment of the website. This has been discussed here:
Using GIT to deploy website
Doing so, you will decouple your work from that of your colleague since you're not changing files over FTP all the time. You work in your part, your partner works on her part. The work is getting merged and then a new version of the website gets published.
You can create git or Mercurial repositories in Atlassian Bitbucket (http://bitbucket.org). If your team is new to version control, i advise you no forks in your first project.
The easy solution ins to use Atlassian SourceTree (http://www.sourcetreeapp.com/) to control your code since there is a bug in netbeans. See NetBeans + Git on BitBucket
You need to create a new repository in bitbucket. I assume you already configure the ssh2 keys. Using Git you need:
git clone --bare --shared php_project php_project.git
git commit
Using Mercurial you need:
hg init
hg commit
Good luck / boa sorte
Pedro

Categories