아 이건 전에 한번 해봤다.
저 빨간 박스가 Github Actions 이다.
OK
Repo 생성
토큰 뽑기
난 실습 이후 내 노트북에서도 쓰려고 그냥 expire 제한을 풀었다.
#
GITUSER=btwoeyes
git clone https://github.com/$GITUSER/cicd-2w.git
tree cicd-2w/
cp server.py cicd-2w/
cd cicd-2w/
#
git status
git add .
git commit -m "first commit"
git push origin main
Username for 'https://github.com': btwoeyes$
Password for 'https://gasida@github.com': <토큰값>
push 이후, 파일 배포까지 많은 과정이 있다.
목표는 공수의 감소. 인력으로 하는 행위를 줄이고, 휴먼 에러를 없애는 것이 목표이다
최근 맡고있는 사업 진행하면서, 손 배포해봤는데, 눈 빠진다. 옆에서 이사님의 불만이 쏟아지신다. ㅋㅋ 자동화 배워보자.
대상 : SSH_PRIVATE_KEY, EC2_PIP
.github/workflows/deploy.yaml을 생성한다.
workflows여야만 한다.(workflow안된다.)
#
git clone https://github.com/gasida/cicd-2w.git
cd cicd-2w
#
mkdir -p .github/workflows/
touch .github/workflows/deploy.yaml
sed -i -e "s/CICD/CICD 2w/g" server.py
나는 Amazon-Linux 2023에서 진행했다. 그래서 ubuntu를 ec2-user로 변경했다.
별 내용 없으므로, ubuntu-latest는 특이사항 없음.
name: CICD1
on:
workflow_dispatch:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Configure the SSH Private Key Secret
run: |
mkdir -p ~/.ssh/
echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
- name: Set Strict Host Key Checking
run: echo "StrictHostKeyChecking=no" > ~/.ssh/config
- name: Git Pull
run: |
export MY_HOST="${{ secrets.EC2_PIP }}"
ssh ec2-user@$MY_HOST << EOF
cd /home/ubuntu/cicd-2w || exit 1
git pull origin main || exit 1
EOF
- name: Run service
run: |
export MY_HOST="${{ secrets.EC2_PIP }}"
ssh ec2-user@$MY_HOST sudo fuser -k -n tcp 80 || true
ssh ec2-user@$MY_HOST "nohup sudo -E python3 /home/ec2-user/cicd-2w/server.py > /home/ec2-user/cicd-2w/server.log 2>&1 &"
git add . && git commit -m "add workflow" && git push origin main
# [서버1]
cd cicd-2w/
grep -i cicd server.py
sudo ps -ef |grep server.py
tail /home/ubuntu/cicd-2w/server.log
git config --global credential.helper store
git push origin main
위 명령어를 통해 credential을 영구적으로 서버에 등록했다.
# /home/ymw/1.2/cicd-2w/.github/workflows/deploy.yaml 수정
name: CICD2
on:
workflow_dispatch:
push:
branches:
- main
jobs:
deployfinal:
runs-on: ubuntu-latest
steps:
- name: Test
run: |
python -V || true
python3 -V || true
which python || true
which python3 || true
env
ACCESSKEY : asdf1234
SECRETKEY : qwer1234
https://github.com/appleboy/ssh-action
name: CICD2
on:
workflow_dispatch:
push:
branches:
- main
jobs:
ssh-deploy:
runs-on: ubuntu-latest
steps:
- name: Github Repository Checkout
uses: actions/checkout@v4
- name: executing remote ssh commands
uses: appleboy/ssh-action@v1.2.0
env:
AWS_KEYS: ${{ secrets.MYKEYS }}
with:
host: ${{ secrets.EC2_PIP }}
username: ec2-user
key: ${{ secrets.SSH_PRIVATE_KEY }}
envs: AWS_KEYS
script_stop: true
script: |
cd /home/ec2-user/cicd-2w
echo "$AWS_KEYS" > .env
ssh-action이라는 job을 이용해 Github에 등록된 Secret을 원격지 서버에 echo를 동작시킴으로서, .env파일을 만들었다!!
name: CICD2
on:
workflow_dispatch:
push:
branches:
- main
jobs:
scp-ssh-deploy:
runs-on: ubuntu-latest
steps:
- name: Github Repository Checkout
uses: actions/checkout@v4
- name: executing remote ssh commands
uses: appleboy/ssh-action@v1.2.0
env:
AWS_KEYS: ${{ secrets.MYKEYS }}
with:
host: ${{ secrets.EC2_PIP }}
username: ec2-user
key: ${{ secrets.SSH_PRIVATE_KEY }}
envs: AWS_KEYS
script_stop: true
script: |
cd /home/ec2-user/cicd-2w
echo "$AWS_KEYS" > .env
sudo fuser -k -n tcp 80 || true
- name: copy file via ssh
uses: appleboy/scp-action@v0.1.7
with:
host: ${{ secrets.EC2_PIP }}
username: ec2-user
key: ${{ secrets.SSH_PRIVATE_KEY }}
source: server.py
target: /home/ec2-user/cicd-2w
선정했던, server.py가 바로 배포되었다.
from http.server import ThreadingHTTPServer, BaseHTTPRequestHandler
from datetime import datetime
class RequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header('Content-type', 'text/plain')
self.end_headers()
now = datetime.now()
response_string = now.strftime("The time is %-I:%M:%S %p, CICD2 End\n")
self.wfile.write(bytes(response_string, "utf-8"))
def startServer():
try:
server = ThreadingHTTPServer(('', 80), RequestHandler)
print("Listening on " + ":".join(map(str, server.server_address)))
server.serve_forever()
except KeyboardInterrupt:
server.shutdown()
if __name__== "__main__":
startServer()
name: CICD2
on:
workflow_dispatch:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Github Repository Checkout
uses: actions/checkout@v4
- name: copy file via ssh
uses: appleboy/scp-action@v0.1.7
with:
host: ${{ secrets.EC2_PIP }}
username: ec2-user
key: ${{ secrets.SSH_PRIVATE_KEY }}
source: server.py
target: /home/ec2-user
- name: executing remote ssh commands
uses: appleboy/ssh-action@v1.2.0
env:
AWS_KEYS: ${{ secrets.MYKEYS }}
with:
host: ${{ secrets.EC2_PIP }}
username: ec2-user
key: ${{ secrets.SSH_PRIVATE_KEY }}
envs: AWS_KEYS
script_stop: true
script: |
cd /home/ec2-user/cicd-2w
echo "$AWS_KEYS" > .env
sudo fuser -k -n tcp 80 || true
rm server.py
cp /home/ec2-user/server.py ./
nohup sudo -E python3 /home/ec2-user/cicd-2w/server.py > /home/ec2-user/cicd-2w/server.log 2>&1 &
echo "test" >> /home/ec2-user/text.txt
스크립트를 보면, server.py를 삭제하고, 복사해서 넣고, python3를 동작시킨다.
git add . && git commit -m "Deploy CICD2 Final" && git push origin main