diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml index b29a47b..92af7c5 100644 --- a/.github/workflows/cd.yml +++ b/.github/workflows/cd.yml @@ -69,10 +69,28 @@ jobs: EC2_IP_ADDRESS=$(aws cloudformation describe-stacks --stack-name $STACK_NAME --region $REGION --query 'Stacks[0].Outputs[?OutputKey==`IPAddress`].OutputValue' --output text) ssh -i $KEY_FILE -o "StrictHostKeyChecking no" ubuntu@$EC2_IP_ADDRESS " + # install docker if it does not already exist + if ! [ -x "$(command -v docker)" ]; then + echo "Docker does not yet exist. Updating packages" + echo "Updating packages" + sudo apt-get update + sudo apt-get upgrade -y + + echo "Installing docker" + sudo apt-get install docker.io -y + sudo chmod 666 /var/run/docker.sock + + echo "Docker install complete" + fi + + echo "Pulling docker image ${{ env.DOCKER_IMAGE }}" docker pull ${{ env.DOCKER_IMAGE }} - # Kill previous container - docker kill \$(docker container ps -q) + if docker container ps -q; then + echo "Killing old docker container" + docker kill \$(docker container ps -q) + fi + echo "Starting container ${{ env.DOCKER_IMAGE }}" docker run -d -p 80:3000 --restart=unless-stopped ${{ env.DOCKER_IMAGE }} " diff --git a/template.yml b/template.yml index a86f3fe..4dc72cf 100644 --- a/template.yml +++ b/template.yml @@ -41,14 +41,9 @@ Resources: SecurityGroups: - !Ref SSHSecurityGroup - !Ref HTTPTrafficSecurityGroup - UserData: !Base64 | - #!/bin/bash -ex - - sudo apt-get update - sudo apt upgrade -y - sudo apt install docker.io -y - - sudo chmod 666 /var/run/docker.sock + Tags: + - Key: Name + Value: pacman-overflow-ec2 ElasticIP: Type: AWS::EC2::EIP