要件
HerokuプロジェクトのCiCdが、Devopsでの実現
HerokuのPipelineはgithubaだけにサポートしています。AzureのDevOpsの実現方法は検討する
分析
1 サンプルNodejsのプロジェクトを作成、Herokuへ配布する
2 DevopsへプロジェクトのReposへ登録する
3 DevopsのReposのプロジェクトのブランチからHerokuへ配布する
4 PullRequestを作成、承認してから配布する
資料
- heroku cli プロジェクトを作成
heroku create sample-prj
https://devcenter.heroku.com/ja/articles/creating-apps
1 サンプルNodejsのプロジェクト
任意のフォルダー、例heoku-t のシテに
npm init
{
"name": "simple-js",
"version": "1.0.0",
"description": "simple db nodejs",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
,"start": "node app"
},
"author": "",
"license": "ISC"
}
app.js
const express = require('express')
const app = express();
const port = process.env.PORT;
const { Client } = require('pg')
const client = new Client({
connectionString: process.env.DATABASE_URL,
ssl: { rejectUnauthorized: false }
})
client.connect();
app.get('/', (req, res) => {
client.query('INSERT into visits (created_at) values(NOW())', (err, response) => {
console.log('err', err)
console.log('response', response)
if (err) {
return res.send(`An expcetion was found: ${err}`);
}
return response
});
return res.send('Successfully recorded the visit');
});
app.listen(port, () => { console.log('Server Started') });
DB設定
sample:
Database d828l720ljbvi4
user hpgiofgzqehkem
port 5432
password xxxxx
uri postgres://hpgiofgzqehkem:a0609392a8f5b3c388c33dd46bc41f9701ee35b4fb8491a12c27aa0a6e091387@ec2-107-23-76-12.compute-1.amazonaws.com:5432/d828l720ljbvi4
heroku cli heroku pg:psql postgresql-perpendicular-27522 --app simpl-js
DBへの連携は上記以外はDATABASE_URLを設定してから、できます。
set DATABASE_URL=xxxxxxx
heroku pg:psql -app simpl-js
DBに表を作成
\l - list database
\c dbxxxx -use dbxxxx database
\dn -list schema
\dt -list table
\d tbl1 -list table definition
simpl-js::DATABASE=> \c d828l720ljbvi4
psql (14.0, server 14.4 (Ubuntu 14.4-1.pgdg20.04+1))
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256, compression: off)
You are now connected to database "d828l720ljbvi4" as user "hpgiofgzqehkem".
simpl-js::DATABASE=> \dn
List of schemas
Name | Owner
------------+----------------
heroku_ext | u53c3pl6l28v44
public | hpgiofgzqehkem
(2 rows)
simpl-js::DATABASE=> create table visits( id bigserial, created_at timestamp without time zone);
CREATE TABLE
simpl-js::DATABASE=> \dt
List of relations
Schema | Name | Type | Owner
--------+--------+-------+----------------
public | visits | table | hpgiofgzqehkem
(1 row)
simpl-js::DATABASE=> \d visits
Table "public.visits"
Column | Type | Collation | Nullable | Default
------------+-----------------------------+-----------+----------+------------------------------------
id | bigint | | not null | nextval('visits_id_seq'::regclass)
created_at | timestamp without time zone | | |
プロジェクト
プロジェクトのフォルダーに
git init
npm install pg
‐‐postgresqlのモジュールをインストール
npm install express
-- http server module - express
heroku apps:create simple-js
-- herokuにsimple-jsというプロジェクトを作成
確認
git remote -v -- herokuのgit とつながている
node-modulesを無視するため
touch .gitignore
node-modulesを追加
リリース
git push heroku master
-- master branch へDeploy
ブランチが違う場合、dev:master
ログ確認
heroku logs
Dbで確認
simpl-js::DATABASE=> select * from visits;
id | created_at
----+----------------------------
1 | 2022-09-02 01:03:42.740947
2.DevOpsへ登録
ロカールのURL登録する。別名Simple
git remote add simple https://richard2003jp@dev.azure.com/richard2003jp/secom-sfdc/_git/simple-js
これから、Dev01ブランチを作成し、DevOpsへPushする
$ git checkout -b dev01
-b 現在のブランチからdev01というブランチを作成する
$ git push -u simple dev01:dev01
-u simpleのリモートリポジトリにdev01というブランチを新規
Local -> herokuのDeploy例
・ソース修正
git add .
git commit -m "test local 2"
git push heroku dev01:master
上記はロカールdev01はherokuのmasterへPushし、自動的にDeployされる
3.DevOps -> herokuのPipelineでDeployする
参照:https://aaronluna.dev/blog/continuously-deploy-heroku-azure-devops/
PipleLineを新規
heroku authorizations:create --description="azure cd token" --short
・確認
heroku authorizations
・Tokenを表示
heroku authorizations:info 上記のID
で、tokenが表示される
git checkout $(Build.SourceBranchName)
git remote add heroku https://heroku:$(tokenid)@git.heroku.com/simpl-js.git
git push heroku $(Build.SourceBranchName):master
Pipelineでデプロイ確認
※個人のパイプライン機能は申請が要ります。使いたいプロジェクトをリストする
ソース修正
DevOpsのdev01へPush
git add .
git commit -m "test pipeline"
git push simple dev01:dev01
4.プルリクエスト
開発手法として、メインブランチ決めて、テーマやFixより別々のサブブランチでソース修正、テスト完了時点に、メインブランチにマージし、デプロイするになります。メインブランチへのマージは、チームの場合、申請承認になりますので、これはプルリクエストです。
ブランチ管理の手法はいろな資料があります。
https://qiita.com/trsn_si/items/cfecbf7dff20c64628ea
http://keijinsonyaban.blogspot.com/2010/10/a-successful-git-branching-model.html
下記にhotfix(bug),featurexxxは小さいのテーマブランチで、develop,main,releaseはメインブランチです。
-
PullRequestのレビューテンプレート
便利のため、PRを作成するとき、Descriptionにレビューポイントのテンプレートが作成できる
プロジェクトのデフォルトブランチのした下記ようなbranch_name.mdをおけば、使用できる
-
PullRequestの例
新ブランチを作成する
git checkout -b feature1001
AzureへPushする
git add .
git commit -m "test for pr"
git push -u simple feature1001
「create a pull request」をおし、Reviewerを指定して、作成する
Revewerが「Complete]を押し、Branchがマージされ、Herokuへ配布する