Installation of SVN server and SVN client under Linux tutorial and automatic deployment of code to the project (full)

Posted by pokemon on Thu, 11 Nov 2021 03:14:37 +0100

Copyright notice: This article is the original article of the blogger and follows CC 4.0 BY-SA Copyright agreement, please attach the original source link and this statement.
Link to this article: https://blog.csdn.net/qq_37655695/article/details/80579072

Installing SVN server under Linux


1. Introduction
Subversion is an excellent version control tool. Its specific advantages and detailed introduction will not be discussed here

First, download and build the SVN server.

1,

yum install subversion

2. Disposition
2.1. Create warehouse
We're here  / home   Next create a file named   svn   In the future, all the code will be placed under this repository. After successful creation, several folders will be added under SVN.

Note: when creating a version library, you must pay attention to whether your user role is root or yourself    If not root    When building the version warehouse, it is in the home directory, that is / home    Directory. If the root directory is created as root, an error will be reported when we submit code

Prompt: no permission. The reason for this problem is to build the SVN server, including creating the Repository directory / home/svn. All subdirectories and files under the directory are carried out under the root user, so there is no problem checking out and submitting under the root user. If you can access the Repository directory from the client on Windows PC, there is a problem, because only the person with manager permission can fully control the Repository directory. If you access the Repository from the client on windows, because it belongs to the Users user group and has no permission to modify the Repository, you can't submit it. And when you change permissions, you can't change permissions because you're not root  

In order to avoid trouble, it is safest to build it under home

# cd /home
# mkdir svn
# svnadmin create /home/svn
# ls svn
conf  db  format  hooks  locks  README.txt

Let's pay special attention here   conf   Folder, where the configuration files are stored

authz  passwd  svnserve.conf

Of which:

authz is a permission control file
passwd is the account password file
svnserve.conf is the SVN service configuration file  
Next, we modify the three files in turn.
2.2. Configure passwd

# vi passwd
[users]
phper=phper
jser=jser

In the above example, we created two users, a phper and a jser

2.3 configuration authz

# vi authz
[/]             //All documents under the warehouse
phper=rw        //Readable and writable
js=r            //read-only
*=              //Other users do not have any permissions

2.4. Expansion: use user grouping
I don't usually use this, but write it down  
Or this file:

# vi authz
[groups]
php = phper
js= jser,jser2
[/]
@php= rw
@js= r
* =


Two groups are created in the above configuration. The user of group 1 is readable and writable, and the user of group 2 is read-only.

Format Description:

Version library directory format:  
[< version Library >: / project / directory]  
@< user group name > = < Permission >  
< user name > = < Permission >

3. Configure svnserve.conf

# vi svnserve.conf 

Open the following 5 comments

anon-access = read #Anonymous user readable
auth-access = write #Authorized user writable
password-db = passwd #Which file is used as the account file
authz-db = authz #Which file is used as the permission file
realm = /home/svn # Authentication space name, directory of version Library

be careful:

Remember to change the realm in the last line to your svn directory
When opening the comment, remember not to leave a space in front, otherwise there may be a problem (it's said on the Internet, I didn't test it personally)
4. Start and stop

# svnserve -d -r /home/svn
# killall svnserve


In the above start command,  - d   Represents a daemon,  - r   Indicates execution in the background. Stopping can also kill the process:

# ps -ef|grep svnserve
root      4908     1  0 21:32 ?        00:00:00 svnserve -d -r /home/svn
root      4949  4822  0 22:05 pts/0    00:00:00 grep svnserve
# kill -9 4908


5. Client connection
Here, use TortoiseSVN and enter the address   svn: / / your IP   You can connect successfully without accidentally entering your user name and password.

The default port is 3690. If you modify the port, remember to add the port number.

SVN client tutorial under Linux

1. Svn introduction
The full name of SVN is subversion, i.e. version control system. Like CVS, SVN is a cross platform software that supports most common operating systems. As an open source version control system, subversion manages data that changes over time. This data is placed in a central repository. The archive is much like an ordinary file server, but it remembers every file change. In this way, you can restore the file to the old version or browse the change history of the file. Subversion is a general-purpose system that can be used to manage any type of file, including program source code.

2. Svn installation
Installation tutorial: installing SVN server under Linux

3. Svn use
3.1. Check out the file to the local directory

svn checkout svn_path local_path
//For example:
svn checkout svn://192.168.1.131/45dian/brand
//It is recommended to add a local directory:
svn checkout svn://192.168.1.131/45dian/brand ./brand/
//Abbreviation  
svn co

