Skip to content
This repository has been archived by the owner on Oct 28, 2024. It is now read-only.

Helper step to get the current build time #1663

Merged
merged 3 commits into from
Apr 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/test/groovy/GetCurrentBuildTimeStepTests.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

import org.junit.Before
import org.junit.Test
import static org.junit.Assert.assertTrue

class GetCurrentBuildTimeStepTests extends ApmBasePipelineTest {

class BuildMock {
def getTimestampString2(){ return 'date' }
}

class ClassMock {
BuildMock getRawBuild(){ return new BuildMock() }
}

@Override
@Before
void setUp() throws Exception {
super.setUp()
script = loadScript('vars/getCurrentBuildTime.groovy')
binding.setProperty('currentBuild', new ClassMock())
}

@Test
void test() throws Exception {
def ret = script.call()
printCallStack()
assertTrue(ret.trim() ? true : false)
}

}
10 changes: 10 additions & 0 deletions vars/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,15 @@ Then put all togeder in a simple JSON file.
* buildNumber: the build id. Mandatory
* returnData: whether to return a data structure with the build details then other steps can consume them. Optional. Default false

## getCurrentBuildTime
Returns the timestamp formatted in xs:dateTime.

See https://javadoc.jenkins-ci.org/hudson/model/Run.html#getTimestampString2--

```
getCurrentBuildTime()
```

## getFlakyJobName
Get the flaky job name in a given multibranch pipeline.

Expand Down Expand Up @@ -1171,6 +1180,7 @@ Pull Request details.

* *labels*: Filter by labels. Optional
* *title*: Filter by title (contains format). Mandatory
* *state*: Filter by state {open|closed|merged|all} (default "open"). Optional

NOTE: It uses `githubPullRequests`

Expand Down
32 changes: 32 additions & 0 deletions vars/getCurrentBuildTime.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

import com.cloudbees.groovy.cps.NonCPS

/**
Returns the timestamp formatted in xs:dateTime.

See https://javadoc.jenkins-ci.org/hudson/model/Run.html#getTimestampString2--
*/
def call(String branch){
return getTimestampString2()
}

@NonCPS
def getTimestampString2() {
return currentBuild.getRawBuild().getTimestampString2()
}
7 changes: 7 additions & 0 deletions vars/getCurrentBuildTime.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Returns the timestamp formatted in xs:dateTime.

See https://javadoc.jenkins-ci.org/hudson/model/Run.html#getTimestampString2--

```
getCurrentBuildTime()
```