Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload build artifact to Github as release in Jenkins

I'm searching for a way to upload a build artifact as Github Release in Jenkins as post-build action or publisher - similar to Publish Over.

This is not yet supported by the Github plugin for Jenkins (JENKINS-18598).

I've been looking into the postbuild-task plugin, but this doesn't seem to support environment variables (which I assume would be helpful to prevent logging my API token in the build output).

Has anybody done this, yet? What would be a good way to solve this with Jenkins? Uploading via cURL or via a CLI client (e.g. the Go-based github-release).

like image 920
StephenKing Avatar asked Jul 05 '14 10:07

StephenKing


People also ask

How do you publish build artifacts in Jenkins?

Publish a package using JenkinsSelect your build pipeline, and then select Configure to edit your build definition. Select Build, and then select Add build step to add a new task. Select Save, and then queue your build. Your NuGet package should be published to your Azure Artifacts feed.

How do I push changes to GitHub after Jenkins build completes?

In Jenkins execute shell under Build, creating a file and trying to push that file from Jenkins workspace to GitHub. Download Git Publisher Plugin and Configure as shown below snapshot. Click on Save and Build. Now you can check your git repository whether the file was pushed successfully or not.


2 Answers

I solved it by using the github-release tool. Works like a charm and very easy.

  1. Add a relevant parameters to the build
  2. Add a shell script to your post build steps
  3. Enter this code:
echo "Compressing artifacts into one file" zip -r artifacts.zip artifacts_folder  echo "Exporting token and enterprise api to enable github-release tool" export GITHUB_TOKEN=$$$$$$$$$$$$ export GITHUB_API=https://git.{your domain}.com/api/v3 # needed only for enterprise  echo "Deleting release from github before creating new one" github-release delete --user ${GITHUB_ORGANIZATION} --repo ${GITHUB_REPO} --tag ${VERSION_NAME}  echo "Creating a new release in github" github-release release --user ${GITHUB_ORGANIZATION} --repo ${GITHUB_REPO} --tag ${VERSION_NAME} --name "${VERSION_NAME}"  echo "Uploading the artifacts into github" github-release upload --user ${GITHUB_ORGANIZATION} --repo ${GITHUB_REPO} --tag ${VERSION_NAME} --name "${PROJECT_NAME}-${VERSION_NAME}.zip" --file artifacts.zip 
like image 130
Asaf Shveki Avatar answered Sep 19 '22 08:09

Asaf Shveki


I think you are on track!

  1. Add the post build task plugin to Jenkins
  2. Use the 'Run script only if all previous steps were successful' option
  3. I would create Jenkins parameters for the release name, tag name etc. and would save those along with your credentials to a file as a last step in the build process (before the post build task execution).
  4. Add a short script to the post build task step that calls the Github API:
  5. Set the environment variables from your saved file and delete it
  6. CURL POST for https://developer.github.com/v3/repos/releases/#create-a-release (You could use the Jenkings Groovy post build plugin instead of the post build task plugin and access the environment variables without saving them into a file but it would add so much complexity that it is not worth to use that plugin.)
  7. CURL POST to upload the artifact: https://developer.github.com/v3/repos/releases/#upload-a-release-asset
like image 44
Andrew Avatar answered Sep 20 '22 08:09

Andrew