4949 needs : build_and_push
5050
5151 steps :
52+ - name : Checkout the repo
53+ uses : actions/checkout@v2
54+
55+ - name : Copy docker-compose.yml to Droplet
56+ uses : appleboy/scp-action@master
57+ with :
58+ host : ${{ secrets.HOST }}
59+ username : ${{ secrets.USERNAME }}
60+ key : ${{ secrets.SSHKEY }}
61+ passphrase : ${{ secrets.PASSPHRASE }}
62+ source : " docker-compose.yml"
63+ target : " myapp"
64+
5265 - name : Deploy to Digital Ocean droplet via SSH action
5366 uses : appleboy/ssh-action@master
5467 with :
@@ -58,14 +71,21 @@ jobs:
5871 passphrase : ${{ secrets.PASSPHRASE }}
5972 envs : IMAGE_NAME,REGISTRY,{{ secrets.DIGITALOCEAN_ACCESS_TOKEN }},GITHUB_SHA
6073 script : |
74+ # Navigate to the app directory
75+ cd myapp
76+
77+ # Create the .env file from the GitHub Secret
78+ echo "${{ secrets.DOT_ENV_FILE }}" > .env
79+
6180 # Login to registry
6281 docker login -u ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }} -p ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }} registry.digitalocean.com
63- # Stop running container
64- docker stop $(echo $IMAGE_NAME)
65- # Remove old container
66- docker rm $(echo $IMAGE_NAME)
67- # Run a new container from a new image
68- docker run -d \
69- --restart always \
70- --name $(echo $IMAGE_NAME) \
71- $(echo $REGISTRY)/$(echo $IMAGE_NAME):$(echo $GITHUB_SHA | head -c7)
82+
83+ # Update the image definition in docker-compose.yml to use the new tag
84+ # This makes sure we run the exact version we just built
85+ sed -i 's|image: .*|image: $(echo $REGISTRY)/$(echo $IMAGE_NAME):$(echo $GITHUB_SHA | head -c7)|' docker-compose.yml
86+
87+ # Pull the new backend image
88+ docker-compose pull backend
89+
90+ # Stop and recreate ONLY the backend service with the new image
91+ docker-compose up -d --no-deps backend
0 commit comments