24
loading...
This website collects cookies to deliver better user experience
If you want to get a proper idea of how to setup a pipeline, read my other article on it
docker pull
and docker run
won't cut it right? There are ports to be taken care of, volumes mapping, environment files.The script I am sharing here is not mine. It was created by someone else and I merely just edited it a bit to fit my requirements. I am unable to find the article of the original author of the script.
#!/bin/bash
########################################
# Put this on a Server
# run chmod +x deploy_app.sh to make the script executable
#
# Execute this script: ./deploy_app.sh docker-username/docker-appname:$TAG
# Replace the $TAG with the actual Build Tag you want to deploy
#
########################################
set -e
DOCKER_IMAGE=$1
CONTAINER_NAME="container-name"
# Check for arguments
if [[ $# -lt 1 ]] ; then
echo '[ERROR] You must supply a Docker Image to pull'
exit 1
fi
echo "Deploying $CONTAINER_NAME to Docker Container"
#Check for running container & stop it before starting a new one
if [ $(docker inspect -f '{{.State.Running}}' $CONTAINER_NAME) = "true" ]; then
docker stop $CONTAINER_NAME
fi
# Pull the latest image
echo "Pulling latest image for $DOCKER_IMAGE"
docker pull $DOCKER_IMAGE
echo "Starting $CONTAINER_NAME using Docker Image name: $DOCKER_IMAGE"
docker run -d --rm=true -p 80:5000 --env-file ~/Scripts/.env --name $CONTAINER_NAME $DOCKER_IMAGE
# Show the running processes
docker ps
--env-file
. However, this also checks if the container is already running. If so, the container is first removed and after the new container is run.CONTAINER_NAME
value.chmod +x <script_path>
Don't forget to pass the name of the image while running the script, eg: sh script.sh ghcr.io/deepjyoti30/ytmdl-web-v2:latest
latest
tag or the exact tag because otherwise a cached image of the container is used. This means the image is not updated at all. So make sure the tag is passed.ssh-action
as mentioned above to ssh into the server. Once done, that action will just run the script.name: Build app and deploy docker
on:
push:
branches:
- "production"
jobs:
push-to-docker:
# Write actions here to build and push the image
# to a hub like Docker Hub or Github Container Registry
# NOTE: Don't leave these lines as such, the workflow will fail.
deploy:
needs: push-to-docker
runs-on: ubuntu-latest
steps:
# SSH into the server
- name: SSH into server
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.SERVER_IP }} # Pass server IP
username: ${{ secrets.SERVER_USERNAME }} # Pass server username
key: ${{ secrets.KEY }} # Pass server ssh private key
script: /bin/bash Scripts/deploy_ytmdl_web.sh ghcr.io/deepjyoti30/ytmdl-web:latest