Create docker-image.yml #2
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: Docker Build and Deploy | |
on: | |
push: | |
branches: | |
- develop | |
pull_request: # Add this line to trigger on PRs | |
branches: | |
- develop # or specify branches from which you want to allow PRs | |
jobs: | |
build-and-deploy: | |
runs-on: ubuntu-latest | |
env: | |
MONGO_PROD_DB: ${{ secrets.MONGO_PROD_DB }} | |
FRONTEND_LINK: "https://devpulse.org" | |
NODE_ENV: "production" | |
ADMIN_EMAIL: ${{ secrets.ADMIN_EMAIL }} | |
ADMIN_PASS: ${{ secrets.ADMIN_PASS }} | |
COORDINATOR_EMAIL: ${{ secrets.COORDINATOR_EMAIL }} | |
COORDINATOR_PASS: ${{ secrets.COORDINATOR_PASS }} | |
GH_TOKEN: ${{ secrets.GH_TOKEN }} | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
- name: Build and push Docker image | |
uses: mr-smithers-excellent/docker-build-push@v6 | |
with: | |
image: ${{ secrets.DOCKER_HUB_USERNAME2 }}/atlp-devpulse-bn | |
tags: | | |
latest | |
${{ github.sha }} # Use SHA for traceability | |
registry: docker.io | |
username: ${{ secrets.DOCKER_HUB_USERNAME2 }} | |
password: ${{ secrets.DOCKER_HUB_PASSWORD2 }} | |
buildArgs: | | |
NODE_ENV=production | |
MONGO_PROD_DB=${{ secrets.MONGO_PROD_DB }} | |
- name: Deploy to DigitalOcean | |
uses: appleboy/ssh-action@v0.1.0 # Specify a version | |
with: | |
host: ${{ secrets.DO_HOST }} | |
username: ${{ secrets.DO_USERNAME }} | |
key: ${{ secrets.DO_KEY }} | |
port: ${{ secrets.DO_PORT }} | |
envs: MONGO_PROD_DB,REGISTER_FRONTEND_URL,REGISTER_ORG_FRONTEND_URL,NODE_ENV,FRONTEND_LINK,ADMIN_EMAIL,ADMIN_PASS,COORDINATOR_EMAIL,COORDINATOR_PASS,GH_TOKEN | |
script: | | |
# Create environment file | |
cat << EOF > env | |
ADMIN_EMAIL=${{ secrets.ADMIN_EMAIL }} | |
ADMIN_PASS=${{ secrets.ADMIN_PASS }} | |
COORDINATOR_EMAIL=${{ secrets.COORDINATOR_EMAIL }} | |
COORDINATOR_PASS=${{ secrets.COORDINATOR_PASS }} | |
FRONTEND_LINK=${{ secrets.FRONTEND_LINK }} | |
REGISTER_FRONTEND_URL=${{ secrets.REGISTER_FRONTEND_URL }} | |
REGISTER_ORG_FRONTEND_URL=${{ secrets.REGISTER_ORG_FRONTEND_URL }} | |
GH_TOKEN=${{ secrets.GH_TOKEN }} | |
MONGO_PROD_DB=${{ secrets.MONGO_PROD_DB }} | |
NODE_ENV=${{ secrets.NODE_ENV }} | |
EOF | |
# Pull the latest image | |
docker pull ${{ secrets.DOCKER_HUB_USERNAME2 }}/atlp-devpulse-bn:latest | |
# Stop and remove existing container if it exists | |
docker stop $(docker ps -q --filter publish=4000) || true | |
docker rm $(docker ps -aq --filter publish=4000) || true | |
# Run new container | |
docker run -d \ | |
-p 4000:4000 \ | |
--env-file env \ | |
${{ secrets.DOCKER_HUB_USERNAME2 }}/atlp-devpulse-bn:latest | |
# Clean up | |
rm env |