Skip to content

updates docker rn command #2

updates docker rn command

updates docker rn command #2

Workflow file for this run

name: Deploy Image to EC2
on:
push:
branches:
- feature/deploy-to-ec2
jobs:
build-and-deploy-ec2:
runs-on: ubuntu-latest
permissions:
contents: write
steps:
# Step 1: Checkout the code
- name: Checkout code
uses: actions/checkout@v3
# Step 2: Set up Docker Buildx (needed for multi-platform builds)
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
# Step 3: Build the Docker image from the Dockerfile in src folder
- name: Build Docker image
run: |
docker build -t ${{ secrets.DOCKER_USERNAME }}/council-toolkit:${{ github.sha }} ./src
# Step 4: Log in to Docker Hub
- name: Log in to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
# Step 5: Push the Docker image to Docker Hub
- name: Push Docker image to Docker Hub
run: |
docker push ${{ secrets.DOCKER_USERNAME }}/council-toolkit:${{ github.sha }}
- name: Configure AWS CLI
uses: aws-actions/configure-aws-credentials@v2
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: eu-west-3
- name: Setup SSH Key
run: |
echo "${{ secrets.EC2_SSH_KEY }}" | base64 --decode > ec2_key.pem
chmod 600 ec2_key.pem
# Step 8: SSH into EC2 instance and check for existing containers
- name: SSH into EC2 and update containers
run: |
ssh -i ec2_key.pem -o StrictHostKeyChecking=no [email protected] << 'EOF'
# Pull the latest Docker image from Docker Hub
docker pull ${{ secrets.DOCKER_USERNAME }}/council-toolkit:${{ github.sha }}
# List the existing containers running the previous version of the image
old_containers=$(docker ps -q --filter "ancestor=${{ secrets.DOCKER_USERNAME }}/council-toolkit")
# Stop and remove the old containers if they exist
if [ ! -z "$old_containers" ]; then
echo "Removing old containers..."
docker stop $old_containers
docker rm $old_containers
fi
# Run the new container with the updated image
echo "Starting new container with the latest image..."
docker run -d --name council-toolkit -p 3002:3000 ${{ secrets.DOCKER_USERNAME }}/council-toolkit:${{ github.sha }} npm run start
EOF