diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..35c53f6 --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,32 @@ +stages: + - test + - deploy + +variables: + DOCKER_DRIVER: overlay2 + +cache: + paths: + - node_modules/ + +test: + stage: test + image: node:18 + script: + - npm install + - npm run test + only: + - merge_requests + - setup-ci-cd +deploy: + stage: deploy + image: docker:20.10.7 + services: + - docker:20.10.7-dind + before_script: + - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY + - apk add --no-cache bash + script: + - bash deploy.sh + only: + - main diff --git a/deploy.sh b/deploy.sh new file mode 100644 index 0000000..bdb2e0e --- /dev/null +++ b/deploy.sh @@ -0,0 +1,22 @@ +#!/bin/bash + +# Navigate to the Terraform directory +cd terraform + +# Initialize Terraform +terraform init + +# Apply the Terraform scripts +terraform apply -auto-approve + +# Get the Droplet IP from Terraform +DROPLET_IP=$(terraform output -raw droplet_ip) + +# Navigate to the Docker directory +cd ../docker + +# Set environment variables +export DROPLET_IP=$DROPLET_IP + +# Deploy the Docker stack +docker -H tcp://$DROPLET_IP:2376 stack deploy -c docker-compose.yml myapp \ No newline at end of file