3.2. Add a new file to the version Library

svn add file
//For example (add test.php):  
svn add test.php 
//Add all php files in the current directory
svn add *.php
//Add user directory (add all contents under the directory (recursively)
svn add user

After adding, you need to submit to the version library.

3.3. Submit the changed documents to the version library

svn commit -m 'Note Content ' [-N] [--no-unlock] PATH
//Abbreviation  
svn ci
//Submission folder and directory
svn ci -m 'Add new file' test.php
svn ci -m 'Add new directory(recursion)' user

3.4 locking / unlocking

svn lock -m 'Lock comment content' [--force] PATH
//For example:
svn lock -m "Lock file" test.php
//Unlock content
svn unlock PATH 

3.5. Updated version
Before modifying a file, you must first update the version library, then modify the file, and then submit.  
If you are prompted to expire when submitting because of a conflict, you need to update and modify the file first, then clear svn resolved, and then submit the commit.

svn update -r m PATH
//Update to the latest version:
svn update
//Restore files to historical version 200
svn -r 200 test.php
//Update test.php to the latest version
svn update test.php
//Abbreviation
svn up

3.6. View file or directory status

svn status PATH
//Displays the status of files and subdirectories, not normally
// ?  Not under the control of svn
// M content modified
// C conflict
// Add A subscription to the version Library
// K locked
svn status -v PATH
//For example:
svn status
svn status -v
//Abbreviation
svn st

3.7 deleting files

svn delete PATH -m 'Note Content '
//For example:
svn delete svn://192.168.1.133/45dian/brand/test.php -m 'delete files in svn'
//Or (recommended)
svn delete test.php
svn ci -m 'Submit deleted files'
//Abbreviation
svn (del,remove,rm)

3.8. View log

svn log PATH
//For example:
//Display the modification record of this file and the change of version number
svn log
svn log test.php

3.9. View file details

svn info PATH
//For example:
//Displays information about the current directory
svn info
//Display test.php file information
svn info test.php

3.10. Differences in comparison documents and directories

svn diff PATH
//Compare the modified file with the latest version in the warehouse
svn diff test.php
//Comparison between versions
svn diff -r m:n PATH
//Compare the difference between version m and version n
svn diff -r 200:201 test.php

3.11. Merge the differences between the two versions into the current file

//Merge m and n versions into the current file
svn merge -r m:n path
//for example
svn merge -r 200:201 test.php
//However, conflicts usually arise and need to be handled

3.12 SVN help

svn help
svn help ci

3.13. Add folder in version warehouse

//Add folder in svn version repository
svn mkdir PATH
//Equivalent to
mkdir work
svn add work -m 'add folders'

3.14. Code base URL change

svn switch (sw): Update working copy to different URL. 
usage: 
    1,switch URL [PATH]
    2,switch –relocate FROM TO [PATH...]
 
1,Update your working copy and map to a new one URL,Its behavior is similar to“ svn update"Very similar, will also
     The file on the server is merged with the local file. This is to map the working copy to a branch or tag in the same warehouse
     method.
2,Overwrite working copy URL Metadata to reflect pure URL When the root of the warehouse URL change 
    (For example, the scheme name or host name changes),However, the working copy is still mapped to the same directory in the same warehouse
    This command updates the correspondence between the working copy and the warehouse.

3.15 conflict resolution
svn resolved: removes the conflicting state of the directory or file of the working copy.
Usage: resolved PATH
Note: This subcommand will not resolve conflicts or remove conflict markers according to syntax; It just removes the conflict
Relevant files, and then let PATH submit again.
Install svn version management on linux server to automatically deploy code to the project
http://bbs.aliyun.com/read/9715.html?spm=5176.7114037.1996646101.1.W3zw3X&pos=1 

http://v5sheji.com/archives/setupsvnonlinux.html

1. Install svn server  

yum install subversion   

Download and install svn server side from image  

You will be prompted whether to ok, enter y and confirm  
Prompt for successful installation:... complete!  
Execute the following commands in sequence:  

cd /usr/local/              //Enter the directory and prepare to create the svn directory  
mkdir svnRepo                   //Create an svn directory  
chmod -R 777 svnRepo            //Modify directory permission to 777  
svnadmin create /usr/local/svnRepo/first  //Create a svn version repository first(first can be named at will)  
cd first/conf               //Enter the configuration file directory under the first version warehouse  

2. Next, modify the three configuration files in this directory  

  (1)   svnserve.conf     // Configure the path of version library information, user file and user password file, and version library path  

