-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-docker-image.sh
More file actions
39 lines (31 loc) · 1.19 KB
/
build-docker-image.sh
File metadata and controls
39 lines (31 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/bin/sh
# Install awscli
apt update && apt install -y awscli
# Set environment variables
eval $(ssh-agent)
export IMAGE_NAME=$BITBUCKET_REPO_SLUG
export DOCKER_BUILDKIT=1
# Configure AWS Credentials
export AWS_REGION=${AWS_REGION}
export AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID}
export AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY}
# Configure AWS CLI
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 aws_region ${AWS_REGION}
# Set SSH key
mkdir -p $(dirname ${SSH_KEY})
echo ${PRIVATE_KEY} | base64 --decode > ${SSH_KEY}
echo ${PUBLIC_KEY} | base64 --decode > ${SSH_KEY}.pub
chmod 600 ${SSH_KEY}
# Build the docker image
echo "Building the docker image..."
docker build . --tag ${IMAGE_NAME} --file ${DOCKERFILE} --ssh git_ssh_key=${SSH_KEY}
# Login to the docker registry
echo "Logging into the docker registry..."
aws ecr get-login-password --region ${AWS_REGION} | docker login --username ${DOCKER_USERNAME} --password-stdin ${REGISTRY}
# Tag the image
docker tag ${IMAGE_NAME} ${REGISTRY}/${REPO}:latest
# Push the image to the registry
echo "Pushing image to registry..."
docker push ${REGISTRY}/${REPO}:latest