Github의 Actions에서 Rsync를 이용한 배포
전제 조건
하고 싶어
환경 주변 설정
사용할 리포지토리의 Settings > Secrets로 들어가서 서버에 연결 정보를 등록합니다.
SSH용 키 등록
SSH용 사용자 이름 등록
SSH용 Host 등록
Actions workflow 만들기
대상 리포지토리의 「Actions」에 들어가 새롭게 Templete를 이용해 작성해도 좋습니다.
트리거 부분
요구 내용에도 쓰고 있습니다만, 간단한 프로젝트를 상정하고 있기 때문에 Staging·프로덕션 환경에 관계없이 배포는 branch의 push로 자동 배포합니다.
name: deploy dev
on:
push:
branches:
- deploy/*
만약 프로덕션을 수동으로 하고 싶은 경우는 「 Github의 Actions에서 Laravel 배포 」를 참고해 주세요.
환경 변수 정의 부분
二つのブランチ(Staging、Production)についての変数を設定します。
steps:
- uses: actions/checkout@v2
- name: Set staging env
if: github.ref == 'refs/heads/deploy/staging'
run: |
echo ::set-env name=RSYNC_SOURCE::/
echo ::set-env name=RSYNC_TARGET::Staging用のDirecotry
- name: Set production env
if: github.ref == 'refs/heads/deploy/pro'
run: |
echo ::set-env name=RSYNC_SOURCE::/
echo ::set-env name=RSYNC_TARGET::本番用のDirecotry
Rsync에서 소스 업
이번은 「 trendyminds/github-actions-rsync 」를 이용합니다만, 「ubuntu-latest」를 이용하므로 직접 써도 괜찮습니다.
- name: Deploy by rsync
uses: trendyminds/github-actions-rsync@master
with:
RSYNC_OPTIONS: -avzr --delete --exclude-from=.rsyncignore
RSYNC_TARGET: $RSYNC_TARGET
RSYNC_SOURCE: $RSYNC_SOURCE
env:
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
SSH_USERNAME: ${{ secrets.SSH_USERNAME }}
SSH_HOSTNAME: ${{ secrets.SSH_HOSTNAME }}
「.rsyncignore」의 파일로 대상외의 파일을 정의해 이용하고 있습니다.
SSH 연결 정보 설정 부분
속도를 향상시키기 위해 Cache를하는 방법을 넣었습니다. 정보 설정은 Cache가 없는 경우에만 작성하는 느낌입니다.
- name: Cache ssh config
id: cache-ssh-config
uses: actions/cache@v1
with:
path: /home/runner/.ssh
key: ${{ runner.os }}-ssh-config-${{ hashFiles('/home/runner/.ssh/private-key-for-deploy') }}
restore-keys: |
${{ runner.os }}-ssh-config-
- name: Make ssh config
if: steps.cache-ssh-config.outputs.cache-hit != 'true'
run: |
mkdir -p /home/runner/.ssh
touch /home/runner/.ssh/private-key-for-deploy
echo "${{ secrets.SSH_PRIVATE_KEY }}" > /home/runner/.ssh/private-key-for-deploy
chmod 600 /home/runner/.ssh/private-key-for-deploy
ssh-keyscan ${{ secrets.SSH_HOSTNAME }} >> ~/.ssh/known_hosts
SSH를 사용하여 서버에서 명령 실행
이쪽에 대해서는 「appleboy/ssh-action
잘 가지 않았던 정의입니다.
- name: Multiple command
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.SSH_HOSTNAME }}
username: ${{ secrets.SSH_USERNAME }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
envs: RSYNC_TARGET
script_stop: false
script: |
cd $RSYNC_TARGET
ls -al
이쪽이 직접 쓴 내용입니다.
- name: Make shell
run: |
touch ./ssh-script.sh
echo "cd $RSYNC_TARGET" >> ssh-script.sh
echo "ls -al" >> ssh-script.sh
cat ssh-script.sh
- name: Excute shell
run: |
ssh -i ~/.ssh/private-key-for-deploy ${{ secrets.SSH_USERNAME }}@${{ secrets.SSH_HOSTNAME }} 'bash -s' < ./ssh-script.sh
완성된 정의
name: deploy dev
on:
push:
branches:
- deploy/*
jobs:
deploy:
name: deploy
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set staging env
if: github.ref == 'refs/heads/deploy/staging'
run: |
echo ::set-env name=RSYNC_SOURCE::/
echo ::set-env name=RSYNC_TARGET::Staging用のDirectory
- name: Set production env
if: github.ref == 'refs/heads/deploy/pro'
run: |
echo ::set-env name=RSYNC_SOURCE::/
echo ::set-env name=RSYNC_TARGET::本番用のDirectory
- name: Deploy by rsync
uses: trendyminds/github-actions-rsync@master
with:
RSYNC_OPTIONS: -avzr --delete --exclude-from=.rsyncignore
RSYNC_TARGET: $RSYNC_TARGET
RSYNC_SOURCE: $RSYNC_SOURCE
env:
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
SSH_USERNAME: ${{ secrets.SSH_USERNAME }}
SSH_HOSTNAME: ${{ secrets.SSH_HOSTNAME }}
- name: Cache ssh config
id: cache-ssh-config
uses: actions/cache@v1
with:
path: /home/runner/.ssh
key: ${{ runner.os }}-ssh-config-${{ hashFiles('/home/runner/.ssh/private-key-for-deploy') }}
restore-keys: |
${{ runner.os }}-ssh-config-
- name: Make ssh config
if: steps.cache-ssh-config.outputs.cache-hit != 'true'
run: |
mkdir -p /home/runner/.ssh
touch /home/runner/.ssh/private-key-for-deploy
echo "${{ secrets.SSH_PRIVATE_KEY }}" > /home/runner/.ssh/private-key-for-deploy
chmod 600 /home/runner/.ssh/private-key-for-deploy
ssh-keyscan ${{ secrets.SSH_HOSTNAME }} >> ~/.ssh/known_hosts
- name: Make shell
run: |
touch ./ssh-script.sh
echo "cd $RSYNC_TARGET" >> ssh-script.sh
echo "ls -al" >> ssh-script.sh
cat ssh-script.sh
- name: Excute shell
run: |
ssh -i ~/.ssh/private-key-for-deploy ${{ secrets.SSH_USERNAME }}@${{ secrets.SSH_HOSTNAME }} 'bash -s' < ./ssh-script.sh
같은 기사 : htps : // 푹 ty- 낙비 t. 치 s와 ry. 코m/13
Reference
이 문제에 관하여(Github의 Actions에서 Rsync를 이용한 배포), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/FattyRabbit/items/70ec304a9ec466dcb43b
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
name: deploy dev
on:
push:
branches:
- deploy/*
二つのブランチ(Staging、Production)についての変数を設定します。
steps:
- uses: actions/checkout@v2
- name: Set staging env
if: github.ref == 'refs/heads/deploy/staging'
run: |
echo ::set-env name=RSYNC_SOURCE::/
echo ::set-env name=RSYNC_TARGET::Staging用のDirecotry
- name: Set production env
if: github.ref == 'refs/heads/deploy/pro'
run: |
echo ::set-env name=RSYNC_SOURCE::/
echo ::set-env name=RSYNC_TARGET::本番用のDirecotry
- name: Deploy by rsync
uses: trendyminds/github-actions-rsync@master
with:
RSYNC_OPTIONS: -avzr --delete --exclude-from=.rsyncignore
RSYNC_TARGET: $RSYNC_TARGET
RSYNC_SOURCE: $RSYNC_SOURCE
env:
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
SSH_USERNAME: ${{ secrets.SSH_USERNAME }}
SSH_HOSTNAME: ${{ secrets.SSH_HOSTNAME }}
- name: Cache ssh config
id: cache-ssh-config
uses: actions/cache@v1
with:
path: /home/runner/.ssh
key: ${{ runner.os }}-ssh-config-${{ hashFiles('/home/runner/.ssh/private-key-for-deploy') }}
restore-keys: |
${{ runner.os }}-ssh-config-
- name: Make ssh config
if: steps.cache-ssh-config.outputs.cache-hit != 'true'
run: |
mkdir -p /home/runner/.ssh
touch /home/runner/.ssh/private-key-for-deploy
echo "${{ secrets.SSH_PRIVATE_KEY }}" > /home/runner/.ssh/private-key-for-deploy
chmod 600 /home/runner/.ssh/private-key-for-deploy
ssh-keyscan ${{ secrets.SSH_HOSTNAME }} >> ~/.ssh/known_hosts
- name: Multiple command
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.SSH_HOSTNAME }}
username: ${{ secrets.SSH_USERNAME }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
envs: RSYNC_TARGET
script_stop: false
script: |
cd $RSYNC_TARGET
ls -al
- name: Make shell
run: |
touch ./ssh-script.sh
echo "cd $RSYNC_TARGET" >> ssh-script.sh
echo "ls -al" >> ssh-script.sh
cat ssh-script.sh
- name: Excute shell
run: |
ssh -i ~/.ssh/private-key-for-deploy ${{ secrets.SSH_USERNAME }}@${{ secrets.SSH_HOSTNAME }} 'bash -s' < ./ssh-script.sh
name: deploy dev
on:
push:
branches:
- deploy/*
jobs:
deploy:
name: deploy
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set staging env
if: github.ref == 'refs/heads/deploy/staging'
run: |
echo ::set-env name=RSYNC_SOURCE::/
echo ::set-env name=RSYNC_TARGET::Staging用のDirectory
- name: Set production env
if: github.ref == 'refs/heads/deploy/pro'
run: |
echo ::set-env name=RSYNC_SOURCE::/
echo ::set-env name=RSYNC_TARGET::本番用のDirectory
- name: Deploy by rsync
uses: trendyminds/github-actions-rsync@master
with:
RSYNC_OPTIONS: -avzr --delete --exclude-from=.rsyncignore
RSYNC_TARGET: $RSYNC_TARGET
RSYNC_SOURCE: $RSYNC_SOURCE
env:
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
SSH_USERNAME: ${{ secrets.SSH_USERNAME }}
SSH_HOSTNAME: ${{ secrets.SSH_HOSTNAME }}
- name: Cache ssh config
id: cache-ssh-config
uses: actions/cache@v1
with:
path: /home/runner/.ssh
key: ${{ runner.os }}-ssh-config-${{ hashFiles('/home/runner/.ssh/private-key-for-deploy') }}
restore-keys: |
${{ runner.os }}-ssh-config-
- name: Make ssh config
if: steps.cache-ssh-config.outputs.cache-hit != 'true'
run: |
mkdir -p /home/runner/.ssh
touch /home/runner/.ssh/private-key-for-deploy
echo "${{ secrets.SSH_PRIVATE_KEY }}" > /home/runner/.ssh/private-key-for-deploy
chmod 600 /home/runner/.ssh/private-key-for-deploy
ssh-keyscan ${{ secrets.SSH_HOSTNAME }} >> ~/.ssh/known_hosts
- name: Make shell
run: |
touch ./ssh-script.sh
echo "cd $RSYNC_TARGET" >> ssh-script.sh
echo "ls -al" >> ssh-script.sh
cat ssh-script.sh
- name: Excute shell
run: |
ssh -i ~/.ssh/private-key-for-deploy ${{ secrets.SSH_USERNAME }}@${{ secrets.SSH_HOSTNAME }} 'bash -s' < ./ssh-script.sh
같은 기사 : htps : // 푹 ty- 낙비 t. 치 s와 ry. 코m/13
Reference
이 문제에 관하여(Github의 Actions에서 Rsync를 이용한 배포), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/FattyRabbit/items/70ec304a9ec466dcb43b텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)