ServiceNow のカスタムアクション GitLab
GitLab 用の ServiceNow カスタムアクションを使用して、コンテナーイメージを使用して、GitLabパイプラインでの変更要求の作成、Sonar スキャン、アーティファクト登録、およびパッケージ登録に関連するデータを収集しDocker。
Docker イメージは、アプリケーションの実行に使用できるソフトウェアパッケージです。このイメージは、コンテナーをインスタンス化する方法と、どのソフトウェア コンポーネントを実行する必要があるかを示します。Docker コンテナーは、アプリケーションコードと、アプリケーションの実行に必要なすべての依存関係を組み合わせた仮想環境です。Docker の詳細については、 Docker のドキュメントを参照してください。
- SNOW_TOKEN: ServiceNow トークン番号。これはマスクされた変数であり、ジョブには表示されません。
- SNOW_TOOLID: ServiceNow ツール ID。
- SNOW_URL: ServiceNow インスタンスの URL。
SNOW_TOKEN変数を追加するときに、ツール [sn_devops_tool] テーブルにあるツール レコードの [ トークンをコピー ] ボタンをクリックしてトークンを取得できます。
環境変数は、ツールレコードページの [Webhook を手動で構成 (Configure Webhooks manually)] ポップアップで使用可能な値で更新する必要があります。接続の詳細を表示する方法の詳細については、「 ツールレコードから Webhook を構成 と GitLab での Webhook の手動構成」を参照してください。
ServiceNow Docker イメージ
docker build -t servicenowdocker/sndevops:4.0.0docker push servicenowdocker/sndevops:4.0.0また、app-devops-gitlab パブリックリポジトリをカスタマイズし、独自の Docker イメージを作成してパイプラインに渡すこともできます。
ServiceNow DevOps 変更ステップ
stages:
- DevOpsChangeApproval
ServiceNow DevOps Change:
stage: DevOpsChangeApproval
image: servicenowdocker/sndevops:4.0.0
script:
- sndevopscli create change -p "{\"changeStepDetails\":{\"timeout\":3600,\"interval\":100},\"attributes\":{\"short_description\":\"Software Deployment\",\"description\":\"Software Deployment.\",\"assignment_group\":\"xxxxxxxxx\",\"implementation_plan\":\"Software update is tested and results can be found in Test Summaries Tab.\",\"backout_plan\":\"When software fails in production, the previous software release will be re-deployed.\",\"test_plan\":\"Testing if the software was successfully deployed or not\"}}"
このカスタムアクションの入力値は、JSON 形式の文字列として入力する必要があります。
- changeStepDetails: [オプション] タイムアウトと間隔の詳細を保持します。
- interval: [オプション] API の試行間隔を秒単位で指定します。デフォルト値は 100 秒です。
- timeout: [オプション] アクションが失敗するまで待機する最大時間 (秒)。デフォルト値は 3600 秒です。注:GitLab の Timeout runner でもタイムアウト値が使用可能な場合は、早い方のタイムアウト値が考慮されます。
- attributes: [オプション] 変更要求属性の詳細は、 ServiceNow インスタンスで変更を作成するときに使用されます。変更要求は、カンマ (,) で区切られたキーと値のペアを含む中括弧 {} で囲まれた JSON オブジェクトです。キーと値のペアは、コロン (:)で区切られたキーと値で構成されます。キーと値のペアでサポートされるキーは、short_description、説明、assignment_group、implementation_plan、backout_plan、test_plan などです。
ServiceNow DevOps SonarScan の結果
stages:
- DevOpsSonarStage
ServiceNow DevOps SonarScan Results:
stage: DevOpsSonarStage
image: servicenowdocker/sndevops:4.0.0
script:
- sndevopscli create sonar -url 'https://sonarcloud.io' -projectKey 'devsystem2019_TestImportTest'
- url: [必須] Sonar URL を指定します。
- projectKey: [必須] Sonar プロジェクトキーを指定します。
- 制限事項
- ソフトウェア品質サマリー結果の作成中、CI_MERGE_REQUEST_SOURCE_BRANCH_NAME環境変数は結合パイプラインに使用され、CI_COMMIT_BRANCH変数は結合パイプラインまたはタグパイプラインでは使用できないため、CI_DEFAULT_BRANCH 環境変数はタグパイプラインに使用されます。詳細については、 GitLab のドキュメントを参照してください。
アーティファクトを登録
stages:
- artifact
artifact:
stage: artifact
image: servicenowdocker/sndevops:4.0.0
script:
- sndevopscli create artifact -a "[{\"name\":\"Artifact-${CI_JOB_NAME}\",\"repositoryName\":\"Repo\",\"version\":\"1.${CI_JOB_ID}.0\"}]"
このカスタムアクションの入力値は、JSON 形式の文字列として入力する必要があります。
-a: [必須] アーティファクトの詳細を指定します。
パッケージの登録
stages:
- package
package:
stage: package
image: servicenowdocker/sndevops:4.0.0
script:
- sndevopscli create package -n "Package2" -a "[{\"name\":\"Artifact-${CI_JOB_NAME}\",\"repositoryName\":\"Repo\" ,\"version\":\"1.${CI_JOB_ID}.0\"}]"このカスタムアクションの入力値は、JSON 形式の文字列として入力する必要があります。
-n: [必須] パッケージ名を指定します。
変更を取得
ジョブレベルで追加し ServiceNow 指定された changeDetails を持つインスタンスから changeRequestNumber を取得して、変更要求を識別します。
stages:
- DevOpsGetChange
ServiceNow DevOps Get Change:
stage: DevOpsGetChange
image: servicenowdocker/sndevops:4.0.0
script:
- sndevopscli get change -p "{\"buildNumber\":${CHG_JOB_ID},\"stageName\":\"ServiceNow DevOps Change Step\",\"pipelineName\":\"GitlabDockerGetAndUpdateChange\"}"
-p: It stands for changeDetails. The change details to be used for identifying change request in ServiceNow instance. The change details is a JSON object surrounded by curly braces {} containing key-value pair separated by a comma ,. A key-value pair consists of a key and a value separated by a colon :. The keys supported in key-value pair are buildNumber, pipelineName, stageName
buildNumber: [mandatory]
This specifies ID of the Job where we have created change request.
stageName: [mandatory]
This specifies the Job name where we have created change request..
pipelineName: [mandatory]
This specifies the pipeline name.
Outputs:
sndevopschg.json file created with content: {
"status": "SUCCESS",
"changeRequestNumber": "CHGXXXXX"
}
changeRequestNumber: Change Request Number found for the given change details
status: To know the status of the Change Request GET.
変更の更新
ジョブレベルで追加して ServiceNow changeRequestDetails とともに入力として指定された changeRequestNumber のインスタンスの変更を更新します。
stages:
- DevOpsUpdateChangeStage
ServiceNow DevOps Update Change:
stage: DevOpsUpdateChangeStage
image: servicenowdocker/sndevops:4.0.0
script:
- sndevopscli update change -n 'CHGXXXXXX' -p "{\"short_description\":\"G Venkata12345 Automated Software Deployment\",\"description\":\"Automated Software Deployment.\",\"assignment_group\":\"XXXXX\",\"implementation_plan\":\"Software update is tested and results can be found in Test Summaries Tab.\",\"backout_plan\":\"When software fails in production, the previous software release will be re-deployed.\",\"test_plan\":\"Testing if the software was successfully deployed or not\"}"
-n [Not mandatory if we have sndevopschg.json in our pipeline yml]: It stands for changeRequestNumber. The change request number to identify a unique change request.
Precedence of choosing changeRequestNumber:
- changeRequestNumber mentioned in the pipeline yml
- changeRequestNumber stored in sndevopschg.json.
-p : It stands for changeDetails. The change details to be used for Updating the change request information identified by the specified change request number with the key-value pairs. The change details is a JSON object surrounded by curly braces {} containing key-value pair separated by a comma ,. A key-value pair consists of a key and a value separated by a colon :. The keys supported in key-value pair are short_description, state, description, work_notes ..so on
OR
- sndevopscli update change -p "{\"short_description\":\"Updated Automated Software Deployment\",\"description\":\"Automated Software Deployment.\",\"assignment_group\":\"XXXXXXXXXX\",\"implementation_plan\":\"Software update is tested and results can be found in Test Summaries Tab.\",\"backout_plan\":\"When software fails in production, the previous software release will be re-deployed.\",\"test_plan\":\"Testing if the software was successfully deployed or not\"}"
NOTE: State should be specified at last in case if you are update the state of change request.
- sndevopscli update change -p "{\"short_description\":\"Updated Automated Software Deployment\",\"description\":\"Automated Software Deployment.\",\"assignment_group\":\"XXXXXXXXXX\",\"implementation_plan\":\"Software update is tested and results can be found in Test Summaries Tab.\",\"backout_plan\":\"When software fails in production, the previous software release will be re-deployed.\",\"test_plan\":\"Testing if the software was successfully deployed or not\","state":"3"}'
自動変更クローズ
ServiceNowインスタンスに自動クローズ変更を組み込みます。自動終了の詳細については、「 パイプライン内で DevOps 変更要求の詳細を構成する」を参照してください。
stages:
- changeapproval
ServiceNow DevOps Change Step:
stage: changeapproval
image: servicenowdocker/sndevops:4.0.0
script:
- sndevopscli create change -p "{\"changeStepDetails\":{\"timeout\":3600,\"interval\":100},\"autoCloseChange\":true,\"attributes\":{\"short_description\":\"G Venkata Automated Software Deployment\",\"description\":\"Automated Software Deployment.\",\"assignment_group\":\"xxxxxxxx\",\"implementation_plan\":\"Software update is tested and results can be found in Test Summaries Tab.\",\"backout_plan\":\"When software fails in production, the previous software release will be re-deployed.\",\"test_plan\":\"Testing if the software was successfully deployed or not\"}}"
autoCloseChange: [optional] : Boolean value