Deploy to staging #25
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 staging | |
on: | |
workflow_run: | |
workflows: [CI] | |
types: | |
- completed | |
branches: [staging] | |
jobs: | |
on-success: | |
runs-on: ubuntu-latest | |
if: ${{ github.event.workflow_run.conclusion == 'success' }} | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
- name: Install sshpass | |
run: sudo apt-get install sshpass | |
- name: Fetch .env file from server | |
run: | | |
sshpass -p ${{ secrets.PASSWORD }} scp -o StrictHostKeyChecking=no ${{ secrets.USERNAME }}@${{ secrets.HOST }}:~/staging-deployment/hng_boilerplate_expressjs/.env .env | |
env: | |
SSH_HOST: ${{ secrets.HOST }} | |
SSH_USERNAME: ${{ secrets.USERNAME }} | |
SSH_PASSWORD: ${{ secrets.PASSWORD }} | |
- name: Build Docker images | |
run: | | |
docker compose --env-file .env -f docker-compose.staging.yml build | |
- name: List Docker images | |
run: docker images | |
- name: Save Docker images to tarball | |
run: | | |
docker save hng_boilerplate_expressjs-backend_staging:latest | gzip > staging-images.tar.gz | |
- name: Upload artifact | |
uses: actions/upload-artifact@v2 | |
with: | |
name: staging-images | |
path: staging-images.tar.gz | |
- name: Copy Docker images to server | |
uses: appleboy/scp-action@master | |
with: | |
host: ${{ secrets.HOST }} | |
username: ${{ secrets.USERNAME }} | |
password: ${{ secrets.PASSWORD }} | |
source: "staging-images.tar.gz" | |
target: "~/images-tar" | |
- name: Deploy to server | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ secrets.HOST }} | |
username: ${{ secrets.USERNAME }} | |
password: ${{ secrets.PASSWORD }} | |
script: | | |
cd ~/staging-deployment/hng_boilerplate_expressjs | |
git stash | |
git checkout staging | |
git pull | |
docker load -i ~/images-tar/staging-images.tar.gz | |
docker compose -f docker-compose.staging.yml down | |
docker compose -f docker-compose.staging.yml up -d | |
on-failure: | |
runs-on: ubuntu-latest | |
if: ${{ github.event.workflow_run.conclusion == 'failure' }} | |
steps: | |
- run: echo "CI Workflow failed. Staging deployment was not triggered." |