Hello,
Please see my gitlab-ci.yml
file below:
default:
image: docker:19.03.10
tags:
- docker
.deployment_script: &deployment_script
- echo "Starting deployment to $DEPLOYMENT_DIRECTION ..."
- TASK_DEFINITION=$(aws ecs describe-task-definition --task-definition "$TASK_DEFINITION_NAME" --region "${AWS_DEFAULT_REGION}")
- NEW_CONTAINER_DEFINTIION=$(echo $TASK_DEFINITION | jq --arg IMAGE "$REPOSITORY_URL:$IMAGE_TAG" --arg ENV_FILE "$ENV_FILE" '.taskDefinition.containerDefinitions[0].image = $IMAGE | .taskDefinition.containerDefinitions[0].environmentFiles[0].value = $ENV_FILE | .taskDefinition.containerDefinitions[0]')
- echo "Registering new container definition ..."
- aws ecs register-task-definition --region "${AWS_DEFAULT_REGION}" --family "${TASK_DEFINITION_NAME}" --container-definitions "${NEW_CONTAINER_DEFINTIION}" --cpu "${CPU_SIZE}" --memory "${MEMORY_SIZE}" --requires-compatibilities "FARGATE" --network-mode "awsvpc" --execution-role-arn "arn:aws:iam::287460153330:role/ecsTaskExecutionRole"
- echo "Updating the service ..."
- aws ecs update-service --region "${AWS_DEFAULT_REGION}" --cluster "${CLUSTER_NAME}" --service "${SERVICE_NAME}" --task-definition "${TASK_DEFINITION_NAME}"
services:
- docker:dind
variables:
REPOSITORY_URL: **********.dkr.ecr.***-****-1.amazonaws.com/*******/****
before_script:
- echo "Creating image tag ..."
- IMAGE_TAG="$(echo $CI_COMMIT_SHA | head -c 8)"
stages:
- build
- deploy
build:
stage: build
script:
- >
- echo $DEPLOY_ENVIRONMENT ...
- echo "Adding dependencies ..."
- apk add --no-cache curl jq python py-pip
- pip install awscli
- aws configure set aws_access_key_id $AWS_ACCESS_KEY_ID
- aws configure set aws_secret_access_key $AWS_SECRET_ACCESS_KEY
- aws configure set region $AWS_DEFAULT_REGION
- $(aws ecr get-login --no-include-email --region "${AWS_DEFAULT_REGION}")
- echo Logging in to Docker Hub ...
- echo $DOCKER_PASSWORD | docker login --username $DOCKER_USERNAME --password-stdin
- echo "Building image ..."
- docker build -f ./TestWebCore/Dockerfile -t $REPOSITORY_URL:latest ./TestWebCore
- echo "Tagging image..."
- docker tag $REPOSITORY_URL:latest $REPOSITORY_URL:$IMAGE_TAG
- echo "Pushing image ..."
- docker push $REPOSITORY_URL:latest
- docker push $REPOSITORY_URL:$IMAGE_TAG
rules:
- if: $CI_COMMIT_BRANCH == "develop"
when: always
- if: $CI_COMMIT_BRANCH == "main"
when: always
deploy-staging:
stage: deploy
environment: staging
dependencies:
- build
variables:
DEPLOYMENT_DIRECTION: "staging"
CPU_SIZE: "512"
MEMORY_SIZE: "1024"
ENV_FILE: "arn:aws:s3:::cc-web-services-env/cc-core-staging.env"
image: registry.gitlab.com/gitlab-org/cloud-deploy/aws-base:latest
script:
- *deployment_script
rules:
- if: $CI_COMMIT_BRANCH == "develop"
when: manual
deploy-production:
stage: deploy
environment: production
dependencies:
- build
variables:
DEPLOYMENT_DIRECTION: "production"
CPU_SIZE: "1024"
MEMORY_SIZE: "2048"
ENV_FILE: "arn:aws:s3:::cc-web-services-env/cc-core.env"
image: registry.gitlab.com/gitlab-org/cloud-deploy/aws-base:latest
script:
- *deployment_script
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
when: manual
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
when: manual
I am able to trigger the pipeline on my develop
branch but for some reason I have failed to trigger a deployment to production. I am not sure what I could be doing wrong. I appreciate all the help.
Regards,