Créer une image Docker personnalisée pour votre système d’automatisation
J’utilise le service AWS CodeBuild.
Étape 1 - créer le Dockerfile
Voici un exemple simple:
FROM ubuntu:20.04
ENV DEBIAN_FRONTEND=noninteractive
WORKDIR /usr/app
RUN apt-get update && apt-get install -y --no-install-recommends \
curl \
git \
python3-dev \
python3-pip \
python-is-python3 \
zlib1g-dev \
zip \
software-properties-common \
build-essential \
&& pip3 install awscli \
&& pip3 install aws-sam-cli
# Ansible
RUN add-apt-repository --yes --update ppa:ansible/ansible \
&& apt install -y --no-install-recommends ansible
# NodeJS
RUN curl -fsSL https://deb.nodesource.com/setup_14.x | bash - \
&& apt install -y --no-install-recommends nodejs
# VueJS
RUN npm install -g @vue/cli
# CleanUp
RUN rm -rf /var/lib/apt/lists/*
Étape 2 - ajouter un Makefile
Assurez-vous de convertir les espaces en tabs ! Et de changer les valeurs des variables
AWS_ACCOUNT := 123456789012
AWS_PROFILE := default
AWS_REGION := ca-central-1
IMAGE_NAME := webux-devops
all: build push
build:
docker build -t ${IMAGE_NAME} .
push:
aws ecr get-login-password --region ${AWS_REGION} | docker login --username AWS --password-stdin ${AWS_ACCOUNT}.dkr.ecr.${AWS_REGION}.amazonaws.com
docker tag ${IMAGE_NAME}:latest ${AWS_ACCOUNT}.dkr.ecr.${AWS_REGION}.amazonaws.com/${IMAGE_NAME}:latest
docker push ${AWS_ACCOUNT}.dkr.ecr.${AWS_REGION}.amazonaws.com/${IMAGE_NAME}:latest
pull:
aws ecr get-login-password --region ${AWS_REGION} | docker login --username AWS --password-stdin ${AWS_ACCOUNT}.dkr.ecr.${AWS_REGION}.amazonaws.com
docker pull ${AWS_ACCOUNT}.dkr.ecr.${AWS_REGION}.amazonaws.com/${IMAGE_NAME}:latest
local_run:
docker run -it --rm \
-v $(PWD):/usr/app \
-v $(HOME)/.aws/credentials:/root/.aws/credentials \
-e AWS_PROFILE=${AWS_PROFILE} \
-e AWS_REGION=${AWS_REGION} \
${IMAGE_NAME} \
/bin/bash
create_ecr:
aws ecr create-repository \
--repository-name ${IMAGE_NAME} \
--image-scanning-configuration scanOnPush=true \
--region ${AWS_REGION} \
--profile ${AWS_PROFILE}
Étape 3 - lancer les commandes
- Pour build et deploy l’image docker, vous pouvez tout simplement lancer :
make
, les commandesbuild
&deploy
vont être lancées - Pour créer le répertoire sur AWS (ECR):
make create_ecr
- Pour lancer l’image localement sur votre machine, principalement utile pour tester votre build sans devoir pousser votre code sur git :
make local_run
, une fois dans le conteneur vous pouvez lancer cette commande pour vérifier que vous avez accès à votre compte AWS :aws sts get-caller-identity
Étape 4 - utiliser votre image avec CodeBuild
Sources:
- https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-codebuild-project.html
- https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-codebuild-project-environment.html
Vous devez faire comme ceci:
J’ai tout simplement pris l’exemple disponible sur la documentation de AWS.
Project:
Type: AWS::CodeBuild::Project
Properties:
Name: myProjectName
Description: A description about my project
ServiceRole: !GetAtt ServiceRole.Arn
Artifacts:
Type: no_artifacts
ServiceRole: ARN # <-- J'utilise un service role spécific pour le cross-account.
Environment:
Type: LINUX_CONTAINER
ComputeType: BUILD_GENERAL1_SMALL
Image: VOTRE_ECR_IMAGE_ARN # <-- Vous devez mettre l'arn de votre image docker ici
ImagePullCredentialsType: SERVICE_ROLE # <-- Ici vous avez deux options, selon votre configuration vous devez mettre SERVICE_ROLE ou CODEBUILD
EnvironmentVariables:
- Name: varName
Type: varType
Value: varValue
Source:
Location: codebuild-demo-test/0123ab9a371ebf0187b0fe5614fbb72c
Type: S3
TimeoutInMinutes: 10
Tags:
- Key: Key1
Value: Value1
- Key: Key2
Value: Value2
Vous devez modifier:
- Image et ImagePullCredentialsType, dans mon environnement je dois utiliser SERVICE_ROLE car le cross account a besoin d’une certaine configuration.
Conclusion
Cette approche vous permet de contrôler votre environnement et de réutiliser le tout localement.
Cet article est basé sur cette approche: https://aws.amazon.com/blogs/devops/aws-building-a-secure-cross-account-continuous-delivery-pipeline/
Je vais essayer d’ajouter des articles par rapport à ce setup.