0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

herokuCLIを通したherokuの開始(入門者用)

0
Posted at

Herokuとは?

Herokuは はアプリの構築、提供、監視、スケールに役立つクラウドプラットフォームで、アイデアを出してから運用を開始するまでのプロセスを迅速に進めることが可能です。また、インフラストラクチャの管理の問題からも解放されます。--公式フォムページから抜粋

補足するとHerokuはPaaS(Platform as a Service)として開発者はアプリケーション開発のみ集中すれば、アプリケーションの配布と運用が出来るということです。GitHubと連動されているのでgitにpushだけでデプロイが可能なのでCI/CDに適切なプラットフォームです。現在Node.js,Java,PHP,Goなどの言語を対応しています。

Heroku-CLIのダウンロード

Heroku-CLIはターミナル上でHerokuアプリをよりやすく扱えるツールです。
こちらでダウンロードできます。(gitを事前にインストールしてください。)
自分のOSに合わせてインストールしてください。

コマンドの操作

ログイン

※会員登録はHerokuのフォームページで行ってください。
インストールが終わったら下記のコマンドでHerokuにログインができます。
heroku login -i
-iオプションは「コマンド内部でログインします」の意味です。このオプションがないとブラウザーが開いて、ブラウザーでログインするようになります。
ログインが終わるとHerokuアプリで作りたいディレクトリに移動してgit initでgitを立ち上げます。
もし、既存のソースがない場合は、下記のリンクでHerokuのチュートリアルコードがcloneできます。
Node.js Java PHP

リポジトリのClone

heroku startでHeroku上にアプリが自動に生成されて。この場合は、アプリの名前がランダムで生成されるので後で変更するかもしくは先にアプリを作ってheroku git:clone -a [アプリ名]でcloneできます。

ソースコードのコミット

PHPとJavaで「hello world」を出力するコードを上げてみます。
※PHPは必ずindex.phpであげてください。
※javaはspring-bootを使用したコードです。

PHP

index.php
<?php
echo 'hello world';

Java

DemoApplication.java
package com.example.test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@SpringBootApplication
public class DemoApplication {

	@RequestMapping("/")
	@ResponseBody
	String home() {
		return "hello world";
	}

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

}

Herokuはウェブアプリケーションなので、ただのSystem.out.println()では見れません。
Springなどのウェブフレームワークを使ったほうがいいと思います。

git add . git commit -am "コミットメッセージ" git push heroku master
上のコマンドでHerokuにソースをPushができます。
heroku open -a [アプリ名]のコマンドでブラウザで自分のアプリが開きます。
image.png
Herokuフォームページの「Open app」ボタンでも確認が出来ます。

終わりに

Herokuに簡単にデプロイする方法についてご紹介させていただきました。どんどんサーバーレス、コードレスのやってきていると思うので、HerokuやAWS Elastic Beanstalkなどのクラウドサービスを身に着けると将来的にすごい強みになるかと思います。
ご覧いただきありがとうございます

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?