Skip to content

Commit

Permalink
Add credential arg for pypi (#43) (#44)
Browse files Browse the repository at this point in the history
* Add credential arg for pypi

Signed-off-by: Sayali Gaikawad <[email protected]>
(cherry picked from commit 5fe49fb)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

Signed-off-by: Sayali Gaikawad <[email protected]>
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
1 parent f5d4d2c commit 8511dad
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 15 deletions.
4 changes: 2 additions & 2 deletions tests/jenkins/TestPublishToPyPi.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class TestPublishToPyPi extends BuildPipelineTest {

@Test
void testWithDefaults() {
this.registerLibTester(new PublishToPyPiLibTester())
this.registerLibTester(new PublishToPyPiLibTester('pypi-token'))
super.setUp()
super.testPipeline('tests/jenkins/jobs/PublishToPyPi_Jenkinsfile')
def twineCommands = getCommands('sh', 'twine')
Expand All @@ -36,7 +36,7 @@ class TestPublishToPyPi extends BuildPipelineTest {

@Test
void testWithCustomDir() {
this.registerLibTester(new PublishToPyPiLibTester('test'))
this.registerLibTester(new PublishToPyPiLibTester('pypi-token', 'test'))
super.setUp()
super.testPipeline('tests/jenkins/jobs/PublishToPyPiWithDir_Jenkinsfile')

Expand Down
2 changes: 1 addition & 1 deletion tests/jenkins/jobs/PublishToPyPiWithDir_Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pipeline {
stage('publishToPyPi') {
steps {
script {
publishToPyPi(artifactsPath: 'test')
publishToPyPi(credentialId: 'pypi-token', artifactsPath: 'test')
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/jenkins/jobs/PublishToPyPiWithDir_Jenkinsfile.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
PublishToPyPiWithDir_Jenkinsfile.echo(Executing on agent [label:none])
PublishToPyPiWithDir_Jenkinsfile.stage(publishToPyPi, groovy.lang.Closure)
PublishToPyPiWithDir_Jenkinsfile.script(groovy.lang.Closure)
PublishToPyPiWithDir_Jenkinsfile.publishToPyPi({artifactsPath=test})
PublishToPyPiWithDir_Jenkinsfile.publishToPyPi({credentialId=pypi-token, artifactsPath=test})
publishToPyPi.legacySCM(groovy.lang.Closure)
publishToPyPi.library({[email protected], retriever=null})
publishToPyPi.signArtifacts({artifactPath=test, sigtype=.asc, platform=linux})
Expand All @@ -27,6 +27,6 @@

/tmp/workspace/sign.sh test --sigtype=.asc --platform=linux
)
publishToPyPi.usernamePassword({credentialsId=jenkins-opensearch-pypi-credentials, usernameVariable=TWINE_USERNAME, passwordVariable=TWINE_PASSWORD})
publishToPyPi.usernamePassword({credentialsId=pypi-token, usernameVariable=TWINE_USERNAME, passwordVariable=TWINE_PASSWORD})
publishToPyPi.withCredentials([[TWINE_USERNAME, TWINE_PASSWORD]], groovy.lang.Closure)
publishToPyPi.sh(twine upload -r pypi test/*)
2 changes: 1 addition & 1 deletion tests/jenkins/jobs/PublishToPyPi_Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pipeline {
stage('publishToPyPi') {
steps {
script {
publishToPyPi()
publishToPyPi(credentialId: 'pypi-token')
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/jenkins/jobs/PublishToPyPi_Jenkinsfile.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
PublishToPyPi_Jenkinsfile.echo(Executing on agent [label:none])
PublishToPyPi_Jenkinsfile.stage(publishToPyPi, groovy.lang.Closure)
PublishToPyPi_Jenkinsfile.script(groovy.lang.Closure)
PublishToPyPi_Jenkinsfile.publishToPyPi()
PublishToPyPi_Jenkinsfile.publishToPyPi({credentialId=pypi-token})
publishToPyPi.legacySCM(groovy.lang.Closure)
publishToPyPi.library({[email protected], retriever=null})
publishToPyPi.signArtifacts({artifactPath=dist, sigtype=.asc, platform=linux})
Expand All @@ -27,6 +27,6 @@

/tmp/workspace/sign.sh dist --sigtype=.asc --platform=linux
)
publishToPyPi.usernamePassword({credentialsId=jenkins-opensearch-pypi-credentials, usernameVariable=TWINE_USERNAME, passwordVariable=TWINE_PASSWORD})
publishToPyPi.usernamePassword({credentialsId=pypi-token, usernameVariable=TWINE_USERNAME, passwordVariable=TWINE_PASSWORD})
publishToPyPi.withCredentials([[TWINE_USERNAME, TWINE_PASSWORD]], groovy.lang.Closure)
publishToPyPi.sh(twine upload -r pypi dist/*)
18 changes: 12 additions & 6 deletions tests/jenkins/lib-testers/PublishToPyPiLibTester.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,31 @@ import static org.hamcrest.CoreMatchers.NullValue
class PublishToPyPiLibTester extends LibFunctionTester {

private String artifactsPath = 'dist'
private String credentialId

public PublishToPyPiLibTester(){}
public PublishToPyPiLibTester(String artifactsPath){
public PublishToPyPiLibTester(String credentialId) {
this.credentialId = credentialId
}
public PublishToPyPiLibTester(String credentialId, String artifactsPath) {
this.credentialId = credentialId
this.artifactsPath = artifactsPath
}

void configure(helper, binding){
binding.setVariable('GITHUB_BOT_TOKEN_NAME', 'github_bot_token_name')
helper.registerAllowedMethod("git", [Map])
helper.registerAllowedMethod("withCredentials", [Map, Closure], { args, closure ->
closure.delegate = delegate
return helper.callClosure(closure)
})
helper.registerAllowedMethod("withCredentials", [Map])
}
void parameterInvariantsAssertions(call){
assertThat(call.args.artifactsPath.toString(), notNullValue())
assertThat(call.args.credentialId.toString(), notNullValue())
}

boolean expectedParametersMatcher(call) {
return call.args.credentialId.first().toString().equals(this.credentialId)
}

boolean expectedParametersMatcherArtifact(call){
if (call.args.artifactsPath.isEmpty()) {
return (this.artifactsPath.equals('dist'))
}
Expand Down
3 changes: 2 additions & 1 deletion vars/publishToPyPi.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

/** Library to publish artifacts to PyPi registry with OpenSearch as maintainer
@param Map args = [:] args A map of the following parameters
@param args.credentialId <required> - Credential id consisting token for publishing the package
@param args.artifactsPath <optional> - The directory containing distribution files to upload to the repository. Defaults to 'dist/*'
*/
void call(Map args = [:]) {
Expand All @@ -21,7 +22,7 @@ void call(Map args = [:]) {
platform: 'linux'
)

withCredentials([usernamePassword(credentialsId: 'jenkins-opensearch-pypi-credentials', usernameVariable: 'TWINE_USERNAME', passwordVariable: 'TWINE_PASSWORD')]) {
withCredentials([usernamePassword(credentialsId: args.credentialId, usernameVariable: 'TWINE_USERNAME', passwordVariable: 'TWINE_PASSWORD')]) {
sh """twine upload -r pypi ${releaseArtifactsDir}/*"""
}
}

0 comments on commit 8511dad

Please sign in to comment.