Thank you for reading this post, don't forget to subscribe!
есть dockerfile
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 |
FROM node:16.8-alpine3.11 as build-deps ARG VUE_APP_BASE_API_URL ARG VUE_APP_KRATOS_AUTH_URL ARG VUE_APP_SENTRY_DSN ARG VUE_APP_BASE_ADMIN_URL ARG VUE_APP_ENVIRONMENT ARG VUE_APP_BASE_URL ARG VUE_APP_METABASE_URL ARG VUE_APP_S3_BUCKET_URL ARG VUE_APP_ADD_TO_SLACK_URL WORKDIR /usr/src/app COPY package.json yarn.lock ./ RUN yarn install RUN echo -ne "VUE_APP_BASE_API_URL=$VUE_APP_BASE_API_URL\n" >> .env \ && echo -ne "VUE_APP_KRATOS_AUTH_URL=$VUE_APP_KRATOS_AUTH_URL\n" >> .env \ && echo -ne "VUE_APP_SENTRY_DSN=$VUE_APP_SENTRY_DSN\n" >> .env \ && echo -ne "VUE_APP_BASE_ADMIN_URL=$VUE_APP_BASE_ADMIN_URL\n" >> .env \ && echo -ne "VUE_APP_ENVIRONMENT=$VUE_APP_ENVIRONMENT\n" >> .env \ && echo -ne "VUE_APP_BASE_URL=$VUE_APP_BASE_URL\n" >> .env \ && echo -ne "VUE_APP_METABASE_URL=$VUE_APP_METABASE_URL\n" >> .env \ && echo -ne "VUE_APP_S3_BUCKET_URL=$VUE_APP_S3_BUCKET_URL\n" >> .env \ && echo -ne "VUE_APP_ADD_TO_SLACK_URL=$VUE_APP_ADD_TO_SLACK_URL\n" >> .env COPY . ./ RUN yarn build FROM nginx:1.21.3 COPY --from=build-deps /usr/src/app/dist/spa /usr/share/nginx/html COPY nginx-app.conf /etc/nginx/conf.d/default.conf EXPOSE 80 CMD ["nginx", "-g", "daemon off;"] |
есть CI вот в таком виде:
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
variables: REGISTRY_URL: 11111.dkr.ecr.eu-west-1.amazonaws.com/test-common-euw1 REGISTRY_NAME: personal-dashboard DOCKER_IMAGE: ${REGISTRY_URL}/${REGISTRY_NAME} DOCKER_TAG: $CI_COMMIT_BRANCH-$CI_COMMIT_SHORT_SHA DOCKER_HOST: tcp://localhost:2376 DOCKER_TLS_CERTDIR: "/certs" DOCKER_TLS_VERIFY: 1 DOCKER_CERT_PATH: "$DOCKER_TLS_CERTDIR/client" DOCKER_PUBLIC_MIRROR: public.ecr.aws/u9z6h3i6 stages: - build - deploy - test lint: stage: test image: node:16.8-alpine3.11 script: - yarn install - yarn lint only: refs: - merge_requests cache: key: app-requirements paths: - node_modules/ tags: - ci-k8s coverage: stage: test image: node:16.8-alpine3.11 script: - yarn test:unit:coverage only: refs: - merge_requests cache: key: app-requirements paths: - node_modules/ tags: - ci-k8s needs: - lint build_docker_image: stage: build image: docker:stable services: - $DOCKER_PUBLIC_MIRROR/docker:20.10-dind before_script: - apk add aws-cli - aws ecr get-login-password --region eu-west-1 | docker login --username AWS --password-stdin ${REGISTRY_URL} script: - > case "$CI_COMMIT_REF_NAME" in develop) export VUE_APP_BASE_API_URL_PREFIX=${VUE_APP_BASE_API_URL_DEV} export VUE_APP_KRATOS_AUTH_URL_PREFIX=${VUE_APP_KRATOS_AUTH_URL_DEV} export VUE_APP_BASE_ADMIN_URL_PREFIX=${VUE_APP_BASE_ADMIN_URL_DEV} export VUE_APP_ENVIRONMENT_PREFIX=${VUE_APP_ENVIRONMENT_DEV} export VUE_APP_BASE_URL_PREFIX=${VUE_APP_BASE_URL_DEV} export VUE_APP_METABASE_URL_PREFIX=${VUE_APP_METABASE_URL_DEV} export VUE_APP_S3_BUCKET_URL_PREFIX=${VUE_APP_S3_BUCKET_URL_DEV} export VUE_APP_ADD_TO_SLACK_URL_PREFIX=${VUE_APP_ADD_TO_SLACK_URL_DEV} ;; staging) export VUE_APP_BASE_API_URL_PREFIX=${VUE_APP_BASE_API_URL_STAGING} export VUE_APP_KRATOS_AUTH_URL_PREFIX=${VUE_APP_KRATOS_AUTH_URL_STAGING} export VUE_APP_BASE_ADMIN_URL_PREFIX=${VUE_APP_BASE_ADMIN_URL_STAGING} export VUE_APP_ENVIRONMENT_PREFIX=${VUE_APP_ENVIRONMENT_STAGING} export VUE_APP_BASE_URL_PREFIX=${VUE_APP_BASE_URL_STAGING} export VUE_APP_METABASE_URL_PREFIX=${VUE_APP_METABASE_URL_STAGING} export VUE_APP_S3_BUCKET_URL_PREFIX=${VUE_APP_S3_BUCKET_URL_STAGING} export VUE_APP_ADD_TO_SLACK_URL_PREFIX=${VUE_APP_ADD_TO_SLACK_URL_STAGING} ;; master) export VUE_APP_BASE_API_URL_PREFIX=${VUE_APP_BASE_API_URL_PROD} export VUE_APP_KRATOS_AUTH_URL_PREFIX=${VUE_APP_KRATOS_AUTH_URL_PROD} export VUE_APP_BASE_ADMIN_URL_PREFIX=${VUE_APP_BASE_ADMIN_URL_PROD} export VUE_APP_ENVIRONMENT_PREFIX=${VUE_APP_ENVIRONMENT_PROD} export VUE_APP_BASE_URL_PREFIX=${VUE_APP_BASE_URL_PROD} export VUE_APP_METABASE_URL_PREFIX=${VUE_APP_METABASE_URL_PROD} export VUE_APP_S3_BUCKET_URL_PREFIX=${VUE_APP_S3_BUCKET_URL_PROD} export VUE_APP_ADD_TO_SLACK_URL_PREFIX=${VUE_APP_ADD_TO_SLACK_URL_PROD} ;; esac - docker build -f Dockerfile --build-arg VUE_APP_BASE_API_URL=${VUE_APP_BASE_API_URL_PREFIX} --build-arg VUE_APP_KRATOS_AUTH_URL=${VUE_APP_KRATOS_AUTH_URL_PREFIX} --build-arg VUE_APP_SENTRY_DSN=${VUE_APP_SENTRY_DSN} --build-arg VUE_APP_ENVIRONMENT=${VUE_APP_ENVIRONMENT_PREFIX} --build-arg VUE_APP_BASE_URL=${VUE_APP_BASE_URL_PREFIX} --build-arg VUE_APP_BASE_ADMIN_URL=${VUE_APP_BASE_ADMIN_URL_PREFIX} --build-arg VUE_APP_METABASE_URL=${VUE_APP_METABASE_URL_PREFIX} --build-arg VUE_APP_S3_BUCKET_URL=${VUE_APP_S3_BUCKET_URL_PREFIX} --build-arg VUE_APP_ADD_TO_SLACK_URL=${VUE_APP_ADD_TO_SLACK_URL_PREFIX} -t ${DOCKER_IMAGE}:${DOCKER_TAG} . - docker push ${DOCKER_IMAGE}:${DOCKER_TAG} only: - develop - staging - master tags: - ci-k8s deploy: stage: deploy image: quay.io/roboll/helmfile:helm3-v0.135.0 script: - > case "$CI_COMMIT_REF_NAME" in develop) export ENVIRONMENT_NAME=dev ;; staging) export ENVIRONMENT_NAME=staging ;; master) export ENVIRONMENT_NAME=prod ;; esac - helmfile -f charts/helmfile.d sync only: - develop - staging - master tags: - ci-k8s |
чтобы для каждого окружения не писать свои export можно загнать в цикл
+ в этой CI добавлено использование cache для первого этапа билда
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
variables: REGISTRY_URL: 1111.dkr.ecr.eu-west-1.amazonaws.com/test-common-euw1 REGISTRY_NAME: personal-dashboard DOCKER_IMAGE: ${REGISTRY_URL}/${REGISTRY_NAME} DOCKER_TAG: $CI_COMMIT_BRANCH-$CI_COMMIT_SHORT_SHA DOCKER_HOST: tcp://localhost:2376 DOCKER_TLS_CERTDIR: "/certs" DOCKER_TLS_VERIFY: 1 DOCKER_CERT_PATH: "$DOCKER_TLS_CERTDIR/client" DOCKER_PUBLIC_MIRROR: public.ecr.aws/u9z6h3i6 CACHE_IMAGE_TAG: $DOCKER_IMAGE:$CI_COMMIT_BRANCH stages: - build - deploy - test lint: stage: test image: node:16.8-alpine3.11 script: - yarn install - yarn lint only: refs: - merge_requests cache: key: app-requirements paths: - node_modules/ tags: - ci-k8s coverage: stage: test image: node:16.8-alpine3.11 script: - yarn test:unit:coverage only: refs: - merge_requests cache: key: app-requirements paths: - node_modules/ tags: - ci-k8s needs: - lint build_docker_image: stage: build image: docker:stable services: - $DOCKER_PUBLIC_MIRROR/docker:20.10-dind before_script: - apk add aws-cli - aws ecr get-login-password --region eu-west-1 | docker login --username AWS --password-stdin ${REGISTRY_URL} script: - > case "$CI_COMMIT_REF_NAME" in develop) for i in `env | grep VUE_APP | grep DEV | awk -F "=" '{print $1}' | awk -F "_DEV" '{print $1}'`; do export "$i"_PREFIX"=\$$i"_DEV""; done ;; staging) for i in `env | grep VUE_APP | grep STAGING | awk -F "=" '{print $1}' | awk -F "_STAGING" '{print $1}'`; do export "$i"_PREFIX"=\$$i"_STAGING""; done ;; master) for i in `env | grep VUE_APP | grep PROD | awk -F "=" '{print $1}' | awk -F "_PROD" '{print $1}'`; do export "$i"_PREFIX"=\$$i"_PROD""; done ;; esac - docker pull ${CACHE_IMAGE_TAG} || true # create cache image for first stage - echo "docker build -f Dockerfile --cache-from \${CACHE_IMAGE_TAG} \\" >> build_cache_first_stage - for i in `cat Dockerfile | grep 'ARG VUE_APP' | awk '{print $2}'`; do echo "--build-arg $i=\${$i"_PREFIX}" \\"; done >> build_cache_first_stage - echo "--tag \${CACHE_IMAGE_TAG} --target build-deps ." >> build_cache_first_stage - cat build_cache_first_stage - sh build_cache_first_stage # create base image - echo "docker build -f Dockerfile --cache-from \${CACHE_IMAGE_TAG} \\" >> build_base_image - for i in `cat Dockerfile | grep 'ARG VUE_APP' | awk '{print $2}'`; do echo "--build-arg $i=\${$i"_PREFIX}" \\"; done >> build_base_image - echo "-t \${DOCKER_IMAGE}:\${DOCKER_TAG} ." >> build_base_image - cat build_base_image - sh build_base_image - docker push ${DOCKER_IMAGE}:${DOCKER_TAG} - docker push ${CACHE_IMAGE_TAG} - docker images only: - develop - staging - master tags: - ci-k8s deploy: stage: deploy image: quay.io/roboll/helmfile:helm3-v0.135.0 script: - > case "$CI_COMMIT_REF_NAME" in develop) export ENVIRONMENT_NAME=dev ;; staging) export ENVIRONMENT_NAME=staging ;; master) export ENVIRONMENT_NAME=prod ;; esac - helmfile -f charts/helmfile.d sync only: - develop - staging - master tags: - ci-k8s |