如何将自定义Groovy添加到步骤或conditionalSteps?

时间:2018-03-13 10:16:50

标签: groovy jenkins-job-dsl jenkins-groovy

使用job-dsl我可以使用自定义bash代码执行Shell脚本,并使处理结果影响Jenkins作业的状态(失败,成功等):

import UIKit
import MapKit

class ViewController: UIViewController {

    @IBOutlet weak var mapView: MKMapView!
    var moscow: [Camera] = []
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        let coordinates = [CLLocationCoordinate2D(latitude:  40.741895, longitude:  -73.989308),CLLocationCoordinate2D(latitude:  38.80495938042258, longitude: -77.0273450631467),CLLocationCoordinate2D(latitude:  40.14404110473566, longitude:  -3.487181320896525),CLLocationCoordinate2D(latitude:  41.3425986835203, longitude:  2.093873366603475),CLLocationCoordinate2D(latitude:  -15.70239476628682, longitude:  27.486982547083358)]

        for (index,coordinate) in coordinates.enumerated() {
            let annotation = MKPointAnnotation()
            annotation.coordinate = coordinate
            annotation.title = "coordinate\(index)"
            self.mapView.addAnnotation(annotation)
        }

        self.mapView.delegate = self

        let overlay = MKPolygon(coordinates: self.getCordinatesOfRectAnnotations(annotationsArray: self.mapView.annotations), count: self.getCordinatesOfRectAnnotations(annotationsArray: self.mapView.annotations).count)
        self.mapView.add(overlay)
    }

    func getCordinatesOfRectAnnotations(annotationsArray:[MKAnnotation]) ->[CLLocationCoordinate2D]{
       let minX : Double = annotationsArray.map({MKMapPointForCoordinate($0.coordinate).x}).min()!
       let minY : Double = annotationsArray.map({MKMapPointForCoordinate($0.coordinate).y}).min()!
       let maxX : Double = annotationsArray.map({MKMapPointForCoordinate($0.coordinate).x}).max()!
       let maxY : Double = annotationsArray.map({MKMapPointForCoordinate($0.coordinate).y}).max()!

       var result : [CLLocationCoordinate2D] = []
       result.append(MKCoordinateForMapPoint(MKMapPoint(x: minX, y: minY)))
       result.append(MKCoordinateForMapPoint(MKMapPoint(x: maxX, y: minY)))
       result.append(MKCoordinateForMapPoint(MKMapPoint(x: maxX, y: maxY)))
       result.append(MKCoordinateForMapPoint(MKMapPoint(x: minX, y: maxY)))
       return result
    }

}

extension ViewController : MKMapViewDelegate
{
    func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
        let renderer = MKPolygonRenderer(overlay: overlay)
        renderer.lineWidth = 1
        renderer.strokeColor = UIColor.red
        return renderer
    }

    // Called when the annotation was added
    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        if annotation is MKUserLocation {
            return nil
        }

        let reuseId = "pin"
        var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) as? MKPinAnnotationView
        if pinView == nil {
            pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
            pinView?.animatesDrop = true
            pinView?.canShowCallout = true
            pinView?.isDraggable = true
            pinView?.pinColor = .purple

            let rightButton: AnyObject! = UIButton(type: UIButtonType.detailDisclosure)
            pinView?.rightCalloutAccessoryView = rightButton as? UIView
        } else {
            pinView?.annotation = annotation
        }

        return pinView
    }

}

如果我想使用自定义Groovy进行冒烟测试而不是依赖shell脚本,该怎么办?我尝试编写一个简单的Groovy函数来返回0或-1来表示成功和失败,但它不起作用。如何在一个步骤中执行任意Groovy代码?

2 个答案:

答案 0 :(得分:1)

使用groovyCommandgroovyScriptFile步骤执行任何Groovy代码作为构建步骤。这些步骤由Groovy Plugin提供。有关详细信息,请参阅插件文档。

有关DSL语法的详细信息,请参阅Job DSL API查看器:

示例:

job('example') {
  steps {
    groovyCommand('println "Hello"') {
      groovyInstallation('groovy-2.4.2')
    }
  }
}

请注意,Groovy脚本将在构建代理上运行。所以它需要Java和Groovy安装。

如果您想在Jenkins master上运行脚本(不推荐,它是瓶颈),请使用systemGroovyCommandsystemGroovyScriptFile步骤。

答案 1 :(得分:-1)

@NonCPS
def myFunction(x) {
    if(x)return true
    return false
}

node{
    if(myFunction('')){
        echo 'OK'
    }else{
        error 'error message'
    }
}