첨부 파일 API를 사용하여 변경 요청에 첨부 파일 추가

  • 릴리스 버전: Australia
  • 업데이트 날짜 2026년 03월 12일
  • 소요 시간: 7분
  • 첨부 파일 API를 사용하여 테스트 보고서 XML을 DevOps 변경 요청에 첨부 파일로 추가합니다.

    API에 대한 자세한 내용은 다음 문서를 참조하십시오 Attachment - POST /now/attachment/file.

    첨부 파일 API URL은 다음과 같습니다.

    https://<your_instance>.service-now.com/api/now/attachment/file?table_name=change_request&table_sys_id=$(getServerChange.sys_id)&file_name=<attached_filename.xml>, 여기서
    • table_name = 'change_request'
    • table_sys_id = <chg 기록 sys_id>
    • file_name = 첨부 파일의 모든 이름

    Azure DevOps

    필수 조건

    • 웹 서비스 액세스 전용 옵션을 선택한 활성 사용자를 생성합니다. 사용자를 생성하는 방법에 대한 자세한 내용은 다음 문서를 참조하십시오 Create a user.
    • 사용자에게 역할 sn_change_write 을 할당합니다. 역할을 할당하는 방법에 대한 자세한 내용은 다음 문서를 참조하십시오 Assign a role to a user.

      사용자를 작성합니다.

    • 시스템 속성 sn_devops.enable_ado_generic_connection 을 활성화해야 합니다.
    • 프로젝트를 구성 Azure DevOps 하는 동안 변경 테이블에 업로드하려면 사용되는 일반 연결에 역할이 sn_change_write 있는 사용자가 있어야 합니다.

    파이프라인의 경우 Azure DevOps 여기에 제공된 형식을 사용하여 변경 요청에 첨부 파일을 추가합니다.

    샘플 파이프라인:
    stage: PostDeploy
        jobs:
          - job: 'pd1'
            pool: server
            steps:
               - task: ServiceNow-DevOps-Server-Get-Change@1
                 inputs:
                   connectedServiceName: 'v01-Software Quality Scans-ServiceNow DevOps Service Connection'
                   projectName: '$(system.teamProject)'
                   pipelineName: '$(build.definitionName)'
                   stageName: 'Deploy'
                   jobName: 'd1'
                   buildNumber: '$(build.buildId)'
                   branchName: '$(Build.SourceBranchName)'
                 name: getServerChange  
               - task: InvokeRESTAPI@1
                 inputs:
                   connectionType: 'connectedServiceName'
                   serviceConnection: 'v01 - new'
                   method: 'POST'
                   body: '<root><name>john2</name><age>34</age></root>'
                   urlSuffix: 'api/now/attachment/file?table_name=change_request&table_sys_id=$(getServerChange.changeSysId)&file_name=test3.xml'
                   waitForCompletion: 'false'

    Azure DevOps 파이프라인입니다.

    GitHub

    파이프라인의 경우 GitHub 여기에 제공된 형식을 사용하여 변경 요청에 첨부 파일을 추가합니다.

    샘플 파이프라인:

    GitHub 파이프라인입니다.

    Now 인스턴스의 변경 요청 양식에 있는 첨부 파일:

    변경 기록의 첨부 파일입니다.

    Jenkins

    파이프라인의 경우 Jenkins 여기에 제공된 형식을 사용하여 변경 요청에 첨부 파일을 추가합니다.

    샘플 파이프라인:
    pipeline {
        agent any
        
        stages {
            stage('POST API Call') {
                steps {
                    script {
                        def apiUrl = 'https://<your_instance>.service-now.com/api/sn_devops/v1/devops/tool/test?toolId=<devops_ToolId>&testType=Smoke'
                        def requestBody = [
                            name: "Smoke tests",
                            duration: 0.0,
                            passedTests: 1,
                            failedTests: 1,
                            skippedTests: 1,
                            blockedTests: 1,
                            totalTests: 4,
                            startTime: "2023-12-14T23:31:31z",
    	                    finishTime: "2023-12-15T23:31:31z",
                            buildNumber: env.BUILD_NUMBER,
                            stageName: "POST API Call",
                            pipelineName: "TestPipelineKL"
                        ]
                        
                        def auth = "<password>" // devops.integration.user:<password> in base64 encoded.
                        def authHeader = "Basic " + auth
                        
                        def response = httpRequest(
                            contentType: 'APPLICATION_JSON',
                            httpMode: 'POST',
                            requestBody: groovy.json.JsonOutput.toJson(requestBody),
                            url: apiUrl,
                            customHeaders: [[name: 'Authorization', value: authHeader]]
                        )
                        
                        if (response.status == 200 || response.status == 201) {
                            echo "API call successful!"
                            echo "Response: ${response.content}"
                        } else {
                            error "API call failed with status ${response.status}"
                        }
                    }
                }
            }
            stage('UAT deploy') {
                        steps {
                            script {
                                def chg_output = snDevOpsChange()
                                def changeRequestNumber = snDevOpsGetChangeNumber(changeDetails: """{"build_number":"${env.BUILD_NUMBER}", "pipeline_name": "TestPipelineKL", "stage_name": "UAT deploy"}""")
                                echo "${changeRequestNumber}"
                                echo "Output of snDevOpsChange(): ${chg_output}"
                        }
                    }
            }
            stage('Example') {
                steps {
                    script {
                        def API_URL = 'https://<your_instance>.service-now.com/api/sn_devops/devops/orchestration/changeInfo?pipelineName=TestPipelineKL&toolId=<devops_ToolId>&buildNumber=' + env.BUILD_NUMBER + '&stageName=UAT%20deploy'
                        def AUTH = 'Basic <password>'
                        // Call the API
                        def response = sh(script: "curl -X GET -H 'Authorization: ${AUTH}' -H 'Content-Type: application/json' '${API_URL}'", returnStdout: true).trim()
                        echo "Response: ${response}"
                        
                        // Store the JSON response into a variable
                        def slurper = new groovy.json.JsonSlurper()
                        def jsonResponse = slurper.parseText(response)
                        echo "Stored JSON Response: ${jsonResponse}"
                        
                        // Store the specific field value from JSON response into a variable
                        def changeId = jsonResponse.result.sys_id
                        echo "Change ID: ${changeId}"
                        
                        // Call the second API to post attachment
                        def xmlData = '<root><name>john2</name><age>34</age></root>'
                        def AUTH2 = 'Basic <password>='
                        def postResponse = sh(script: "curl -X POST -H 'Authorization: ${AUTH2}' -H 'Content-Type: application/xml' -d '${xmlData}' 'https://<your_instance>.service-now.com/api/now/attachment/file?table_name=change_request&table_sys_id=${changeId}&file_name=test10111.xml'", returnStdout: true).trim()
                    }
                }
            }
        }
    }
    

    Jenkins 파이프라인입니다.

    첨부 파일 API의 제한 사항

    • 첨부 파일 xml 파일의 텍스트 크기는 오케스트레이션 도구에서 허용할 수 있는 크기여야 합니다.
    • Azure DevOps의 경우 sn_change_write 파일을 변경 기록에 업로드하려면 첨부 파일 API를 인증하는 데 역할이 필요합니다.