AWS Full CICD 3 – 准备好buildspec.yml 

buildspec.yml 文件

AWS_DEFAULT_REGION 和 CODEBUILD_BUILD_NUMBER 是系统的默认环境变量,其余的都是自己Set的

AWS_DEFAULT_REGION 需要在 account 设定 default region


需要创建好ECR Image Repo然后把ECR Repo URL存入environment variable当中

version: 0.2

phases:
  pre_build:
    commands:
      - echo Logging in to Amazon ECR
      - aws --version
      - aws ecr get-login-password --region ${AWS_DEFAULT_REGION} | docker login --username AWS --password-stdin 3213232.dkr.ecr.${AWS_DEFAULT_REGION}.amazonaws.com
      - export IMAGE_TAG=${ENV_TYPE}_${CODEBUILD_BUILD_NUMBER}

  build:
    commands:
      - echo Build started on `date`
      - echo Building the Docker image...
      - >-
        docker build .
        --tag ${IMAGE_REPO_URL}:${IMAGE_TAG}
        --file dockerfile
        --build-arg DB_HOST=${DB_HOST}
        --build-arg DB_DATABASE=${DB_DATABASE}
        --build-arg DB_USERNAME=${DB_USERNAME}
        --build-arg DB_PASSWORD=${DB_PASSWORD}
        --build-arg ENV_TYPE=${ENV_TYPE}

  post_build:
    commands:
      - echo Build completed on `date`
      - echo Pushing the Docker images...
      - docker push ${IMAGE_REPO_URL}:${IMAGE_TAG}

      - echo Logging in to Amazon EKS...
      - aws eks --region ${AWS_DEFAULT_REGION} update-kubeconfig --name YourClusterName
      - kubectl set image deployment/testbackend-deployment testbackend-container=${IMAGE_REPO_URL}:${IMAGE_TAG} -n YourNamespace

以下是部署Laravel应用的dockerfile

FROM php:7.3.33-apache
WORKDIR /var/www/html

ARG DB_HOST
ARG DB_DATABASE
ARG DB_USERNAME
ARG DB_PASSWORD
ARG ENV_TYPE

ENV DB_HOST ${DB_HOST}
ENV DB_DATABASE ${DB_DATABASE}
ENV DB_USERNAME ${DB_USERNAME}
ENV DB_PASSWORD ${DB_PASSWORD}
ENV ENV_TYPE ${ENV_TYPE}

COPY deployment/${ENV_TYPE}_httpd/opcache.ini /usr/local/etc/php/conf.d/opcache.ini
COPY deployment/${ENV_TYPE}_httpd/php.ini /usr/local/etc/php/

COPY ./ ./
COPY ./deployment/${ENV_TYPE}_httpd/httpd.conf /etc/apache2/sites-enabled/
RUN rm -rf /var/www/html/vendor
COPY ./deployment/vendor /var/www/html/vendor/


RUN echo "\
APP_NAME=Laravel\n\
APP_ENV=production\n\
APP_KEY=base64:3232232=\n\
APP_DEBUG=true\n\
ADMIN_HTTPS=true\n\
APP_URL=https://www.xxxx.com/\n\
\
LOG_CHANNEL=stack\n\
\
DB_CONNECTION=mysql\n\
DB_HOST="${DB_HOST}"\n\
DB_PORT=3306\n\
DB_DATABASE="${DB_DATABASE}"\n\
DB_USERNAME="${DB_USERNAME}"\n\
DB_PASSWORD="${DB_PASSWORD}"\n\
DB_PREFIX=tbl_\n\
\
BROADCAST_DRIVER=log\n\
CACHE_DRIVER=file\n\
QUEUE_CONNECTION=sync\n\
SESSION_DRIVER=database\n\
SESSION_LIFETIME=120\n\
\
REDIS_CLIENT=predis\n\
REDIS_URL=\n\
REDIS_HOST=\n\
REDIS_PORT=6379\n\
REDIS_PASSWORD=\n\
REDIS_DB=5\n\
\
MAIL_DRIVER=smtp\n\
MAIL_HOST=smtp.xxx.com\n\
MAIL_PORT=465\n\
[email protected]\n\
MAIL_PASSWORD=emailPassword\n\
MAIL_ENCRYPTION=ssl\n\
[email protected]\n\
[email protected]\n\
\
AWS_ACCESS_KEY_ID=\n\
AWS_SECRET_ACCESS_KEY=\n\
AWS_DEFAULT_REGION=us-east-1\n\
AWS_BUCKET=\n\
\
PUSHER_APP_ID=\n\
PUSHER_APP_KEY=\n\
PUSHER_APP_SECRET=\n\
PUSHER_APP_CLUSTER=mt1\n\
" > .env

RUN apt-get update && apt-get install -y \
    git \
    zip \
    curl \
    sudo \
    unzip \
    libgd-dev \
    libicu-dev \
    libbz2-dev \
    libpng-dev \
    libjpeg-dev \
    libmcrypt-dev \
    libreadline-dev \
    libfreetype6-dev \
    g++ \
    libssl-dev \
    zlib1g-dev \
    libonig-dev \
    libxml2-dev \
    libicu-dev \
    zip \
    libzip-dev

# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

RUN docker-php-ext-install mysqli pdo pdo_mysql bz2 intl iconv bcmath opcache calendar gd zip mbstring

RUN apt-get update && apt-get install -y \
    libfreetype6-dev \
    libjpeg62-turbo-dev \
    libpng-dev \
    && docker-php-ext-configure gd --with-freetype-dir=/usr --with-jpeg-dir=/usr --with-png-dir=/usr \
    && docker-php-ext-install -j$(nproc) gd \
    && pecl install -o -f redis \
    &&  rm -rf /tmp/pear \
    &&  docker-php-ext-enable redis


RUN chmod o+w /var/www/html/storage -R && \
    chown www-data:www-data -R /var/www/html/storage && \
    chmod -R 775 /var/www/html/bootstrap && \
    chmod 664 /var/www/html/.env && \
    php artisan package:discover && \
	rm -rf /etc/apache2/sites-available/000-default.conf && \
	rm -rf /etc/apache2/sites-available/default-ssl.conf && \
	rm -rf /etc/apache2/sites-enabled/000-default.conf && \
	 a2enmod headers && \
    sed -ri -e 's/^([ \t]*)(<\/VirtualHost>)/\1\tHeader set Access-Control-Allow-Origin "*"\n\1\2/g' /etc/apache2/sites-enabled/*.conf && \
    a2enmod rewrite  && \
	echo "ServerName localhost" >> /etc/apache2/apache2.conf

EXPOSE 80
CMD ["apache2-foreground"]

AWS Full CICD 1 – 创建CodeCommit Project
AWS Full CICD 2 – 为CodeBuild Service Role添加权限
AWS Full CICD 3 – 准备好buildspec.yml

Loading

Facebook评论