diff --git a/Jenkinsfile b/Jenkinsfile index 767f2ee..9885602 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -3,6 +3,7 @@ FROM centos:7 RUN yum update clean all RUN yum -y install java-1.8.0-openjdk-devel.x86_64 RUN yum -y install maven +RUN yum -y install git RUN adduser jenkins ENV JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk """; @@ -21,46 +22,109 @@ def getChangelistDescription() { } def getNextVersion() { - def latestVersion = readFile "${env.WORKSPACE}/version.txt" + def latestVersion = readFile "${env.WORKSPACE}/src/main/resources/version.txt" print "version=" + latestVersion; def (major, minor, patch) = latestVersion.tokenize('.').collect { it.toInteger() }; - print "major=" + major + ",minor=" + minor + ",patch=" + patch; + print "next version: major=" + major + ",minor=" + minor + ",patch=" + (patch + 1); return "${major}.${minor}.${patch + 1}"; } -node { - try { +// whether or not to deploy to github & dockerhub +def deploy; +// the version being built/released +def tag; +// the docker image +def image; + +def email; +withCredentials([string(credentialsId: 'botdarr-email', variable: 'botdarr_email')]) { + email = "${botdarr_email}"; +} + +def username; +withCredentials([string(credentialsId: 'botdarr-username', variable: 'botdarr_username')]) { + username = "${botdarr_username}"; +} + +pipeline { + agent any + stages { stage("Checkout") { - checkout([$class: 'GitSCM', branches: [[name: '**']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'github', url: 'https://github.com/shayaantx/botdar.git']]]) + steps { + script { + checkout([$class: 'GitSCM', branches: [[name: '**']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'github', url: "https://github.com/${username}/botdar.git"]]]) + env.GIT_COMMIT_MSG = sh (script: 'git log -1 --pretty=%B ${GIT_COMMIT}', returnStdout: true).trim() + deploy = !env.GIT_COMMIT_MSG.startsWith("Update version to") && env.BRANCH_NAME == "development"; + } + } } stage('Prepare docker') { - fileOperations([fileCreateOperation(fileContent: "${dockerFileContents}", fileName: './Dockerfile')]); + steps { + script { + fileOperations([fileCreateOperation(fileContent: "${dockerFileContents}", fileName: './Dockerfile')]); + tag = getNextVersion(); + image = docker.build("botdarr-image", "-f ./Dockerfile ."); + } + } } - - def tag = getNextVersion(); - def image = docker.build("botdarr-image", "-f ./Dockerfile ."); - image.inside('-u root') { - stage('Build') { - sh './mvnw --no-transfer-progress compile' + stage('Build') { + steps { + script { + image.inside('-u root') { + sh './mvnw --no-transfer-progress compile' + } + } } - - stage("Test") { - sh './mvnw --no-transfer-progress test' + } + stage('Test') { + steps { + script { + image.inside('-u root') { + sh './mvnw --no-transfer-progress test' + } + } } - - stage("Package") { - fileOperations([fileCreateOperation(fileContent: "version=${tag}", fileName: './src/main/resources/version.txt')]); - sh './mvnw --no-transfer-progress package -DskipTests' + } + stage('Package') { + steps { + script { + image.inside('-u root') { + sh "echo ${tag} > ./src/main/resources/version.txt" + sh './mvnw --no-transfer-progress package -DskipTests' + archiveArtifacts 'target/botdarr-release.jar' + } + } } - - stage("Archive") { - archiveArtifacts 'target/botdarr-release.jar' + } + + stage('Update version') { + when { + expression { + return deploy + } } - - if (env.BRANCH_NAME == "development") { - //don't upload for PR's - stage('Create/Upload Release') { + steps { + sshagent(['jenkins-ssh-key-github']) { + sh "git remote set-url origin git@github.com:${username}/botdarr.git" + sh "git config --global user.name ${username}" + sh "git config --global user.email ${email}" + sh "ssh -oStrictHostKeyChecking=no ${username}@github.com || true" + sh "git add src/main/resources/version.txt" + sh "git commit -m \"Update version to ${tag}\"" + sh "git push origin HEAD:${env.BRANCH_NAME}" + } + } + } + + stage('Create/Upload Release') { + when { + expression { + return deploy + } + } + steps { + script { withCredentials([string(credentialsId: 'git-token', variable: 'token')]) { def description = getChangelistDescription(); print "description=" + description; @@ -72,35 +136,42 @@ node { } } } + + stage('Upload to dockerhub') { + when { + expression { + return deploy + } + } + steps { + script { + def dockerFileImage = """ + FROM centos:7 + RUN yum update; yum clean all; yum -y install java-1.8.0-openjdk-devel.x86_64; + ENV JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk + ENV PATH=$PATH:$JAVA_HOME/bin - if (env.BRANCH_NAME == "development") { - //don't upload for PR's - stage('Upload to dockerhub') { - def dockerFileImage = """ - FROM centos:7 - RUN yum update; yum clean all; yum -y install java-1.8.0-openjdk-devel.x86_64; - ENV JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk - ENV PATH=$PATH:$JAVA_HOME/bin - - RUN mkdir -p /home/botdarr - ADD target/botdarr-release.jar /home/botdarr + RUN mkdir -p /home/botdarr + ADD target/botdarr-release.jar /home/botdarr - WORKDIR /home/botdarr - RUN java -version + WORKDIR /home/botdarr + RUN java -version - ENTRYPOINT ["java", "-jar", "botdarr-release.jar"] - """; - fileOperations([fileCreateOperation(fileContent: "${dockerFileImage}", fileName: './DockerfileUpload')]); - def releaseTag = "latest"; - def imageWithReleaseTag = docker.build("shayaantx/botdarr:${releaseTag}", "-f ./DockerfileUpload ."); - withDockerRegistry(credentialsId: 'docker-credentials') { - imageWithReleaseTag.push(); + ENTRYPOINT ["java", "-jar", "botdarr-release.jar"] + """; + fileOperations([fileCreateOperation(fileContent: "${dockerFileImage}", fileName: './DockerfileUpload')]); + def releaseTag = "latest"; + def imageWithReleaseTag = docker.build("${username}/botdarr:${releaseTag}", "-f ./DockerfileUpload ."); + withDockerRegistry(credentialsId: 'docker-credentials') { + imageWithReleaseTag.push(); + } } } } - } finally { - stage("Cleanup") { - deleteDir(); + } + post { + always { + deleteDir() } } } \ No newline at end of file diff --git a/pom.xml b/pom.xml index 8b0a44c..1a5defb 100644 --- a/pom.xml +++ b/pom.xml @@ -24,7 +24,7 @@ net.dv8tion JDA - 4.0.0_39 + 4.2.0_168 jar compile @@ -97,7 +97,7 @@ junit junit - 4.12 + 4.13.1 test diff --git a/src/main/java/com/botdarr/clients/ChatClientType.java b/src/main/java/com/botdarr/clients/ChatClientType.java index 36e86a6..f508172 100644 --- a/src/main/java/com/botdarr/clients/ChatClientType.java +++ b/src/main/java/com/botdarr/clients/ChatClientType.java @@ -99,7 +99,8 @@ public void init() throws Exception { ChatClientResponseBuilder responseChatClientResponseBuilder = new DiscordResponseBuilder(); ApisAndCommandConfig config = buildConfig(responseChatClientResponseBuilder); - JDA jda = new JDABuilder(Config.getProperty(Config.Constants.DISCORD_TOKEN)).addEventListeners(new ListenerAdapter() { + + JDA jda = JDABuilder.createDefault((Config.getProperty(Config.Constants.DISCORD_TOKEN))).addEventListeners(new ListenerAdapter() { @Override public void onGenericEvent(@Nonnull GenericEvent event) { super.onGenericEvent(event); diff --git a/src/main/resources/version.txt b/src/main/resources/version.txt new file mode 100644 index 0000000..ab0fa33 --- /dev/null +++ b/src/main/resources/version.txt @@ -0,0 +1 @@ +5.0.5 diff --git a/version.txt b/version.txt deleted file mode 100644 index 32f3eaa..0000000 --- a/version.txt +++ /dev/null @@ -1 +0,0 @@ -5.0.1 \ No newline at end of file