0
0

More than 1 year has passed since last update.

Github ActionsでVPSに自動デプロイする(AWS lightsail)

Posted at

環境(VPSでなくても大丈夫なはず)

VPS: Amazon Lightsail(node 18)

VPS内でgit cloneを行う(公式が推奨しているHTTPSでクローン)

指定したディレクトリでいつもの

コマンド
git clone https://github.com/ユーザ名/リポジトリ名.git

でクローン。

ターミナル
git remote -v
origin  https://github.com/username/git-repository.git (fetch)
origin  https://github.com/username/git-repository.git (push)

リモートリポジトリが指定されていることを確認。

2021年8月13日にパスワード認証がなくなったため、SSHかアクセストークンを生成しないとcloneできないとのこと。

今回はトークンを作成し、リモートリポジトリのURLを変更すると、cloneやpullなどができるようになる。

トークンの作成

にアクセスし、左下の「Developer Settings」を選択し、
「Personal Access Setting」->「Token(classic)」を選択。
「Generete new Token(classic)」を選択

「Note」に任意の名前を
Expiration(期限)は自由だが、無期限はセキュリティの観点から 非推奨
「Select scopes」は
・repo (プライベートリポジトリにアクセスする権限)
・workflow(github actionsで使用可能にする権限)
を選択。

Generete Tokenを押しトークンを生成。

トークンの中身は1度だけしか表示されないのでどこかにメモしておこう!

トークンを使う

作成したトークンはパスワード認証の代わりとして作成したが、今回は自動デプロイを行うため、
cloneやpull時にトークンを入力する手間をなくしたい。そのため、今からcloneやpullを行った際に
トークンを入力しなくて済むように設定する。

ターミナル
#指定したリモートリポジトリを削除する。(githubのリポジトリは消えませんので安心を)
git remote rm origin

#リモートリポジトリを確認
git remote -v
- origin  https://github.com/username/git-repository.git (fetch)
- origin  https://github.com/username/git-repository.git (push)
+ (表示されない)

#トークンを用いたリモートリポジトリを指定({username}にユーザ名、{password}にトークンを指定。********はリ
ポジトリ名)
git remote add origin https://{username}:{password}@github.com/{username}/********.git

#再度リモートリポジトリを確認
-(表示されない)
+ origin https://{username}:{password}@github.com/{username}/********.git (fetch)
+ origin https://{username}:{password}@github.com/{username}/********.git (push)

と表示されれば設定完了!
あとはGithub ActionsでSSHでVPSに接続し、その中でpullやmergeなどを行うコードをいじる。

注意点!

Github Actionsはlogの部分にerrと書いてあるときがあるが、エラーを吐いているわけではないことがあるので注意!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0