Skip to content

Instantly share code, notes, and snippets.

@valeriomazzeo
Created July 5, 2017 21:24
Show Gist options
  • Save valeriomazzeo/5491aee76f758f7352e2e6611ce87ec1 to your computer and use it in GitHub Desktop.
Save valeriomazzeo/5491aee76f758f7352e2e6611ce87ec1 to your computer and use it in GitHub Desktop.
Creates or update a GitHub release for the given tag name
#!/usr/bin/env ruby
require 'optparse'
require 'octokit'
options = {}
OptionParser.new do |opt|
opt.on('-s', '--secret SECRET', 'GitHub access token') { |o| options[:secret] = o }
opt.on('-r', '--repo-slug REPO_SLUG', 'Repo slug. i.e.: apple/swift') { |o| options[:repo_slug] = o }
opt.on('-c', '--changelog-file CHANGELOG_FILE', 'Changelog path') { |o| options[:changelog_file] = o }
opt.on('-t', '--tag TAG', 'Tag name') { |o| options[:tag_name] = o }
end.parse!
raise OptionParser::MissingArgument if options[:secret].nil?
raise OptionParser::MissingArgument if options[:repo_slug].nil?
raise OptionParser::MissingArgument if options[:changelog_file].nil?
raise OptionParser::MissingArgument if options[:tag_name].nil?
client = Octokit::Client.new(:access_token => options[:secret])
user = client.user
user.login
unless client.scopes.include? 'public_repo' or client.scopes.include? 'repo'
raise Error, "Insufficient permissions. Make sure your token contains the repo or public_repo scope."
end
puts "Logged in as #{user.name}"
puts "Deploying to repo: #{options[:repo_slug]}"
tag_matched = false
release_url = nil
releases = client.releases(options[:repo_slug])
body = File.open(options[:changelog_file], "rb").read
releases.each do |release|
if release.tag_name == options[:tag_name]
release_url = release.rels[:self].href
tag_matched = true
end
end
# if tag has been pushed directly to git, create a github release
if tag_matched == false
client.create_release(options[:repo_slug], options[:tag_name], { :name => options[:tag_name], :body => body })
else
client.update_release(release_url, { :name => options[:tag_name], :body => body })
end
@felixjb
Copy link

felixjb commented Aug 19, 2019

Hi, how can I use this with travis-ci to deploy and create my release notes in GitHub?

@valeriomazzeo
Copy link
Author

Hi @felixjb, I use it this way:

Gemfile

source "https://rubygems.org"
gem 'github_changelog_generator'
travis.yml

      before_script:
        - bundle install
        - bundle exec github_changelog_generator --max-issues 0 -t ${GITHUB_API_TOKEN} --between-tags ${TRAVIS_TAG}
      script: bundle exec github_release.rb -s ${GITHUB_API_TOKEN} -r ${TRAVIS_REPO_SLUG} -c CHANGELOG.md -t ${TRAVIS_TAG}

@felixjb
Copy link

felixjb commented Aug 19, 2019

Is there a way to use it entirely on travis.yml? Because my project is in TypeScript

@valeriomazzeo
Copy link
Author

I am not sure what you are asking. The Gemfile is needed to install github_changelog_generator that generates CHANGELOG.md with release notes from commits between TRAVIS_TAG.

If you already have your changelog file, you can just call the script directly by replacing those parameters..

@hobbyquaker
Copy link

Hi @valeriomazzeo
First of all: Many thanks for this script! I'm using it since a while to add a previously generated changelog to the release description, worked like a charm! But sadly since a few days it throws an error and I'm totally clueless, my ruby skills are near zero ;-)
Can you give me a hint on how to fix this?
https://travis-ci.org/rdmtc/RedMatic/jobs/631894199#L729
Regards,
Sebastian

@valeriomazzeo
Copy link
Author

valeriomazzeo commented Jan 6, 2020

@hobbyquaker it looks like it could be an issue with that specific version of octokit and faraday.
I use bundler and in my Gemfile I don't install faraday manually.
Instead - I only install the latest version of gem 'octokit'.

In .travis.yml try replacing this:

after_deploy:
- gem install octokit:4.14.0
- gem install faraday:0.15.4

with this:

after_deploy:
- gem install octokit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment