aizu's Knowledge Base
    • Go to: aizu.my Homepage
    • Recent
    • Tags
    • Register
    • Login

    bitbucket pipeline - sample aws-cli, ecr

    Ref
    1
    1
    46
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • A
      aizu
      last edited by

      image: node:20
      
      pipelines:
        branches:
          development:
            - step:
                name: Build image, push to ECR and deploy to ECS
                caches:
                  - docker
                services:
                  - docker
                script:
                  # install packages
                  - apt-get update && apt-get install -y awscli
                  # create aws-cli credential file
                  - mkdir -p ~/.aws
                  - echo "[default]" > ~/.aws/credentials
                  - echo "aws_access_key_id=$AWS_DEV_ACCESS_KEY" >> ~/.aws/credentials
                  - echo "aws_secret_access_key=$AWS_DEV_SECRET_ACCESS_KEY" >> ~/.aws/credentials
                  - echo "[default]" > ~/.aws/config
                  - echo "region=$AWS_DEFAULT_REGION" >> ~/.aws/config
                  - echo "output=json" >> ~/.aws/config
                  # login to ECR
                  - aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username AWS --password-stdin $AWS_DEVELOPMENT_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com
                  # prepare the variables, get version number from package.json
                  - IMAGE_NAME="image_name_goes_here"
                  - VERSION=$(sed -n 's/.*"version":\ "\(.*\)".*/\1/p' package.json)
                  - VERSION_TAG="v${VERSION}"
                  # build docker image
                  - docker build -t $IMAGE_NAME:$VERSION_TAG .
                  - docker tag $IMAGE_NAME:$VERSION_TAG $AWS_DEVELOPMENT_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$IMAGE_NAME:$VERSION_TAG
                  - docker images
                  # send image to ECR
                  - docker push $AWS_DEVELOPMENT_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$IMAGE_NAME:$VERSION_TAG
                  # modify task definition template file
                  - aws s3 cp $TASK_DEF_FILE_S3_URL_LOCATION task-definition.json
                  - sed -i 's|"image":\ ".*"|"image":\ "'$AWS_DEVELOPMENT_ACCOUNT_ID'.dkr.ecr.'$AWS_DEFAULT_REGION'.amazonaws.com/'$IMAGE_NAME':'$VERSION_TAG'"|' task-definition.json
                  - sed -i 's|"environmentFiles":\ \[\]|"environmentFiles":\ \[{"value":\ "'$ENV_FILE_S3_ARN'","type":\ "s3"}\]|' task-definition.json
                  # add task into ECS & update services
                  - aws ecs register-task-definition --cli-input-json file://task-definition.json > task-definition-registration-out
                  - export UPDATED_TASK_DEFINITION=$(sed -n 's/.*"taskDefinitionArn":\ "\(.*\)".*/\1/p' task-definition-registration-out)
                  - aws ecs update-service --service image_name_goes_here --cluster dev-fargate-cluster --task-definition ${UPDATED_TASK_DEFINITION} > /dev/null
          staging:
            - step:
                name: Staging command goes here
                caches:
                  - docker
                services:
                  - docker
                script:
                  - echo "Hello!"
      definitions:
        services:
          docker:
            memory: 2048
      
      1 Reply Last reply Reply Quote 0
      • First post
        Last post