Deploy to GCP VM #5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Deploy to GCP VM | |
on: | |
workflow_dispatch: # 버튼 생성 | |
jobs: | |
deploy: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
with: | |
ref: feature/deploy # feature/deploy 브랜치를 체크아웃 | |
- name: Authenticate to Google Cloud | |
uses: google-github-actions/auth@v1 | |
with: | |
credentials_json: '${{ secrets.GCP_SA_KEY }}' | |
- name: Clean up GCP authentication files | |
run: | | |
if gcloud auth list --format="value(account)" | grep -q '@'; then | |
gcloud auth revoke --all | |
fi | |
rm -f ~/.config/gcloud/application_default_credentials.json | |
- name: Set up Cloud SDK | |
uses: google-github-actions/setup-gcloud@v1 | |
with: | |
project_id: '${{ secrets.GCP_PROJECT_ID }}' | |
- name: Generate SSH key | |
run: | | |
ssh-keygen -t rsa -b 2048 -f ~/.ssh/id_rsa -q -N "" | |
- name: Add SSH key to GCP project metadata | |
run: | | |
gcloud compute project-info add-metadata --metadata "ssh-keys=${GCP_VM_USER}:$(cat ~/.ssh/id_rsa.pub)" | |
env: | |
GCP_VM_USER: '${{ secrets.GCP_VM_USER }}' | |
- name: Copy code to GCP VM | |
run: | | |
gcloud compute scp --recurse --quiet ./* $GCP_VM_USER@$GCP_INSTANCE_NAME:/home/$GCP_VM_USER/app --zone $GCP_ZONE | |
env: | |
GCP_INSTANCE_NAME: '${{ secrets.GCP_INSTANCE_NAME }}' | |
GCP_VM_USER: '${{ secrets.GCP_VM_USER }}' | |
GCP_ZONE: '${{ secrets.GCP_ZONE }}' | |
- name: Deploy to GCP VM | |
run: | | |
gcloud compute ssh $GCP_VM_USER@$GCP_INSTANCE_NAME --zone $GCP_ZONE --command " | |
cd /home/$GCP_VM_USER/app && | |
sudo docker compose -f sandol/docker-compose.yml up -d --build | |
" | |
env: | |
GCP_INSTANCE_NAME: '${{ secrets.GCP_INSTANCE_NAME }}' | |
GCP_VM_USER: '${{ secrets.GCP_VM_USER }}' | |
GCP_ZONE: '${{ secrets.GCP_ZONE }}' | |
- name: Delete leftover GCP credential files | |
run: | | |
rm -f ~/app/gha-creds-*.json |