Gitリモートリポジトリ作成

Gitのリモートリポジトリを作成して、sshでアクセスする。

# Vagrantfile

```ruby:Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :

# Jenkins and Git and Rust
Vagrant.configure("2") do |config|
  config.vm.box = "bento/ubuntu-20.04"

  config.vm.network "private_network", ip: "192.168.33.10"

  config.vm.synced_folder "./data", "/vagrant_data"

  config.vm.provider "virtualbox" do |vb|
  #  vb.gui = true
    vb.memory = "2048"
  end

  # I know Vagrantfile does not require sudo. 
  # I've added sudo to the command so that it can be executed interactively.
  config.vm.provision "shell", inline: <<-SHELL
    sudo apt update

    # Create repository directory.
    sudo mkdir /var/lib/gitrepo
    cd /var/lib/gitrepo
    sudo git init --bare --shared project.git

    # Create group for git user.
    sudo groupadd git
    sudo gpasswd -a vagrant git
    sudo chown -R :git /var/lib/gitrepo

    # git clone ssh://localhost/var/lib/gitrepo/project.git project.git

  SHELL
end
```

環境は`vagrant up`するだけでできる。
リポジトリへのアクセス権がないとアクセスできないので、グループを作って対応した。

# 実行結果

まずは`git clone`。

```shell
$ git clone ssh://localhost/var/lib/gitrepo/project.git
Cloning into 'project'...
The authenticity of host 'localhost (::1)' can't be established.
ECDSA key fingerprint is SHA256:PRKtsYe7kZnpTU8+G7+a9KEMhRSsiyGXK2dOXQnJBQY.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'localhost' (ECDSA) to the list of known hosts.
vagrant@localhost's password:
warning: You appear to have cloned an empty repository.
```

ローカルリポジトリを更新する。

```shell
vagrant@vagrant:~$ cd project.git
vagrant@vagrant:~/project.git$ echo test > README.md
vagrant@vagrant:~/project.git$ git add .
vagrant@vagrant:~/project.git$ git commit -m 'initial commit'
[master (root-commit) 4dc4176] initial commit
 Committer: vagrant <vagrant@vagrant.vm>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:

    git config --global --edit

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 1 insertion(+)
 create mode 100644 README.md
```

`git push`する。

```shell
vagrant@vagrant:~/project.git$ git push origin master
vagrant@localhost's password:
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 211 bytes | 35.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To ssh://localhost/var/lib/gitrepo/project.git
 * [new branch]      master -> master
vagrant@vagrant:~/project.git$
```

うまくいった。Windowsホストからアクセスする場合は、22番ポートが2222番ポートにフォワードされていること、今回のユーザがvagrantであることを考慮して、以下のようにする。

```
> git clone ssh://vagrant@localhost:2222/var/lib/gitrepo/project.git project.git
Cloning into 'project.git'...
vagrant@localhost's password:
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.
```

# 振り返り
1. 次はJenkinsとの連携だ。

# 参考資料
1. [CentOSにGitサーバー（Apache）を構築](https://qiita.com/Imasug/items/ae110d39957579ef4e9b)