try ssh deploy #1
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: (Prod) Build and push to ECR | ||
on: | ||
push: | ||
tags: | ||
# Only build and deploy when a new tag is created matching a semantic version. | ||
- 'v[0-9]+.[0-9]+.[0-9]+' | ||
jobs: | ||
build-and-deploy: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v4 | ||
- name: Install dependencies | ||
run: | | ||
npm install | ||
- name: Create .env file | ||
run: | | ||
echo AWS_ACCESS_KEY_ID=${{ secrets.AWS_ACCESS_KEY_ID }} >> .env | ||
echo AWS_SECRET_ACCESS_KEY=${{ secrets.AWS_SECRET_ACCESS_KEY }} >> .env | ||
echo AWS_DEFAULT_REGION=${{ secrets.AWS_REGION }} >> .env | ||
echo NUXT_PUBLIC_GDOC_ENV=prod >> .env | ||
- name: Build Nuxt.js application | ||
run: | | ||
npm run build | ||
- name: Create a temporary directory for deployment artifacts | ||
run: | | ||
mkdir -p dist | ||
- name: Copy generated files to dist directory | ||
run: | | ||
cp -r .output/* dist/ | ||
- name: Login to AWS | ||
uses: aws-actions/configure-aws-credentials@v1 | ||
with: | ||
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} | ||
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | ||
region: ${{ secrets.AWS_REGION }} | ||
- name: Connect to EC2 instance | ||
uses: appleboy/[email protected] | ||
with: | ||
hosts: ${{ secrets.EC2_HOSTNAME }} | ||
username: ec2-user | ||
key: ${{ secrets.SSH_PRIVATE_KEY }} | ||
script: | | ||
sudo systemctl stop gstream.service | ||
sudo rm -rf /var/www/gstream/* | ||
scp -r -P 22 dist/ ec2-user@${{ secrets.EC2_HOSTNAME }}:/var/www/gstream/ | ||
- name: Restart service | ||
uses: appleboy/[email protected] | ||
with: | ||
hosts: ${{ secrets.EC2_HOSTNAME }} | ||
username: ec2-user | ||
key: ${{ secrets.SSH_PRIVATE_KEY }} | ||
script: | | ||
sudo systemctl daemon-reload | ||
sudo systemctl start gstream.service | ||
sudo systemctl enable gstream.service |