From dd81951ca10afef8c2a282c2b0688b2675555455 Mon Sep 17 00:00:00 2001 From: Faisal Hanif Date: Mon, 5 Apr 2021 10:57:10 +0700 Subject: [PATCH] [CHORES] Build and deploy docker image via container registry --- .gitlab-ci.yml | 96 ++++++++++++++++++++++++++-------------------- Dockerfile | 12 +++--- docker-compose.yml | 20 ++++++++++ nginx.conf | 11 ++++++ 4 files changed, 91 insertions(+), 48 deletions(-) create mode 100644 docker-compose.yml create mode 100644 nginx.conf diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index dd8444e..48990d8 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -6,9 +6,30 @@ cache: stages: - test - sonar-scanner - - build + - publish - deploy +.cd-switch-environment: &cd-switch-environment + # select environment based on branch name + # requires bash + - > + if [[ $CI_COMMIT_REF_NAME =~ ^[Pp][Bb][Ii]-[0-9]+-.*$ ]]; then + ENVIRONMENT=development + REACT_APP_API_MAIN_URL=$REACT_APP_API_DEVELOPMENT_URL + elif [[ $CI_COMMIT_REF_NAME == staging ]]; then + ENVIRONMENT=staging + REACT_APP_API_MAIN_URL=$REACT_APP_API_STAGING_URL + fi + +.cd-job-template: + image: tbcare/docker:git-bash + only: + refs: + - /^[Pp][Bb][Ii]-[0-9]+-.*$/ + - staging + before_script: + - *cd-switch-environment + test: image: nezappl/web:stage stage: test @@ -20,56 +41,49 @@ test: - coverage sonar-scanner: - image: addianto/sonar-scanner-cli + image: + name: sonarsource/sonar-scanner-cli + entrypoint: [""] stage: sonar-scanner script: - sonar-scanner -Dsonar.host.url=$SONARQUBE_URL - -Dsonar.projectKey=$SONARQUBE_PROJECT_KEY -Dsonar.login=$SONARQUBE_TOKEN -Dsonar.branch.name=$CI_COMMIT_REF_NAME + -Dsonar.projectKey=$SONARQUBE_PROJECT_KEY + allow_failure: true -build: - image: nezappl/web:stage - stage: build - script: - - npm install - - > - if [[ "${CI_COMMIT_REF_NAME}" == "staging" ]]; - then - REACT_APP_API_MAIN_URL=$STAGING_API_MAIN_URL - elif [[ "${CI_COMMIT_REF_NAME}" == "master" ]]; - then - REACT_APP_API_MAIN_URL=$STAGING_API_MAIN_URL - fi +publish: + extends: .cd-job-template + services: + - docker:20-dind + stage: publish + before_script: + - *cd-switch-environment + - IMAGE=tbcare/web:$ENVIRONMENT - echo -e "REACT_APP_API_MAIN_URL=${REACT_APP_API_MAIN_URL}\n" >> .env - - cat .env - - npm run build - artifacts: - paths: - - build - only: - - staging - - master + - echo $CI_REGISTRY_PASSWORD | docker login -u $CI_REGISTRY_USER --password-stdin + script: + - docker build --tag $IMAGE . + - docker push $IMAGE deploy: - image: node:slim + extends: .cd-job-template stage: deploy + before_script: + - *cd-switch-environment + - eval $(ssh-agent -s) + - ssh-add <(echo "$SSH_PRIVATE_KEY" | base64 -d) + - mkdir -p ~/.ssh + - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config' script: - - npm i -g netlify-cli - > - if [[ "${CI_COMMIT_REF_NAME}" == "staging" ]]; - then - NETLIFY_SITE_ID=$STAGING_NETLIFY_SITE_ID - NETLIFY_AUTH_TOKEN=$STAGING_NETLIFY_AUTH_TOKEN - elif [[ "${CI_COMMIT_REF_NAME}" == "master" ]]; - then - NETLIFY_SITE_ID=$MASTER_NETLIFY_SITE_ID - NETLIFY_AUTH_TOKEN=$MASTER_NETLIFY_AUTH_TOKEN - fi - - netlify deploy --site $NETLIFY_SITE_ID --auth $NETLIFY_AUTH_TOKEN --prod - dependencies: - - build - only: - - staging - - master + ssh $TARGET_USER_AT_HOST " + cd admin/$ENVIRONMENT && + git fetch origin $CI_COMMIT_REF_NAME && + git checkout $CI_COMMIT_REF_NAME && + git merge --ff && + docker-compose pull && + docker-compose up -d && + exit + " diff --git a/Dockerfile b/Dockerfile index 451112b..9b9975f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,5 @@ -FROM node:slim - -WORKDIR /web -COPY package.json /web - -RUN npm install -g -RUN npm install -g netlify-cli +FROM node:14.16.0-alpine3.13 +WORKDIR /srv +COPY . . +RUN npm ci +RUN npm run build diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..af40d50 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,20 @@ +version: '2.4' + +services: + nginx: + image: nginx:1.19-alpine + ports: + - '${PORT:-8000}:80' + volumes: + - ./nginx.conf:/etc/nginx/conf.d/default.conf + - build:/var/www/html/ + depends_on: + - app + + app: + image: tbcare/web:${ENVIRONMENT:-staging} + volumes: + - build:/srv/build + +volumes: + build: diff --git a/nginx.conf b/nginx.conf new file mode 100644 index 0000000..b28a7c7 --- /dev/null +++ b/nginx.conf @@ -0,0 +1,11 @@ +server { + listen 80; + + location / { + root /var/www/html; + index index.html index.htm; + try_files $uri $uri/ /index.html =404; + } + + client_max_body_size 2M; +} -- GitLab