Skip to content

Instantly share code, notes, and snippets.

@riskers riskers/.dockerignore
Last active Mar 11, 2019

Embed
What would you like to do?
eggjs Dockerfile + gitlab ci
app/**/*.js
test/**/*.js
config/**/*.js
app/**/*.map
test/**/*.map
config/**/*.map
cache:
paths:
- node_modules/
stages:
- install_deps
- test
- docker
install_deps:
stage: install_deps
only:
- /^publish\//
script:
- yarn
test:
stage: test
only:
- /^publish\//
script:
- npm run test
docker:
stage: docker
only:
- /^publish\//
script:
- docker build -t fe/homesite:$CI_COMMIT_REF_SLUG .
- docker tag fe/homesite:$CI_COMMIT_REF_SLUG registry.i.entropage.com/fe/homesite:$CI_COMMIT_REF_SLUG
- docker push registry.i.entropage.com/fe/homesite:$CI_COMMIT_REF_SLUG
FROM node:10.14.2-alpine
LABEL author="riskers"
WORKDIR /app
# node install
COPY package.json /app/package.json
COPY yarn.lock /e-image-bed/yarn.lock
RUN yarn
# copy file
COPY app /app/app
COPY config /app/config
COPY typings /app/typings
COPY test /app/test
# typescript build to javascript
COPY tsconfig.json /app/tsconfig.json
RUN npm run tsc
EXPOSE 9000
CMD [ "npm", "start" ]
@riskers

This comment has been minimized.

Copy link
Owner Author

commented Jan 28, 2019

贴合项目,因为要控制 CDN 资源的缓存,所以需要在 prod 环境下统一资源的版本号:

  1. extend/helper.ts 中添加函数:
export const getStatic = (resource: string) => {
  if ((process.env as any).ENV === 'dev') {
    return resource;
  }
  const ci = (process.env as any).CI_VERSION;
  return resource + '?t=' + ci;
};

在模板 (app/view/index.tpl) 中:

<link rel="stylesheet" href="{{helper.getStatic('/public/css/style.css')}}" />
  1. package.json 中添加 ENV=dev 以确定开发环境:
"dev": "ENV=dev egg-bin dev -r egg-ts-helper/register --port=9000",
  1. .gitlab-ci.yml 中 build 一步中通过 build-arg 添加 CI_VERSION:
docker build --build-arg CI_VERSION=$CI_COMMIT_REF_SLUG -t fe/homesite:$CI_COMMIT_REF_SLUG .
  1. Dockerfile 中添加 ARGENV,以便将 CI_VERSION 打入容器系统变量中:
# ARG: 将 build-arg 传入的 CI_VERSION 赋为 CI_VERSION
ARG CI_VERSION

# 确定线上环境变量
ENV ENV=prod

# 确定版本号
ENV CI_VERSION $CI_VERSION 

# 略

# 传入环境变量启动容器
ENTRYPOINT ENV=prod CI_VERSION=${CI_VERSION} npm start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.