将SSH配置为AWS EC2
1.生成SSH键
cd ~/.ssh
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
输入文件名:“ key_name”
ls
并列出.ssh文件夹的文件,您应该看到“ key_name”和“ key_name.pub”
- 向授权密钥添加公钥
cat github-actions.pub >> ~/.ssh/authorized_keys
有关SSH密钥生成过程的详细信息检查以下参考:https://zellwk.com/blog/github-actions-deploy/
2.设置github秘密
ssh_private_key:我们在EC2上创建的私钥
host_name / ip_address:EC2的弹性IP或IP
user_name:EC2用户的用户名。
3.在.github/workflows下创建一个branch_name.yml(对于dev
branch dev.yml
)文件
将开发人员更新为您的分支名称
name: Deploy
on:
push:
branches: [ dev ]
jobs:
Deploy:
name: Deploy to EC2
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Build & Deploy
env:
PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
HOSTNAME: ${{secrets.SSH_HOST}}
USER_NAME: ${{secrets.USER_NAME}}
run: |
echo "$PRIVATE_KEY" > private_key && chmod 600 private_key
ssh -o StrictHostKeyChecking=no -i private_key ${USER_NAME}@${HOSTNAME} '
# Now we have got the access of EC2 and we will start the deploy .
cd /home/ubuntu/<PROJECT_DIRECTORY> &&
git checkout dev &&
git fetch --all &&
git reset --hard origin/dev &&
git pull origin dev &&
sudo npm i &&
sudo npm run build &&
sudo pm2 stop ./dist/index.js &&
sudo pm2 start ./dist/index.js
'
要点参考:https://gist.github.com/raviagheda/c69ae5e884f4490b1af656dbd80c00dd
享受!
如果您在这里,则意味着您可能很喜欢阅读此博客。只需跟随我"Ravi Agheda",它将激励写更多并为开源做出贡献。您可以让我成为coffee☕️。小额支持有很长的路要走!