LoginSignup
2
1

More than 3 years have passed since last update.

RubyonJetsをGithubActionsで自動デプロイする

Posted at

概要

Ruby on Jetsを用いたアプリケーションをmergeやpushと同時に自動デプロイできるようにします。
GithubActionsが提供するランナーにはAWS CLIやrsync、zipなどが元からインストールされているみたいで、簡単にデプロイすることができました。

手順としては2ステップでできます!
1. AWS CredentialsをGithub Secretsに登録
2. .github/workflows/にymlファイルを記述する

環境

Ruby: 2.5.7
Ruby on Jet: 2.3.18

手順

AWSのCredentialsをGithub Secretsに登録

暗号化されたシークレットを参考にAWS_ACCESS_KEY_IDとAWS_SECRET_ACCESS_KEYを設定してください。
結構簡単に設定できます。

GithubActionsの設定ymlを記述

今回の例ではmainブランチにmerge(or push)した際にproductionに自動デプロイできるような設定を記述しました。

/.github/workflows/deploy_to_production.yml

name: Deploy to production

on:
  push:
    branches: [ main ]

jobs:
  deploy:

    runs-on: ubuntu-latest
    strategy:
      matrix:
        ruby-version: [ '2.5.7' ]

    steps:
    - uses: actions/checkout@v2

    - name: Set up Ruby
      uses: ruby/setup-ruby@473e4d8fe5dd94ee328fdfca9f8c9c7afc9dae5e
      with:
        ruby-version: ${{ matrix.ruby-version }}
        bundler-cache: false # runs 'bundle install' and caches installed gems automatically

    - name: Bundle install
      run: bundle install --path vendor/gems

    - name: Configure AWS credentials
      uses: aws-actions/configure-aws-credentials@v1
      with:
        aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
        aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
        aws-region: ap-northeast-1

    - name: Jets deploy
      env:
        JETS_ENV: production
      run: echo y | bundle exec jets deploy

以下の箇所はjets deploy時にbundle installが走るpathと揃える目的があります。
こうしないとjets deploy時にgemの容量がオーバーしてうまくいきませんでした。

- name: Bundle install
  run: bundle install --path vendor/gems

まとめ

stagingのデプロイは/.github/workflows/deploy_to_staging.ymlなど作成して、JETS_ENV: stagingにするだけでOKです!
ちなみにこの記述をしたブランチをmainに反映した時点から自動デプロイが始まるので気をつけてください!

参考

GitHubの新機能「GitHub Actions」で試すCI/CD

2
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
2
1