anon-access = none       //The default is read-only read
auth-access = write      //Write permission after authentication
password-db = passwd     //Account password profile
authz-db = authz         //Permission profile
realm = first            //Change to your own version library effective range

(2)   authz      // Files, creating svn groups and permissions for group users  

[group]  
first = ddl,shl       //Create a first group and make two users ddl and shl  
 
[/]                   //Specify the permissions under the root directory  
@first = rw           //The first group user permissions are read and write  
* = r                 //Other users have read permission only  

(3)   passwd    // Create or modify user passwords  

[users] 
 
ddl = 123456    //User name = password  
shl = 123456    //. . .  

3. Then set the self start  

  Open self startup file   / etc/rc.local    

#!/bin/sh 
# 
# This script will be executed *after* all the other init scripts. 
# You can put your own initialization stuff in here if you don't 
# want to do the full Sys V style init stuff. 
 
touch /var/lock/subsys/local 

Add the following line  

svnserve -d -r /usr/local/svnRepo/first 

svn command:  

lsof -i :3690   see svn Start 
 
ps aux |grep 'svn'  Find all svn Started process 
 
kill -9 2505    Kill 2505 this found svn process 
 
svnserve -d -r /usr/local/svnRepo/first start-up svn(You can put this in/etc/local/rc.local File to realize startup and self startup)

netstat -anp|grep svnserve   Check it out SVN information

SVN version library startup mode. Now there are first and test version libraries under svnRepo   

1: Single version library startup      svnserve -d -r /usr/local/svnRepo/first  
2: Multi version library startup      svnserve -d -r /usr/local/svnRepo  
The difference lies in the directory specified by the start parameter - r in the command when starting svn.  
 
4. Restrict the operation permissions of different users on different version libraries, and modify the authz file under the conf directory in the version library  
Take configuring the first version library as an example:

 
authz 


[groups] 
 
company = user1,user2 
 
[first:/]             //Specify the permissions under the version library and directory  
 
@company = rw     //The company group user permissions are read and write  
 
* = r           //Other users have read permission only  

passwd sets the account and password of users in the group  

[users] 
 
user1  = 123456  
user2 = 123456 

5. Client access  

Suppose the client uses tortoiseSVN  
Open the resource library browser and enter the address, svn: / / your svn server ip:3690  
Enter username ddl password 12345  
Because there are no files in the network resource library, you need to right-click the client [create foder] and then [add foder]

6. Use post commit to realize automatic deployment

  svn is not built just to store the code in the warehouse, but to synchronously deploy it to the corresponding project.

stay   There are a lot of tmpl in / hooks of svn directory. These tmpl can do many things, but today we will talk about automatic deployment of post-commit.tmpl.

The tmpl file is a template file. If we want to use post commit, we need to manually copy the contents of the corresponding tmpl to the new post commit

cp post-commit.tmpl post-commit

step.1:
Then open the file, empty the contents, and modify it to the following code:

#!/bin/sh
export LANG=zh_CN.UTF-8
/usr/bin/svn update --username **** --password **** /var/www/test

The first line means: use sh to parse the script, because there are subtle differences in the syntax of various shell s

The second line is the encoding format: Here I use UTF-8

Last line / usr/bin/svn   It is the path of svn, not the project path. It should be the same. The last half sentences are user name, password and project path respectively-- username is a bit of a pit. Many of the results found on the Internet are wrong. In fact, there are two minus signs in front, which are under the English input method.

step.2:
Modify the executable permissions of post commit

chmod 755 post-commit

step.3:

Enter the project directory / var/www/test on the server   checkout the entire project.

Note that the checkout folder should not be used here. The correct code should be as follows:

svn checkout svn: / / your server address: {port number} / test   // Note that there is another point after the directory
Note: you do not need to add / test here:(   The purpose of adding is to realize multiple projects.  )

svn checkout svn: / / your server address: 3690. / / note that there is a dot after the directory
So far, if you follow this step, there should be no problem. You can automatically synchronize the local commit code to the project.

6. Common mistakes I encountered

255 error: Post commit does not give execution permission or the header is not executed with SH, that is, there is no #! / bin/sh
Prompt to skip Directory: there is no checkout code in the server directory. Remember to check out first before synchronization (!, required for each new project)
There is no prompt: this happens when you use the tool to commit. It succeeds, but there is no synchronization. It is recommended that you go to linux and use. / post commit to manually read the error. If the code is garbled, please change the encoding format in the file to GBK
Prompt * * * * is a directory: / usr/bin/svn you made a mistake. It is not a directory, but the path of svn

Topics: Linux svn server