├── .gitignore ├── Dockerfile ├── README.md ├── deploy-to-kubernetes.yaml ├── environment ├── config-dev.yaml ├── config-prod.yaml ├── dev.properties └── prod.properties ├── helm-chart ├── .helmignore ├── Chart.yaml ├── config-values │ ├── config-dev.yaml │ └── config-prod.yaml ├── templates │ ├── NOTES.txt │ ├── _helpers.tpl │ ├── configMap.yaml │ ├── deployment.yaml │ ├── service.yaml │ └── tests │ │ └── test-connection.yaml └── values.yaml ├── kubernetes ├── deployment.yaml └── service.yaml ├── kustomize ├── base │ ├── deployment.yaml │ ├── kustomization.yaml │ └── service.yaml └── overlays │ ├── dev │ ├── dev.properties │ └── kustomization.yaml │ ├── local │ ├── kustomization.yaml │ └── local.properties │ └── prod │ ├── deployment-prod.yaml │ ├── kustomization.yaml │ └── prod.properties ├── package-lock.json ├── package.json ├── public ├── config.js ├── favicon.ico ├── index.html ├── logo.png ├── manifest.json └── robots.txt ├── skaffold.yaml └── src ├── App.css ├── App.js ├── App.test.js ├── index.css ├── index.js ├── logo.svg └── serviceWorker.js /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # production 12 | /build 13 | 14 | # misc 15 | .DS_Store 16 | .env.local 17 | .env.development.local 18 | .env.test.local 19 | .env.production.local 20 | 21 | npm-debug.log* 22 | yarn-debug.log* 23 | yarn-error.log* 24 | /.idea/ 25 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | # build environment 2 | FROM node:12.2.0-alpine as build 3 | WORKDIR /app 4 | 5 | ENV PATH /app/node_modules/.bin:$PATH 6 | COPY package.json /app/package.json 7 | RUN npm install --silent 8 | RUN npm config set unsafe-perm true #https://stackoverflow.com/questions/52196518/could-not-get-uid-gid-when-building-node-docker 9 | RUN npm install react-scripts@3.0.1 -g --silent 10 | COPY . /app 11 | RUN npm run build 12 | 13 | # production environment 14 | FROM nginx:1.16.0-alpine 15 | COPY --from=build /app/build /usr/share/nginx/html 16 | EXPOSE 80 17 | CMD ["nginx", "-g", "daemon off;"] 18 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). 2 | 3 | ## Available Scripts 4 | 5 | In the project directory, you can run: 6 | 7 | ### `npm start` 8 | 9 | Runs the app in the development mode.
10 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser. 11 | 12 | The page will reload if you make edits.
13 | You will also see any lint errors in the console. 14 | 15 | ### `npm test` 16 | 17 | Launches the test runner in the interactive watch mode.
18 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. 19 | 20 | ### `npm run build` 21 | 22 | Builds the app for production to the `build` folder.
23 | It correctly bundles React in production mode and optimizes the build for the best performance. 24 | 25 | The build is minified and the filenames include the hashes.
26 | Your app is ready to be deployed! 27 | 28 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. 29 | 30 | 31 | # Deploy on Kubernetes 32 | 33 | ## Normal objects 34 | 35 | ### Build docker image 36 | ``` 37 | docker build --tag multi-stage-react-app-example:latest . 38 | ``` 39 | 40 | #### Deploy with native `kubectl` commands 41 | ``` 42 | kubectl apply -f kubernetes/config-dev.yaml # creates the configMap in the Kubernetes cluster 43 | kubectl apply -f kubernetes/deployment.yaml 44 | kubectl apply -f kubernetes/service.yaml 45 | ``` 46 | 47 | ** Undo deployments ** 48 | 49 | ``` 50 | kubectl delete configMap multi-stage-react-app-example-config 51 | kubectl delete deployment multi-stage-react-app-example 52 | kubectl delete service multi-stage-react-app-example 53 | ``` 54 | 55 | Forward port 56 | 57 | ``` 58 | kubectl port-forward svc/multi-stage-react-app-example 3001:80 59 | ``` 60 | 61 | Then access the app at [http://localhost:3001](http://localhost:3001) 62 | 63 | ## Deploy with [helm](https://helm.sh/) 64 | 65 | ### Create release 66 | ``` 67 | helm upgrade dev-release ./helm-chart/ --install --force --values helm-chart/config-values/config-dev.yaml 68 | helm ls #verify dev-release is present 69 | ``` 70 | 71 | 72 | ### Undo release 73 | 74 | ``` 75 | helm delete --purge dev-release 76 | ``` 77 | 78 | ## Deploy with [kustomize](https://kustomize.io/) 79 | 80 | Build with kustomize to see what Kubernetes objects are generated 81 | ``` 82 | kustomize build kustomize/base/ 83 | ``` 84 | 85 | Apply base 86 | ``` 87 | kubectl apply -k kustomize/base 88 | ``` 89 | 90 | Undo 91 | ``` 92 | kubectl delete -k kustomize/base 93 | ``` 94 | 95 | Apply DEV overlay 96 | ``` 97 | kubectl apply -k kustomize/overlays/dev 98 | ``` 99 | 100 | Undo 101 | ``` 102 | kubectl delete -k kustomize/overlays/dev 103 | ``` 104 | 105 | ## Cherry on the cake - use [skaffold](https://skaffold.dev/) 106 | 107 | > We will use [skaffold profiles](https://skaffold.dev/docs/how-tos/profiles/) 108 | 109 | ### Deploy via kubectl 110 | ``` 111 | skaffold run -p native-kubernetes 112 | ``` 113 | 114 | ``` 115 | skaffold delete -p native-kubernetes 116 | ``` 117 | 118 | 119 | ### Deploy via kustomize 120 | 121 | For example build the prod thing: 122 | ``` 123 | skaffold run -p kustomize-prod 124 | ``` 125 | 126 | ``` 127 | skaffold delete -p kustomize-prod 128 | ``` 129 | -------------------------------------------------------------------------------- /deploy-to-kubernetes.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: Service 3 | metadata: 4 | labels: 5 | app.kubernetes.io/component: service 6 | name: multi-stage-react-app-example 7 | spec: 8 | ports: 9 | - name: http 10 | port: 80 11 | protocol: TCP 12 | targetPort: 80 13 | selector: 14 | app: multi-stage-react-app-example 15 | type: NodePort 16 | --- 17 | apiVersion: apps/v1 18 | kind: Deployment 19 | metadata: 20 | labels: 21 | app.kubernetes.io/component: service 22 | name: multi-stage-react-app-example 23 | spec: 24 | replicas: 1 25 | selector: 26 | matchLabels: 27 | app: multi-stage-react-app-example 28 | template: 29 | metadata: 30 | labels: 31 | app.kubernetes.io/component: service 32 | app: multi-stage-react-app-example 33 | spec: 34 | containers: 35 | - name: multi-stage-react-app-example 36 | image: multi-stage-react-app-example:latest 37 | imagePullPolicy: IfNotPresent 38 | ports: 39 | - containerPort: 80 -------------------------------------------------------------------------------- /environment/config-dev.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: ConfigMap 3 | metadata: 4 | name: multi-stage-react-app-example-config 5 | data: 6 | config.js: | 7 | window.REACT_APP_API_URL='https://www.bookmarks.dev/api/public/bookmarks' 8 | window.REACT_APP_ENVIRONMENT='DEV' 9 | window.REACT_APP_NAVBAR_COLOR='LightGreen' 10 | -------------------------------------------------------------------------------- /environment/config-prod.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: ConfigMap 3 | metadata: 4 | name: multi-stage-react-app-example-config 5 | data: 6 | config.js: | 7 | window.REACT_APP_API_URL='https://www.bookmarks.dev/api/public/bookmarks' 8 | window.REACT_APP_ENVIRONMENT='PROD' 9 | window.REACT_APP_NAVBAR_COLOR='LightPink' 10 | -------------------------------------------------------------------------------- /environment/dev.properties: -------------------------------------------------------------------------------- 1 | window.REACT_APP_API_URL='https://www.bookmarks.dev/api/public/bookmarks' 2 | window.REACT_APP_ENVIRONMENT='DEV' 3 | window.REACT_APP_NAVBAR_COLOR='LightGreen' 4 | -------------------------------------------------------------------------------- /environment/prod.properties: -------------------------------------------------------------------------------- 1 | window.REACT_APP_API_URL='https://www.bookmarks.dev/api/public/bookmarks' 2 | window.REACT_APP_ENVIRONMENT='PROD' 3 | window.REACT_APP_NAVBAR_COLOR='LightPink' 4 | -------------------------------------------------------------------------------- /helm-chart/.helmignore: -------------------------------------------------------------------------------- 1 | # Patterns to ignore when building packages. 2 | # This supports shell glob matching, relative path matching, and 3 | # negation (prefixed with !). Only one pattern per line. 4 | .DS_Store 5 | # Common VCS dirs 6 | .git/ 7 | .gitignore 8 | .bzr/ 9 | .bzrignore 10 | .hg/ 11 | .hgignore 12 | .svn/ 13 | # Common backup files 14 | *.swp 15 | *.bak 16 | *.tmp 17 | *~ 18 | # Various IDEs 19 | .project 20 | .idea/ 21 | *.tmproj 22 | .vscode/ 23 | -------------------------------------------------------------------------------- /helm-chart/Chart.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | appVersion: "1.0" 3 | description: A Helm chart for Kubernetes 4 | name: helm-chart 5 | version: 0.1.0 6 | -------------------------------------------------------------------------------- /helm-chart/config-values/config-dev.yaml: -------------------------------------------------------------------------------- 1 | configValues: | 2 | window.REACT_APP_API_URL='https://www.bookmarks.dev/api/public/bookmarks' 3 | window.REACT_APP_ENVIRONMENT='DEV' 4 | window.REACT_APP_NAVBAR_COLOR='LightGreen' 5 | 6 | -------------------------------------------------------------------------------- /helm-chart/config-values/config-prod.yaml: -------------------------------------------------------------------------------- 1 | configValues: | 2 | window.REACT_APP_API_URL='https://www.bookmarks.dev/api/public/bookmarks' 3 | window.REACT_APP_ENVIRONMENT='PROD' 4 | window.REACT_APP_NAVBAR_COLOR='LightPink' 5 | 6 | -------------------------------------------------------------------------------- /helm-chart/templates/NOTES.txt: -------------------------------------------------------------------------------- 1 | 1. Get the application URL by running these commands: 2 | {{- if .Values.ingress.enabled }} 3 | {{- range $host := .Values.ingress.hosts }} 4 | {{- range $.Values.ingress.paths }} 5 | http{{ if $.Values.ingress.tls }}s{{ end }}://{{ $host }}{{ . }} 6 | {{- end }} 7 | {{- end }} 8 | {{- else if contains "NodePort" .Values.service.type }} 9 | export NODE_PORT=$(kubectl get --namespace {{ .Release.Namespace }} -o jsonpath="{.spec.ports[0].nodePort}" services {{ include "helm-chart.fullname" . }}) 10 | export NODE_IP=$(kubectl get nodes --namespace {{ .Release.Namespace }} -o jsonpath="{.items[0].status.addresses[0].address}") 11 | echo http://$NODE_IP:$NODE_PORT 12 | {{- else if contains "LoadBalancer" .Values.service.type }} 13 | NOTE: It may take a few minutes for the LoadBalancer IP to be available. 14 | You can watch the status of by running 'kubectl get svc -w {{ include "helm-chart.fullname" . }}' 15 | export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ include "helm-chart.fullname" . }} -o jsonpath='{.status.loadBalancer.ingress[0].ip}') 16 | echo http://$SERVICE_IP:{{ .Values.service.port }} 17 | {{- else if contains "ClusterIP" .Values.service.type }} 18 | export POD_NAME=$(kubectl get pods --namespace {{ .Release.Namespace }} -l "app.kubernetes.io/name={{ include "helm-chart.name" . }},app.kubernetes.io/instance={{ .Release.Name }}" -o jsonpath="{.items[0].metadata.name}") 19 | echo "Visit http://127.0.0.1:8080 to use your application" 20 | kubectl port-forward $POD_NAME 8080:80 21 | {{- end }} 22 | -------------------------------------------------------------------------------- /helm-chart/templates/_helpers.tpl: -------------------------------------------------------------------------------- 1 | {{/* vim: set filetype=mustache: */}} 2 | {{/* 3 | Expand the name of the chart. 4 | */}} 5 | {{- define "helm-chart.name" -}} 6 | {{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" -}} 7 | {{- end -}} 8 | 9 | {{/* 10 | Create a default fully qualified app name. 11 | We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec). 12 | If release name contains chart name it will be used as a full name. 13 | */}} 14 | {{- define "helm-chart.fullname" -}} 15 | {{- if .Values.fullnameOverride -}} 16 | {{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" -}} 17 | {{- else -}} 18 | {{- $name := default .Chart.Name .Values.nameOverride -}} 19 | {{- if contains $name .Release.Name -}} 20 | {{- .Release.Name | trunc 63 | trimSuffix "-" -}} 21 | {{- else -}} 22 | {{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}} 23 | {{- end -}} 24 | {{- end -}} 25 | {{- end -}} 26 | 27 | {{/* 28 | Create chart name and version as used by the chart label. 29 | */}} 30 | {{- define "helm-chart.chart" -}} 31 | {{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" -}} 32 | {{- end -}} 33 | -------------------------------------------------------------------------------- /helm-chart/templates/configMap.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: ConfigMap 3 | metadata: 4 | name: {{ .Release.Name }}-multi-stage-react-app-example-config 5 | annotations: 6 | # https://github.com/helm/helm/blob/master/docs/charts_hooks.md 7 | "helm.sh/hook-delete-policy": "before-hook-creation" 8 | "helm.sh/hook": pre-install, pre-upgrade 9 | data: 10 | config.js: {{ toYaml .Values.configValues | indent 4 }} 11 | -------------------------------------------------------------------------------- /helm-chart/templates/deployment.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: apps/v1 2 | kind: Deployment 3 | metadata: 4 | name: {{ include "helm-chart.fullname" . }} 5 | labels: 6 | app.kubernetes.io/name: {{ include "helm-chart.name" . }} 7 | helm.sh/chart: {{ include "helm-chart.chart" . }} 8 | app.kubernetes.io/instance: {{ .Release.Name }} 9 | app.kubernetes.io/managed-by: {{ .Release.Service }} 10 | spec: 11 | replicas: {{ .Values.replicaCount }} 12 | selector: 13 | matchLabels: 14 | app.kubernetes.io/name: {{ include "helm-chart.name" . }} 15 | app.kubernetes.io/instance: {{ .Release.Name }} 16 | template: 17 | metadata: 18 | labels: 19 | app.kubernetes.io/name: {{ include "helm-chart.name" . }} 20 | app.kubernetes.io/instance: {{ .Release.Name }} 21 | spec: 22 | imagePullSecrets: 23 | - name: {{ .Values.image.imagePullSecrets }} 24 | containers: 25 | - name: {{ .Chart.Name }} 26 | image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}" 27 | imagePullPolicy: {{ .Values.image.pullPolicy }} 28 | ports: 29 | - name: http 30 | containerPort: 80 31 | protocol: TCP 32 | livenessProbe: 33 | httpGet: 34 | path: / 35 | port: http 36 | readinessProbe: 37 | httpGet: 38 | path: / 39 | port: http 40 | volumeMounts: 41 | - name: multi-stage-react-app-example-config-volume 42 | mountPath: /usr/share/nginx/html/config.js 43 | subPath: config.js 44 | readOnly: true 45 | resources: 46 | {{- toYaml .Values.resources | nindent 12 }} 47 | volumes: 48 | - name: multi-stage-react-app-example-config-volume 49 | configMap: 50 | name: {{ .Release.Name }}-multi-stage-react-app-example-config 51 | {{- with .Values.nodeSelector }} 52 | nodeSelector: 53 | {{- toYaml . | nindent 8 }} 54 | {{- end }} 55 | {{- with .Values.affinity }} 56 | affinity: 57 | {{- toYaml . | nindent 8 }} 58 | {{- end }} 59 | {{- with .Values.tolerations }} 60 | tolerations: 61 | {{- toYaml . | nindent 8 }} 62 | {{- end }} 63 | -------------------------------------------------------------------------------- /helm-chart/templates/service.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: Service 3 | metadata: 4 | name: {{ include "helm-chart.fullname" . }} 5 | labels: 6 | app.kubernetes.io/name: {{ include "helm-chart.name" . }} 7 | helm.sh/chart: {{ include "helm-chart.chart" . }} 8 | app.kubernetes.io/instance: {{ .Release.Name }} 9 | app.kubernetes.io/managed-by: {{ .Release.Service }} 10 | spec: 11 | type: {{ .Values.service.type }} 12 | ports: 13 | - port: {{ .Values.service.port }} 14 | targetPort: http 15 | protocol: TCP 16 | name: http 17 | selector: 18 | app.kubernetes.io/name: {{ include "helm-chart.name" . }} 19 | app.kubernetes.io/instance: {{ .Release.Name }} 20 | -------------------------------------------------------------------------------- /helm-chart/templates/tests/test-connection.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: Pod 3 | metadata: 4 | name: "{{ include "helm-chart.fullname" . }}-test-connection" 5 | labels: 6 | app.kubernetes.io/name: {{ include "helm-chart.name" . }} 7 | helm.sh/chart: {{ include "helm-chart.chart" . }} 8 | app.kubernetes.io/instance: {{ .Release.Name }} 9 | app.kubernetes.io/managed-by: {{ .Release.Service }} 10 | annotations: 11 | "helm.sh/hook": test-success 12 | spec: 13 | containers: 14 | - name: wget 15 | image: busybox 16 | command: ['wget'] 17 | args: ['{{ include "helm-chart.fullname" . }}:{{ .Values.service.port }}'] 18 | restartPolicy: Never 19 | -------------------------------------------------------------------------------- /helm-chart/values.yaml: -------------------------------------------------------------------------------- 1 | # Default values for helm-chart. 2 | # This is a YAML-formatted file. 3 | # Declare variables to be passed into your templates. 4 | 5 | replicaCount: 1 6 | 7 | image: 8 | repository: multi-stage-react-app-example 9 | tag: latest 10 | pullPolicy: IfNotPresent 11 | imagePullSecrets: cfcr 12 | 13 | nameOverride: "" 14 | fullnameOverride: "" 15 | 16 | service: 17 | type: NodePort 18 | port: 80 19 | 20 | ingress: 21 | enabled: false 22 | annotations: {} 23 | # kubernetes.io/ingress.class: nginx 24 | # kubernetes.io/tls-acme: "true" 25 | paths: [] 26 | hosts: 27 | - chart-example.local 28 | tls: [] 29 | # - secretName: chart-example-tls 30 | # hosts: 31 | # - chart-example.local 32 | 33 | resources: {} 34 | 35 | nodeSelector: {} 36 | 37 | tolerations: [] 38 | 39 | affinity: {} 40 | 41 | configValues: | 42 | window.REACT_APP_API_URL='https://www.bookmarks.dev/api/public/bookmarks' 43 | window.REACT_APP_ENVIRONMENT='LOCAL with helm' 44 | window.REACT_APP_NAVBAR_COLOR='LightBlue' 45 | -------------------------------------------------------------------------------- /kubernetes/deployment.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: apps/v1 2 | kind: Deployment 3 | metadata: 4 | labels: 5 | app.kubernetes.io/component: service 6 | name: multi-stage-react-app-example 7 | spec: 8 | replicas: 1 9 | selector: 10 | matchLabels: 11 | app: multi-stage-react-app-example 12 | template: 13 | metadata: 14 | labels: 15 | app.kubernetes.io/component: service 16 | app: multi-stage-react-app-example 17 | spec: 18 | containers: 19 | - name: multi-stage-react-app-example 20 | image: multi-stage-react-app-example:latest 21 | imagePullPolicy: IfNotPresent 22 | ports: 23 | - containerPort: 80 24 | volumeMounts: 25 | - name: multi-stage-react-app-example-config-volume 26 | mountPath: /usr/share/nginx/html/config.js 27 | subPath: config.js 28 | readOnly: true 29 | volumes: 30 | - name: multi-stage-react-app-example-config-volume 31 | configMap: 32 | name: multi-stage-react-app-example-config 33 | -------------------------------------------------------------------------------- /kubernetes/service.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: Service 3 | metadata: 4 | labels: 5 | app.kubernetes.io/component: service 6 | name: multi-stage-react-app-example 7 | spec: 8 | ports: 9 | - name: http 10 | port: 80 11 | protocol: TCP 12 | targetPort: 80 13 | selector: 14 | app: multi-stage-react-app-example 15 | type: NodePort 16 | -------------------------------------------------------------------------------- /kustomize/base/deployment.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: apps/v1 2 | kind: Deployment 3 | metadata: 4 | labels: 5 | app.kubernetes.io/component: service 6 | name: multi-stage-react-app-example 7 | spec: 8 | replicas: 1 9 | selector: 10 | matchLabels: 11 | app: multi-stage-react-app-example 12 | template: 13 | metadata: 14 | labels: 15 | app.kubernetes.io/component: service 16 | app: multi-stage-react-app-example 17 | spec: 18 | containers: 19 | - name: multi-stage-react-app-example 20 | image: multi-stage-react-app-example:latest 21 | imagePullPolicy: IfNotPresent 22 | ports: 23 | - containerPort: 80 24 | volumeMounts: 25 | - name: multi-stage-react-app-example-config-volume 26 | mountPath: /usr/share/nginx/html/config.js 27 | subPath: config.js 28 | readOnly: true 29 | volumes: 30 | - name: multi-stage-react-app-example-config-volume 31 | configMap: 32 | name: multi-stage-react-app-example-config 33 | -------------------------------------------------------------------------------- /kustomize/base/kustomization.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: kustomize.config.k8s.io/v1beta1 2 | kind: Kustomization 3 | resources: 4 | - deployment.yaml 5 | - service.yaml 6 | 7 | commonLabels: 8 | app.kubernetes.io/name: multi-stage-react-app-example 9 | -------------------------------------------------------------------------------- /kustomize/base/service.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: Service 3 | metadata: 4 | labels: 5 | app.kubernetes.io/component: service 6 | name: multi-stage-react-app-example 7 | spec: 8 | ports: 9 | - name: http 10 | port: 80 11 | protocol: TCP 12 | targetPort: 80 13 | selector: 14 | app: multi-stage-react-app-example 15 | type: NodePort 16 | -------------------------------------------------------------------------------- /kustomize/overlays/dev/dev.properties: -------------------------------------------------------------------------------- 1 | window.REACT_APP_API_URL='https://www.bookmarks.dev/api/public/bookmarks' 2 | window.REACT_APP_ENVIRONMENT='DEV' 3 | window.REACT_APP_NAVBAR_COLOR='LightGreen' 4 | -------------------------------------------------------------------------------- /kustomize/overlays/dev/kustomization.yaml: -------------------------------------------------------------------------------- 1 | bases: 2 | - ../../base 3 | 4 | configMapGenerator: 5 | - name: multi-stage-react-app-example-config 6 | files: 7 | - config.js=dev.properties #add here the key config.js as you would when creating with kubectl 8 | -------------------------------------------------------------------------------- /kustomize/overlays/local/kustomization.yaml: -------------------------------------------------------------------------------- 1 | bases: 2 | - ../../base 3 | 4 | configMapGenerator: 5 | - name: multi-stage-react-app-example-config 6 | files: 7 | - config.js=local.properties #add here the key config.js as you would when creating with kubectl 8 | -------------------------------------------------------------------------------- /kustomize/overlays/local/local.properties: -------------------------------------------------------------------------------- 1 | window.REACT_APP_API_URL='https://www.bookmarks.dev/api/public/bookmarks' 2 | window.REACT_APP_ENVIRONMENT='LOCAL' 3 | window.REACT_APP_NAVBAR_COLOR='LightBlue' -------------------------------------------------------------------------------- /kustomize/overlays/prod/deployment-prod.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: apps/v1 2 | kind: Deployment 3 | metadata: 4 | name: multi-stage-react-app-example 5 | spec: 6 | replicas: 2 7 | 8 | -------------------------------------------------------------------------------- /kustomize/overlays/prod/kustomization.yaml: -------------------------------------------------------------------------------- 1 | bases: 2 | - ../../base 3 | 4 | patchesStrategicMerge: 5 | - deployment-prod.yaml 6 | 7 | configMapGenerator: 8 | - name: multi-stage-react-app-example-config 9 | files: 10 | - config.js=prod.properties #add here the key config.js as you would when creating with kubectl 11 | -------------------------------------------------------------------------------- /kustomize/overlays/prod/prod.properties: -------------------------------------------------------------------------------- 1 | window.REACT_APP_API_URL='https://www.bookmarks.dev/api/public/bookmarks' 2 | window.REACT_APP_ENVIRONMENT='PROD' 3 | window.REACT_APP_NAVBAR_COLOR='LightPink' 4 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "multi-stage-react-app-example", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "axios": "^0.21.1", 7 | "react": "^16.9.0", 8 | "react-dom": "^16.9.0", 9 | "react-scripts": "3.1.1" 10 | }, 11 | "scripts": { 12 | "start": "react-scripts start", 13 | "build": "react-scripts build", 14 | "test": "react-scripts test", 15 | "eject": "react-scripts eject" 16 | }, 17 | "eslintConfig": { 18 | "extends": "react-app" 19 | }, 20 | "browserslist": { 21 | "production": [ 22 | ">0.2%", 23 | "not dead", 24 | "not op_mini all" 25 | ], 26 | "development": [ 27 | "last 1 chrome version", 28 | "last 1 firefox version", 29 | "last 1 safari version" 30 | ] 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /public/config.js: -------------------------------------------------------------------------------- 1 | window.REACT_APP_API_URL='https://www.bookmarks.dev/api/public/bookmarks' 2 | window.REACT_APP_ENVIRONMENT='LOCAL - public dir' 3 | window.REACT_APP_NAVBAR_COLOR='LightBlue' 4 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodepediaOrg/multi-stage-react-app-example/5d0099131bcf3c7394b8484ec80aeab46441d71b/public/favicon.ico -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 12 | 13 | 14 | 18 | 19 | 28 | Multi Stage React App Example 29 | 30 | 31 | 32 | 33 |
34 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /public/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodepediaOrg/multi-stage-react-app-example/5d0099131bcf3c7394b8484ec80aeab46441d71b/public/logo.png -------------------------------------------------------------------------------- /public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "React App", 3 | "name": "Create React App Sample", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | }, 10 | { 11 | "src": "logo.png", 12 | "type": "image/png", 13 | "sizes": "192x192" 14 | }, 15 | { 16 | "src": "logo.png", 17 | "type": "image/png", 18 | "sizes": "512x512" 19 | } 20 | ], 21 | "start_url": ".", 22 | "display": "standalone", 23 | "theme_color": "#000000", 24 | "background_color": "#ffffff" 25 | } 26 | -------------------------------------------------------------------------------- /public/robots.txt: -------------------------------------------------------------------------------- 1 | # https://www.robotstxt.org/robotstxt.html 2 | User-agent: * 3 | -------------------------------------------------------------------------------- /skaffold.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: skaffold/v1beta13 2 | kind: Config 3 | build: 4 | artifacts: 5 | - image: multi-stage-react-app-example 6 | docker: 7 | dockerfile: Dockerfile 8 | deploy: 9 | kustomize: 10 | path: kustomize/overlays/local 11 | portForward: 12 | - resourceType: service 13 | resourceName: multi-stage-react-app-example 14 | port: 80 15 | localPort: 3001 16 | profiles: 17 | - name: native-kubernetes 18 | deploy: 19 | kubectl: 20 | manifests: 21 | - kubernetes/* 22 | 23 | - name: kustomize-prod 24 | deploy: 25 | kustomize: 26 | path: kustomize/overlays/prod 27 | -------------------------------------------------------------------------------- /src/App.css: -------------------------------------------------------------------------------- 1 | .App { 2 | text-align: center; 3 | } 4 | 5 | .App-logo { 6 | animation: App-logo-spin infinite 20s linear; 7 | height: 40vmin; 8 | pointer-events: none; 9 | } 10 | 11 | .App-header { 12 | background-color: #282c34; 13 | min-height: 100vh; 14 | display: flex; 15 | flex-direction: column; 16 | align-items: center; 17 | justify-content: center; 18 | font-size: calc(10px + 2vmin); 19 | color: white; 20 | } 21 | 22 | .App-link { 23 | color: #61dafb; 24 | } 25 | 26 | @keyframes App-logo-spin { 27 | from { 28 | transform: rotate(0deg); 29 | } 30 | to { 31 | transform: rotate(360deg); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/App.js: -------------------------------------------------------------------------------- 1 | import React, {useState, useEffect} from 'react'; 2 | import './App.css'; 3 | import axios from 'axios'; 4 | 5 | function App() { 6 | 7 | const [data, setData] = useState([]); 8 | 9 | let navbarStyles = { 10 | backgroundColor: window.REACT_APP_NAVBAR_COLOR 11 | } 12 | 13 | useEffect(() => { 14 | const fetchData = async () => { 15 | 16 | const result = await axios( 17 | window.REACT_APP_API_URL, 18 | ); 19 | setData(result.data); 20 | }; 21 | fetchData(); 22 | }, []); 23 | 24 | 25 | return ( 26 | 27 |
28 | 35 | {data.map((bookmark) => ( 36 |
37 |
38 |
{bookmark.name}
39 |
{bookmark.location}
40 |

{bookmark.description}

41 |
42 |
43 | ))} 44 |
45 | ); 46 | } 47 | 48 | export default App; 49 | -------------------------------------------------------------------------------- /src/App.test.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import App from './App'; 4 | 5 | it('renders without crashing', () => { 6 | const div = document.createElement('div'); 7 | ReactDOM.render(, div); 8 | ReactDOM.unmountComponentAtNode(div); 9 | }); 10 | -------------------------------------------------------------------------------- /src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", 4 | "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue", 5 | sans-serif; 6 | -webkit-font-smoothing: antialiased; 7 | -moz-osx-font-smoothing: grayscale; 8 | } 9 | 10 | code { 11 | font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New", 12 | monospace; 13 | } 14 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import './index.css'; 4 | import App from './App'; 5 | import * as serviceWorker from './serviceWorker'; 6 | 7 | ReactDOM.render(, document.getElementById('root')); 8 | 9 | // If you want your app to work offline and load faster, you can change 10 | // unregister() to register() below. Note this comes with some pitfalls. 11 | // Learn more about service workers: https://bit.ly/CRA-PWA 12 | serviceWorker.unregister(); 13 | -------------------------------------------------------------------------------- /src/logo.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /src/serviceWorker.js: -------------------------------------------------------------------------------- 1 | // This optional code is used to register a service worker. 2 | // register() is not called by default. 3 | 4 | // This lets the app load faster on subsequent visits in production, and gives 5 | // it offline capabilities. However, it also means that developers (and users) 6 | // will only see deployed updates on subsequent visits to a page, after all the 7 | // existing tabs open on the page have been closed, since previously cached 8 | // resources are updated in the background. 9 | 10 | // To learn more about the benefits of this model and instructions on how to 11 | // opt-in, read https://bit.ly/CRA-PWA 12 | 13 | const isLocalhost = Boolean( 14 | window.location.hostname === 'localhost' || 15 | // [::1] is the IPv6 localhost address. 16 | window.location.hostname === '[::1]' || 17 | // 127.0.0.1/8 is considered localhost for IPv4. 18 | window.location.hostname.match( 19 | /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/ 20 | ) 21 | ); 22 | 23 | export function register(config) { 24 | if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) { 25 | // The URL constructor is available in all browsers that support SW. 26 | const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href); 27 | if (publicUrl.origin !== window.location.origin) { 28 | // Our service worker won't work if PUBLIC_URL is on a different origin 29 | // from what our page is served on. This might happen if a CDN is used to 30 | // serve assets; see https://github.com/facebook/create-react-app/issues/2374 31 | return; 32 | } 33 | 34 | window.addEventListener('load', () => { 35 | const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`; 36 | 37 | if (isLocalhost) { 38 | // This is running on localhost. Let's check if a service worker still exists or not. 39 | checkValidServiceWorker(swUrl, config); 40 | 41 | // Add some additional logging to localhost, pointing developers to the 42 | // service worker/PWA documentation. 43 | navigator.serviceWorker.ready.then(() => { 44 | console.log( 45 | 'This web app is being served cache-first by a service ' + 46 | 'worker. To learn more, visit https://bit.ly/CRA-PWA' 47 | ); 48 | }); 49 | } else { 50 | // Is not localhost. Just register service worker 51 | registerValidSW(swUrl, config); 52 | } 53 | }); 54 | } 55 | } 56 | 57 | function registerValidSW(swUrl, config) { 58 | navigator.serviceWorker 59 | .register(swUrl) 60 | .then(registration => { 61 | registration.onupdatefound = () => { 62 | const installingWorker = registration.installing; 63 | if (installingWorker == null) { 64 | return; 65 | } 66 | installingWorker.onstatechange = () => { 67 | if (installingWorker.state === 'installed') { 68 | if (navigator.serviceWorker.controller) { 69 | // At this point, the updated precached content has been fetched, 70 | // but the previous service worker will still serve the older 71 | // content until all client tabs are closed. 72 | console.log( 73 | 'New content is available and will be used when all ' + 74 | 'tabs for this page are closed. See https://bit.ly/CRA-PWA.' 75 | ); 76 | 77 | // Execute callback 78 | if (config && config.onUpdate) { 79 | config.onUpdate(registration); 80 | } 81 | } else { 82 | // At this point, everything has been precached. 83 | // It's the perfect time to display a 84 | // "Content is cached for offline use." message. 85 | console.log('Content is cached for offline use.'); 86 | 87 | // Execute callback 88 | if (config && config.onSuccess) { 89 | config.onSuccess(registration); 90 | } 91 | } 92 | } 93 | }; 94 | }; 95 | }) 96 | .catch(error => { 97 | console.error('Error during service worker registration:', error); 98 | }); 99 | } 100 | 101 | function checkValidServiceWorker(swUrl, config) { 102 | // Check if the service worker can be found. If it can't reload the page. 103 | fetch(swUrl) 104 | .then(response => { 105 | // Ensure service worker exists, and that we really are getting a JS file. 106 | const contentType = response.headers.get('content-type'); 107 | if ( 108 | response.status === 404 || 109 | (contentType != null && contentType.indexOf('javascript') === -1) 110 | ) { 111 | // No service worker found. Probably a different app. Reload the page. 112 | navigator.serviceWorker.ready.then(registration => { 113 | registration.unregister().then(() => { 114 | window.location.reload(); 115 | }); 116 | }); 117 | } else { 118 | // Service worker found. Proceed as normal. 119 | registerValidSW(swUrl, config); 120 | } 121 | }) 122 | .catch(() => { 123 | console.log( 124 | 'No internet connection found. App is running in offline mode.' 125 | ); 126 | }); 127 | } 128 | 129 | export function unregister() { 130 | if ('serviceWorker' in navigator) { 131 | navigator.serviceWorker.ready.then(registration => { 132 | registration.unregister(); 133 | }); 134 | } 135 | } 136 | --------------------------------------------------------------------------------