Skip to content
This repository has been archived by the owner on Nov 16, 2020. It is now read-only.

Commit

Permalink
C&P nexus-repo creation code
Browse files Browse the repository at this point in the history
create docker image #3
  • Loading branch information
metas-ts committed May 5, 2017
1 parent 876894f commit 70c1b2b
Showing 1 changed file with 138 additions and 10 deletions.
148 changes: 138 additions & 10 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,135 @@
// thx to https://github.com/jenkinsci/pipeline-examples/blob/master/docs/BEST_PRACTICES.md


def boolean isRepoExists(String repoId)
{
withCredentials([usernameColonPassword(credentialsId: 'nexus_jenkins', variable: 'NEXUS_LOGIN')])
{
echo "Check if the nexus repository ${repoId} exists";

// check if there is a repository for ur branch
final String checkForRepoCommand = "curl --silent -X GET -u ${NEXUS_LOGIN} https://repo.metasfresh.com/service/local/repositories | grep '<id>${repoId}-releases</id>'";
final grepExitCode = sh returnStatus: true, script: checkForRepoCommand;
final repoExists = grepExitCode == 0;

echo "The nexus repository ${repoId} exists: ${repoExists}";
return repoExists;
}
}

def createRepo(String repoId)
{
withCredentials([usernameColonPassword(credentialsId: 'nexus_jenkins', variable: 'NEXUS_LOGIN')])
{
echo "Create the repository ${repoId}-releases";

final String createRepoPayload = """<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<repository>
<data>
<id>${repoId}-releases</id>
<name>${repoId}-releases</name>
<exposed>true</exposed>
<repoType>hosted</repoType>
<writePolicy>ALLOW_WRITE_ONCE</writePolicy>
<browseable>true</browseable>
<indexable>true</indexable>
<repoPolicy>RELEASE</repoPolicy>
<providerRole>org.sonatype.nexus.proxy.repository.Repository</providerRole>
<provider>maven2</provider>
<format>maven2</format>
</data>
</repository>
""";

// # nexus ignored application/json
final String createRepoCommand = "curl --silent -H \"Content-Type: application/xml\" -X POST -u ${NEXUS_LOGIN} -d \'${createRepoPayload}\' https://repo.metasfresh.com/service/local/repositories"
sh "${createRepoCommand}"

echo "Create the repository-group ${repoId}";

final String createGroupPayload = """<?xml version="1.0" encoding="UTF-8"?>
<repo-group>
<data>
<repositories>
<!-- include mvn-public that contains everything we need to perform the build-->
<repo-group-member>
<name>mvn-public</name>
<id>mvn-public</id>
<resourceURI>https://repo.metasfresh.com/content/repositories/mvn-public/</resourceURI>
</repo-group-member>
<!-- include ${repoId}-releases which is the repo to which we release everything we build within this branch -->
<repo-group-member>
<name>${repoId}-releases</name>
<id>${repoId}-releases</id>
<resourceURI>https://repo.metasfresh.com/content/repositories/${repoId}-releases/</resourceURI>
</repo-group-member>
</repositories>
<name>${repoId}</name>
<repoType>group</repoType>
<providerRole>org.sonatype.nexus.proxy.repository.Repository</providerRole>
<exposed>true</exposed>
<id>${repoId}</id>
<provider>maven2</provider>
<format>maven2</format>
</data>
</repo-group>
"""

// # nexus ignored application/json
final String createGroupCommand = "curl --silent -H \"Content-Type: application/xml\" -X POST -u ${NEXUS_LOGIN} -d \'${createGroupPayload}\' https://repo.metasfresh.com/service/local/repo_groups"
sh "${createGroupCommand}"

echo "Create the scheduled task to keep ${repoId}-releases from growing too big";

final String createSchedulePayload = """<?xml version="1.0" encoding="UTF-8"?>
<scheduled-task>
<data>
<id>cleanup-repo-${repoId}-releases</id>
<enabled>true</enabled>
<name>Remove Releases from ${repoId}-releases</name>
<typeId>ReleaseRemoverTask</typeId>
<schedule>daily</schedule>
<startDate>${currentBuild.startTimeInMillis}</startDate>
<recurringTime>03:00</recurringTime>
<properties>
<scheduled-task-property>
<key>numberOfVersionsToKeep</key>
<value>3</value>
</scheduled-task-property>
<scheduled-task-property>
<key>indexBackend</key>
<value>false</value>
</scheduled-task-property>
<scheduled-task-property>
<key>repositoryId</key>
<value>${repoId}-releases</value>
</scheduled-task-property>
</properties>
</data>
</scheduled-task>"""

// # nexus ignored application/json
final String createScheduleCommand = "curl --silent -H \"Content-Type: application/xml\" -X POST -u ${NEXUS_LOGIN} -d \'${createSchedulePayload}\' https://repo.metasfresh.com/service/local/schedules"
sh "${createScheduleCommand}"
} // withCredentials
}

def deleteRepo(String repoId)
{
withCredentials([usernameColonPassword(credentialsId: 'nexus_jenkins', variable: 'NEXUS_LOGIN')])
{
echo "Delete the repository ${repoId}";

final String deleteGroupCommand = "curl --silent -X DELETE -u ${NEXUS_LOGIN} https://repo.metasfresh.com/service/local/repo_groups/${repoId}"
sh "${deleteGroupCommand}"

final String deleteRepoCommand = "curl --silent -X DELETE -u ${NEXUS_LOGIN} https://repo.metasfresh.com/service/local/repositories/${repoId}-releases"
sh "${deleteRepoCommand}"

final String deleteScheduleCommand = "curl --silent -X DELETE -u ${NEXUS_LOGIN} https://repo.metasfresh.com/service/local/schedules/cleanup-repo-${repoId}-releases"
sh "${deleteScheduleCommand}"
}
}

/**
* This method will be used further down to call additional jobs such as metasfresh-procurement and metasfresh-webui
Expand Down Expand Up @@ -147,17 +276,16 @@ node('agent && linux') // shall only run on a jenkins agent with linux
{
stage('Preparation') // for display purposes
{
if(!isRepoExists(MF_MAVEN_REPO_NAME))
{
createRepo(MF_MAVEN_REPO_NAME);
}

// checkout our code
checkout([
$class: 'GitSCM',
branches: [[name: "${env.BRANCH_NAME}"]],
doGenerateSubmoduleConfigurations: false,
extensions: [
[$class: 'CleanCheckout']
],
submoduleCfg: [],
userRemoteConfigs: [[credentialsId: 'github_metas-dev', url: 'https://github.com/metasfresh/metasfresh-admin.git']]
])
// note that we do not know if the stuff we checked out in the other node is available here, so we somehow need to make sure by checking out (again).
// see: https://groups.google.com/forum/#!topic/jenkinsci-users/513qLiYlXHc
checkout scm; // i hope this to do all the magic we need
sh 'git clean -d --force -x' // clean the workspace
}

configFileProvider([configFile(fileId: 'metasfresh-global-maven-settings', replaceTokens: true, variable: 'MAVEN_SETTINGS')])
Expand Down

0 comments on commit 70c1b2b

Please sign in to comment.