如何把一个遗留的应用程序放在Git中

64 阅读4分钟

[

Benjamin Rancourt

](benjaminrancourt.medium.com/?source=pos…)

Benjamin Rancourt

关注

6月24日

-

4分钟阅读

[

拯救

](medium.com/m/signin?ac…)

如何将遗留的应用程序放入Git

为了验证所有的应用程序都在一个版本控制系统中

你是否遇到过一个老的应用程序,在2022年,仍然不在一个版本控制系统(VCS)下,如GitApache Subversion?一个系统,为了修改它,你需要连接到应用程序所在的服务器,用vim直接修改文件?

在Ville de Sherbrooke,我们的一个主要系统是。有人可以轻易地引入新的变化或缺陷。只有做了修改的人才会知道为什么要这样做!

是时候开始跟踪该系统的变化了。

结构化

由于一个遗留系统可能非常复杂,我们需要对自己进行结构化,以确定我们要对其进行版本管理的内容和方式。

Git分支

我们的系统有两个环境:生产和开发。由于它们可能各自有不同的应用程序版本,因此需要在 Git 中有相应的分支:maindevelop

在考虑 Git 的时候,你也许可以去GitLabGitHub创建一个新的 Git 仓库。确保它是空的,因为这样会容易得多。

开发文件

由于开发者直接在服务器上做了修改,很可能有一些文件我们不希望被版本化,比如副本或待办事项。

因此,我们需要用一个允许列表(也称为白名单)来列出我们想要的文件。

我们可以从生产服务器上的文件开始列举,因为我们不想要的文件应该不多。

index.htmlapi.jssearch.jsmain.jsmain.cssREADME.md

一个简单的名为files.txt 的文本文件的例子,它列出了一个自定义应用程序的所有应被版本控制的文件。

如果你知道你想要的文件的扩展名,你可以用类似于下面的命令轻松地创建该文件。

find . -regex ".*\.js" -o -regex ".*\.css" > files.txt

它将列出所有符合重码的文件。你将能够修改这个基础文件以包括或排除更多的内容。

现在,我们的结构已经完成,让我们进入脚本编写阶段

脚本编写

由于我们不应该在生产服务器上添加Git(这可能会带来潜在的漏洞),我们将用scp下载我们想要的文件到本地机器上。

对于那些感兴趣的人,如果你是这个命令的新手,这里有一个有用的小抄

为我们的应用程序创建一个存档

# From our local computer, upload the file that contains the list of files we want to versionscp list.txt USERNAME@PRODUCTION_SERVER_NAME:/PATH_OF_OUR_APPLICATION# Connect to the production serverssh USERNAME@PRODUCTION_SERVER_NAME# Go to where the application iscd /PATH_OF_OUR_APPLICATION# Create an archive containing the files we wanttar --create --preserve-permissions --file APPLICATION_NAME.tar --files-from files.txt --verbose# Go back to our local computerexit

将源代码下载到我们的本地计算机上

# Create a new directory to receive the source codemkdir -p ~/Downloads/APPLICATION_NAME# Download our TAR archive to our computerscp USERNAME@PRODUCTION_SERVER_NAME:/PATH_OF_OUR_APPLICATION/APPLICATION_NAME.tar ~/Downloads# Extract the TAR archive to our new directorytar --extract --preserve-permissions --file ~/Downloads/APPLICATION_NAME.tar --directory ~/Downloads/APPLICATION_NAME --verbose

将源代码上传到GitLab的仓库中

# Go in the directory that now contains our source codecd ~/Downloads/APPLICATION_NAME# Initialize the Git repository in that directorygit initgit remote add origin git@gitlab.com:benjaminrancourt/APPLICATION.git# Add all files to the Git repositorygit add .# Commit changesgit commit -m "Source code from the production environment"# Create the main branchgit branch -M main# Push the main branch to GitLabgit push origin main

在另一个环境中重复上述步骤

还记得我们的遗留应用程序在不同的环境中可能有不同的版本吗?如果是这样的话,你可能会想知道哪些变化还没有在生产服务器上进行。

所以,我们要把这些版本推送到Git上,这样我们就可以轻松地进行比较。

例如,对于我的开发环境,我会做以下工作。

# Download the source code from the development server (follow the previous procedure)# Go in the directory that now contains our source codecd ~/Downloads/APPLICATION_NAME# Make sure we are on the main branchgit checkout main# Create a new branch from maingit checkout -b develop# Extract the archive from the development server tar --extract --preserve-permissions --file ~/Downloads/APPLICATION_NAME_DEV.tar --directory ~/Downloads/APPLICATION_NAME --verbose# Add all changesgit add .# Commit the changesgit commit -m "Source code from the development environment"# Push the develop branch to GitLabgit push origin develop

一个从developmain 的合并请求,在这里我可以很容易地识别尚未在生产中的改动。

结论

通过把你的遗留系统放入一个版本控制系统,你大大减少了丢失应用程序源代码的风险。

向更好的系统迈进了一步!😎

原文发表于 www.benjaminrancourt.ca.