jenkins部署前端react项目(docker)
在docker里运行jenkins:
docker run ^
--rm ^
-u root ^
-p 8080:8080 ^
-v jenkins-data:/var/jenkins_home ^
-v /var/run/docker.sock:/var/run/docker.sock ^
-v /D/source/jenkins:/home ^
jenkinsci/blueocean
第一次安装jenkins,需要安装jenkins的要求填写admin的密码,安装推荐的插件,另外还要安装两个插件: Docker、Docker PipeLine。
将前端项目提交到github上。
.dockerignore:
node_modules
.env.jenkins:
// .env.test
NODE_ENV=production
REACT_APP_MODE=test
BUILD_PATH= build
app.js:
const express = require('express')
const { createProxyMiddleware } = require('http-proxy-middleware')
const app = express()
app.use(express.static('build'))
//通过代理解决跨域
app.use(
'/api',
createProxyMiddleware({
target: 'http://localhost:81',
changeOrigin: true,
})
)
app.listen(process.env.PORT, () => {
console.log(process.env.PORT)
})
JenkinsFile:
pipeline {
agent {
docker {
image 'node:12'
args '-p 3002:3002'
}
}
environment {
CI = 'true'
}
stages {
stage('install') {
steps {
sh 'yarn'
}
}
stage('Build') {
steps {
sh 'yarn build_jenkins'
}
}
stage('server') {
steps {
sh 'yarn server'
}
}
}
}
package.json:
{
"name": "air",
"version": "0.1.0",
"private": true,
"dependencies": {
"@babel/core": "7.12.3",
"@pmmmwh/react-refresh-webpack-plugin": "0.4.3",
"@svgr/webpack": "5.5.0",
"@testing-library/jest-dom": "^5.11.4",
"@testing-library/react": "^11.1.0",
"@testing-library/user-event": "^12.1.10",
"@typescript-eslint/eslint-plugin": "^4.5.0",
"@typescript-eslint/parser": "^4.5.0",
"antd": "^4.16.1",
"axios": "^0.21.1",
"babel-eslint": "^10.1.0",
"babel-jest": "^26.6.0",
"babel-loader": "8.1.0",
"babel-plugin-named-asset-import": "^0.3.7",
"babel-preset-react-app": "^10.0.0",
"bfj": "^7.0.2",
"camelcase": "^6.1.0",
"case-sensitive-paths-webpack-plugin": "2.3.0",
"compression-webpack-plugin": "5",
"cross-env": "^7.0.3",
"css-loader": "4.3.0",
"dotenv": "8.2.0",
"dotenv-expand": "5.1.0",
"eslint": "^7.11.0",
"eslint-config-react-app": "^6.0.0",
"eslint-plugin-flowtype": "^5.2.0",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-jest": "^24.1.0",
"eslint-plugin-jsx-a11y": "^6.3.1",
"eslint-plugin-react": "^7.21.5",
"eslint-plugin-react-hooks": "^4.2.0",
"eslint-plugin-testing-library": "^3.9.2",
"eslint-webpack-plugin": "^2.5.2",
"file-loader": "6.1.1",
"fs-extra": "^9.0.1",
"html-webpack-plugin": "4.5.0",
"identity-obj-proxy": "3.0.0",
"immutability-helper": "^3.1.1",
"immutable": "^4.0.0-rc.12",
"jest": "26.6.0",
"jest-circus": "26.6.0",
"jest-resolve": "26.6.0",
"jest-watch-typeahead": "0.6.1",
"mini-css-extract-plugin": "0.11.3",
"momment": "^0.0.1",
"optimize-css-assets-webpack-plugin": "5.0.4",
"pnp-webpack-plugin": "1.6.4",
"postcss-flexbugs-fixes": "4.2.1",
"postcss-loader": "3.0.0",
"postcss-normalize": "8.0.1",
"postcss-preset-env": "6.7.0",
"postcss-safe-parser": "5.0.2",
"prompts": "2.4.0",
"qr-code-with-logo": "^1.1.0",
"qrcode": "^1.4.4",
"react": "^17.0.2",
"react-app-polyfill": "^2.0.0",
"react-dev-utils": "^11.0.3",
"react-dnd": "^14.0.2",
"react-dnd-html5-backend": "^14.0.0",
"react-dom": "^17.0.2",
"react-redux": "^7.2.4",
"react-refresh": "^0.8.3",
"react-router-dom": "^5.2.0",
"react-smooth-dnd": "^0.11.1",
"redux": "^4.1.0",
"redux-immutable": "^4.0.0",
"redux-thunk": "^2.3.0",
"resolve": "1.18.1",
"resolve-url-loader": "^3.1.2",
"sass-loader": "^10.0.5",
"semver": "7.3.2",
"style-loader": "1.3.0",
"terser-webpack-plugin": "4.2.3",
"ts-pnp": "1.2.0",
"url-loader": "4.1.1",
"uuid": "^8.3.2",
"web-vitals": "^1.0.1",
"webpack": "4.44.2",
"webpack-bundle-analyzer": "^4.4.2",
"webpack-dev-server": "3.11.1",
"webpack-manifest-plugin": "2.2.0",
"webpackbar": "^5.0.0-3",
"workbox-webpack-plugin": "5.1.4"
},
"scripts": {
"start": "cross-env PORT=3002 node scripts/start.js",
"build": "cross-env MODE_ENV=prod GENERATE_SOURCEMAP=false node scripts/build.js",
"build_test": "cross-env MODE_ENV=test PUBLIC_URL=/air GENERATE_SOURCEMAP=false GENERATE_BUNDLE_ANALYZER_REPORT=true node scripts/build.js",
"build_jenkins": "cross-env MODE_ENV=jenkins GENERATE_SOURCEMAP=false node scripts/build.js",
"server": "PORT=3002 node app",
"test": "node scripts/test.js"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"jest": {
"roots": [
"<rootDir>/src"
],
"collectCoverageFrom": [
"src/**/*.{js,jsx,ts,tsx}",
"!src/**/*.d.ts"
],
"setupFiles": [
"react-app-polyfill/jsdom"
],
"setupFilesAfterEnv": [
"<rootDir>/src/setupTests.js"
],
"testMatch": [
"<rootDir>/src/**/__tests__/**/*.{js,jsx,ts,tsx}",
"<rootDir>/src/**/*.{spec,test}.{js,jsx,ts,tsx}"
],
"testEnvironment": "jsdom",
"testRunner": "D:\\source\\air\\node_modules\\jest-circus\\runner.js",
"transform": {
"^.+\\.(js|jsx|mjs|cjs|ts|tsx)$": "<rootDir>/config/jest/babelTransform.js",
"^.+\\.css$": "<rootDir>/config/jest/cssTransform.js",
"^(?!.*\\.(js|jsx|mjs|cjs|ts|tsx|css|json)$)": "<rootDir>/config/jest/fileTransform.js"
},
"transformIgnorePatterns": [
"[/\\\\]node_modules[/\\\\].+\\.(js|jsx|mjs|cjs|ts|tsx)$",
"^.+\\.module\\.(css|sass|scss)$"
],
"modulePaths": [],
"moduleNameMapper": {
"^react-native$": "react-native-web",
"^.+\\.module\\.(css|sass|scss)$": "identity-obj-proxy"
},
"moduleFileExtensions": [
"web.js",
"js",
"web.ts",
"ts",
"web.tsx",
"tsx",
"json",
"web.jsx",
"jsx",
"node"
],
"watchPlugins": [
"jest-watch-typeahead/filename",
"jest-watch-typeahead/testname"
],
"resetMocks": true
},
"babel": {
"presets": [
"react-app"
]
}
}
config.js:
const baseURL = {
// 使用反向代理解决跨域时,dev应为空字符串
dev: 'http://localhost:81',
//test: 'https://efficacious-tiny-infinity.glitch.me',
test: 'https://399b5.sse.codesandbox.io',
jenkins: '',
// 使用yarn build打包,所有接口使用这个url,需要服务器解决跨域
prod: 'http://39.97.238.175:81'
}[process.env.REACT_APP_MODE]
const nodeBaseURL = {
dev: 'http://localhost:81',
// test: 'https://efficacious-tiny-infinity.glitch.me',
// prod: 'https://efficacious-tiny-infinity.glitch.me',
test: 'https://399b5.sse.codesandbox.io',
jenkins: '',
prod: 'https://399b5.sse.codesandbox.io',
}[process.env.REACT_APP_MODE]
export { baseURL, nodeBaseURL }