Accélérer votre CI/CD Azure en utilisant une image Docker

Photo by Ian Taylor on Unsplash

Accélérer votre CI/CD Azure en utilisant une image Docker

Lorsque vous créez vos pipelines CI/CD en utilisant des outils externes vous perdez un temps considérable lors du téléchargement des binaires, de l'installation et la configuration avant de pouvoir continuer votre traitement.

Dans cet article, je vous propose simplement de construire une image Docker vous permettant d'accélérer votre CI/CD car tous les outils nécessaires seront embarqués et configurés en amont de vos activités de déploiements.

Les outils

Avant de vous fournir le Dockerfile associé à ma proposition voici la liste des outils installés dans l'image utilisant une base linux Ubuntu 20.04 :

  • git, curl, gzip, jq ...

  • nodejs & npm

  • python 3 & pip

  • Azure CLI & PowerShell

  • Binaire agent d'Azure DevOps

  • HashiCorp Terraform avec ces outils:

    • Terraform-docs

    • Checkov

    • Infracost

Dockerfile

Et voici le fichier dockerfile tant attendu, n'oubliez pas de fournir les trois arguments attendus :

args:
    - runtimeImage: ubuntu:20.04
      agentVersion: "2.217.2"
      tfDocsVersion: "v0.16.0"
ARG runtimeImage
FROM ${runtimeImage}
ARG agentVersion
ARG tfDocsVersion

ENV AGENT_ALLOW_RUNASROOT="0"
ENV DEBIAN_FRONTEND=noninteractive
ENV \
  APP_ROOT=/opt/app-root \
  # The $HOME is not set by default, but some applications needs this variable
  HOME=/opt/app-root \
  PATH=/opt/app-root/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

USER 0
WORKDIR ${APP_ROOT}
RUN apt-get update && \
  apt-get install -y -qq --no-install-recommends \
  apt-transport-https \
  apt-utils \
  bash \
  bc \
  build-essential \
  ca-certificates \
  curl \
  git \
  gnupg \
  gzip \
  iputils-ping \
  jq \
  lsb-release \
  nodejs \
  npm \
  python3.7 \
  python3-pip \
  software-properties-common \
  tar \
  unzip \
  wget && \
  add-apt-repository ppa:deadsnakes/ppa && \
  wget -q "https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb" && \
  wget -O- https://apt.releases.hashicorp.com/gpg | \
  gpg --dearmor | \
  tee /usr/share/keyrings/hashicorp-archive-keyring.gpg && \
  echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] \
  https://apt.releases.hashicorp.com $(lsb_release -cs) main" | \
  tee /etc/apt/sources.list.d/hashicorp.list && \
  dpkg -i packages-microsoft-prod.deb && \
  apt-get update && \
  apt-get install -y powershell \
  terraform && \
  pip3 install -U urllib3 requests setuptools checkov && \
  curl -sL https://aka.ms/InstallAzureCLIDeb | bash && \
  curl -fsSL "https://vstsagentpackage.azureedge.net/agent/${agentVersion}/vsts-agent-linux-x64-${agentVersion}.tar.gz" -O && \
  tar xvfz vsts-agent-linux-x64-${agentVersion}.tar.gz && \
  rm vsts-agent-linux-x64-${agentVersion}.tar.gz && \
  curl -Lo ./terraform-docs.tar.gz https://github.com/terraform-docs/terraform-docs/releases/download/${tfDocsVersion}/terraform-docs-${tfDocsVersion}-linux-amd64.tar.gz && \
  tar -xzf terraform-docs.tar.gz && \
  chmod +x terraform-docs && \
  mv terraform-docs /usr/local/bin/terraform-docs && \
  rm terraform-docs.tar.gz && \
  curl -fsSL https://raw.githubusercontent.com/infracost/infracost/master/scripts/install.sh | sh && \
  chmod 777 -R /opt/app-root && \
  apt-get clean && \
  rm -rf /var/lib/apt/lists/* 

COPY start.sh stop.sh .

ENTRYPOINT ["/bin/bash", "start.sh"]

Start.sh

#!/bin/bash

trap -- '' SIGINT SIGTERM

./config.sh --unattended --agent "$HOSTNAME" --url "https://dev.azure.com/aloizeau" --auth PAT --token "$TOKEN" --pool "$POOL" --work "_work" --replace --acceptTeeEula
./run.sh

exit 0

Stop.sh

!/bin/bash

echo "cleanup. removing azure pipelines agent..."
./config.sh remove --unattended --auth PAT --token "$TOKEN"

exit 0

Whitelist des URLs utilisées

Au besoin, si vous avez un réseau bridé, voici quelques urls à autoriser pour le bon fonctionnement du build:

"terraform-docs.io;api.github.com;*.azure.com;*.microsoft.com;*.core.windows.net;*.infracost.io;infracost.io;*.icanhazip.com;files.pythonhosted.org;*.bridgecrew.cloud;*.githubusercontent.com;*.hashicorp.com;pypi.org;*.vsassets.io;ipv4.icanhazip.com;*.dev.azure.com;dev.azure.com;vssps.dev.azure.com;dc.services.visualstudio.com;*.azure.com;management.core.windows.net;graph.windows.net;*.vault.azure.net;login.microsoftonline.com;*.azurewebsites.net;github.com;kubernetes-charts.storage.googleapis.com;*.azurecr.io;*.blob.core.windows.net;vstsagentpackage.azureedge.net;registry.terraform.io;releases.hashicorp.com;aka.ms;objects.githubusercontent.com;*.visualstudio.com;*.vsrm.visualstudio.com;*.vstmr.visualstudio.com;*.vssps.visualstudio.com;*.vsblob.visualstudio.com;bridgecrew.cloud;*.file.core.windows.net"

Did you find this article valuable?

Support Antoine LOIZEAU by becoming a sponsor. Any amount is appreciated!