Pipeline自動化Node.js應用程序的部署時,將Node.js應用程式(app.js)和package.json加入專案中,.gitlab-ci.yml設定檔加到專案根目錄。
app.js
const express = require('express');
const app = express();
const port = process.env.PORT || 3000;
app.get('/', (req, res) => {
res.send('Hello, World!');
});
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});
package.json
{
"name": "your-node-app",
"version": "1.0.0",
"description": "A simple Node.js app",
"main": "app.js",
"scripts": {
"start": "node app.js",
"test": "echo \"Error:no test specified\" && exit 1"
},
"dependencies": {
"express": "^4.17.1"
}
}
.gitlab-ci.yml
stages:
- build
- test
- deploy
before_script:
- apt-get update -qy # 更新套件管理系統
- apt-get install -y nodejs npm # 安裝Node.js和npm
- npm install # 在執行任何階段之前安裝套件
build:
stage: build
script:
- npm run start
test:
stage: test
script:
- npm test # 執行測試
deploy:
stage: deploy
script:
- node app.js
only:
- master # 只有當提交到master分支時才執行此
ps. 這篇寫的有點敷衍了事,等鐵人完賽後再找空閒時間重新整理°