Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deploy from travis #1051

Merged
merged 11 commits into from
Sep 16, 2019
Merged

Deploy from travis #1051

merged 11 commits into from
Sep 16, 2019

Conversation

sivakumar-kailasam
Copy link
Member

@sivakumar-kailasam sivakumar-kailasam commented Sep 9, 2019

With this PR we get deploys to master on merges and preview apps for PRs if they're successful. We'll have to look at the travis deploy step logs to get the url for now.

I've reorganized the build steps so that we save a few mins in each build.

@sivakumar-kailasam sivakumar-kailasam marked this pull request as ready for review September 9, 2019 10:40
@chancancode
Copy link
Contributor

This would only deploy after the build succeeded right, is that what we want?

@sivakumar-kailasam
Copy link
Member Author

Yep.

@chancancode
Copy link
Contributor

I actually appreciate that we can get a preview right now even if the tests are failing (usually for silly reasons, like failing spell check). I suppose I can live without it... but have you considered running the deploy (preview) in parallel with the tests?

@sivakumar-kailasam
Copy link
Member Author

sivakumar-kailasam commented Sep 10, 2019 via email

@chancancode
Copy link
Contributor

chancancode commented Sep 10, 2019

It seems like we will also lose the "Deploy preview ready!" link from the commit status, which would be a shame. (Unless I am wrong and the netlify/ember-guides/deploy-preview link status will still show up even though we are pushing the builds manually?)

We have a similar setup at work (we also push the deploys from Travis, though not to Netlify). I wrote a script (in Ruby) to add the deploy preview link manually using the GitHub commit status API, which you can probably just use (if we don't mind relying on the system Ruby on Travis, which should work fine) or port to Node:

begin
  require "bundler/inline"
rescue LoadError => e
  $stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler"
  raise e
end

gemfile do
  source "https://rubygems.org"
  gem "activesupport"
  gem "jwt"
end

require "active_support/all"
require "json"
require "net/http"
require "openssl"
require "open3"

def get_token(http)
  private_pem = File.read("???.private-key.pem")
  private_key = OpenSSL::PKey::RSA.new(private_pem)

  payload = {
    # issued at time
    iat: Time.now.to_i,
    # JWT expiration time
    exp: 1.minute.from_now.to_i,
    # Integration's GitHub identifier
    iss: ???
  }

  jwt = JWT.encode(payload, private_key, "RS256")

  response = http.post(
    "/installations/???/access_tokens",
    "",
    "Accept" => "application/vnd.github.machine-man-preview+json",
    "Authorization" => "Bearer #{jwt}",
    "Content-Type" => "text/plain"
  )

  if response.code != "201"
    raise "Failed to create token: #{response.body}"
  else
    JSON.parse(response.body)["token"]
  end
end

def pending!(http, token, message)
  create_commit_status(
    http, token,
    context: "Deploy",
    state: "pending",
    description: message,
    target_url: ENV["TRAVIS_BUILD_WEB_URL"]
  )
end

def error!(http, token, message)
  create_commit_status(
    http, token,
    context: "Deploy",
    state: "error",
    description: message,
    target_url: ENV["TRAVIS_BUILD_WEB_URL"]
  )
end

def success!(http, token, message, url)
  create_commit_status(
    http, token,
    context: "Deploy",
    state: "success",
    description: message,
    target_url: url
  )
end

def deploy!(http, token)
  pending!(http, token, "Building...")

  output, status = Open3.capture2("yarn deploy")

  $stdout.print output

  if status.success?
    url = ???
    success!(http, token, "Preview available", url)
  else
    error!(http, token, "Error!")
  end
end

def create_commit_status(http, token, options)
  response = http.post(
    "/repos/ember-learn/guides-source/statuses/#{ENV['TRAVIS_COMMIT']}",
    JSON.generate(options),
    "Accept" => "application/vnd.github.machine-man-preview+json",
    "Authorization" => "token #{token}",
    "Content-Type" => "application/json"
  )

  if response.code != "201"
    raise "Failed to create commit status: #{response.body}"
  end
end

Net::HTTP.start("api.github.com", 443, use_ssl: true) do |http|
  token = get_token(http)

  if ARGV[0] == "--pending"
    pending!(http, token, ARGV[1])
  else
    deploy!(http, token)
  end
end

This is how it looks:

Screen Shot 2019-09-09 at 9 45 59 PM

Screen Shot 2019-09-09 at 9 45 35 PM

The "Details" link take you right to the deployed app once its finish, or the Travis build responsible for pushing it while it's pending.

Unfortunately, it's slightly involved to setup cause you can only use the status API as an app. But once you have the app setup and generated the key-pairs, the script should Just Work™. There may also be a GitHub Action somewhere that does exactly this.

I don't think this is a blocker though, but it would be good to add the link back to help contributors find their preview. Otherwise, if you have to dig through the build logs to find the URL, I think the utility of the previews are reduced significantly – most people probably won't even know that there are previews for their pull requests.

@sivakumar-kailasam
Copy link
Member Author

Thanks for the script. ATM our deploys to master are failing which is the main issue this PR is trying to address. We're aware of the functionalities we're loosing by doing this, so I'll open an issue which includes your script and will detail what needs to be done to be on parity with what we used to have.

@sivakumar-kailasam
Copy link
Member Author

We have around 4k files to upload during the deploy step. Netlify cli doesn't show any progress as it uploads files, so travis times out & we end up with a broken deploy again. Will close this PR in favor of a github app that can handle all these issues.

Copy link
Contributor

@jenweber jenweber left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you everyone who helped with this! It is critical for the upcoming 3.13 release, scheduled for this week!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants