20210906のMacに関する記事は7件です。

GitHub Actions Macアプリのビルド方法+リリースの自動化方法

Macアプリのリリース手順が煩わしかったので、GitHub Actionsを利用してリリースを自動化しました。 その際の知見(Q&A)と実際の構成を共有します。 Q&A Q: fastlane, bundlerはデフォルトで利用できる? 利用できる。(bundle installをしなくてもfastlane利用可能。) Q: passwordやp12ファイルなど機密情報はどのように扱う? 暗号化されたシークレット参照 この資料にパスワードやp12ファイルなどの機密情報の設定方法およびワークフローファイルからの参照方法について記載されています。(以下は抜粋) シークレットの設定方法(リポジトリ単位) ワークフローファイルからのシークレットの参照方法 Q: workflowは手動起動できる? できる。 パラメータも設定可能。 手動起動の際にbranchの指定も可能。 設定例 on: # Workflowの手動起動の設定です。 # version_numberを入力できるようにしています。 workflow_dispatch: inputs: version_number: description: 'アプリのバージョン番号。入力例 0.3.0, 1.0.0 など。' required: true # 中略 #version_numberの参照方法 bundle exec fastlane release version_number:${{ github.event.inputs.version_number }} 上の定義をすると、workflowの起動画面は下のようになります。 また手動起動のほか、pullreqのタイミングやbranchがpush時などに自動起動が可能です。 ワークフローをトリガーするイベント 同資料中のworkflow_dispatch GitHub Actionsのメタデータ構文 Q: actions/checkout@v2 でcheckoutされるブランチは? デフォルトではworkflowを実行したブランチ actions/checkout Q: ワークフローファイルの環境変数とコンテキストの違いは? 以下の通り 環境変数 Q: git tagの作成の仕方は? 一例 - name: Create git tag run: | git tag v${{github.event.inputs.version_number}} git push origin v${{github.event.inputs.version_number}} Q: GitHubのリリースページの作成の仕方は? 一例 ※ actions/create-release@v1はarchivedされていてメンテナンスされていない。 - name: Create release id: create_release uses: actions/create-release@v1 env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # This token is provided by Actions, you do not need to create your own token with: tag_name: v${{github.event.inputs.version_number}} release_name: Release v${{github.event.inputs.version_number}} body: | Changes in this Release - First Change - Second Change draft: true prerelease: false actions/create-release Q: GitHubのリリースページにzipファイルなどの成果物をアップロードするには 下の例は、action/create-release@v1を前提としている。 - name: Upload Release Assets id: upload-release-asset uses: actions/upload-release-asset@v1 env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} with: upload_url: ${{ steps.create_release.outputs.upload_url }} # This pulls from the CREATE RELEASE step above, referencing it's ID to get its outputs object, which include a `upload_url`. See this blog post for more info: https://jasonet.co/posts/new-features-of-github-actions/#passing-data-to-future-steps asset_path: ./build/DokodemoDrag.app.zip asset_name: DokodemoDrag.app.zip asset_content_type: application/zip actions/upload-release-asset Q: on: workflow_dispatchを設定しても, Actionsタブでワークフローが表示されない。 私の場合、GitHubから直接.github/workflow/aaa.yamlファイルを作成した場合に上の事象が発生しています。 (mainブランチ以外にワークフローファイルを作成すると表示されないかもしれない) 結構表示されないケースがある模様。 Q: expression中での三項演算子的なものは? ${{ val1 ? val1 : "B" }} 的なことをしたい。 ドキュメント的には見当たらない。 動作検証の限りでは、||と&&はRubyやJavaScriptと同様の動作をするので、 ${{ val1 || "B" }} # val1の値が偽になるなら"B"を返す. や ${{ (val1 && val2) || "B" }} # val1 && val2が偽でなければval2を返し、 偽なら"B"を返す。 といったことができる。 下記は実際の利用イメージ - name: 'Test' env: # github.event.inputs.version_nameは 1.0.0などを想定 TAG_NAME: v${{ github.event.inputs.version_name }} run: |          # 式の結果が偽なら""を出力する。真なら v1.0.0などを返す。 echo ${{ (github.event.inputs.version_name && env.TAG_NAME) }} Q: ${{expression}}の評価がいまいち分からない 別記事を作成しました。 Q: GitHub Actionsの完了時のメール通知を止めたい Account Settings -> Notifications -> Actionsのセクションから設定を変更できる。 宣伝(DokodemoDragについて) DokodemoDragは、個人的に作成中のアプリでMacのWindow移動を楽に行えるようにするツールです。よければ触ってみてください ※現時点ではnotarizationができておらず、macOSのGateKeeperに検出されます)。 実装の概要もこちらで公開しています。 https://zenn.dev/hmu/articles/4c17afaaf5012e 宣伝は以上で、以下は実際に自動化したGitHub Actionsの構成についてです。 実際のworkflowの構成 前提 GitHubのランナー上のXcode versionは12.4 プロジェクトの構成はworkspace下にprojectが2つ存在する DokodemoDrag.xcworkspace/ |-DokodemoDrag.xcodeproj/ |-DokodemoDragLauncher/DokodemoDragLauncher.xcodeproj/ リリース形式は、GitHub上のリリースページにappname.app.zipという形式で公開(Storeへの公開ではない) リリース対象のMac AppのCertificateは、Developer IdではなくDevelopmentを利用 証明書のimportにmatchは利用せず手動で行う プロジェクトのビルドにはfastlaneを利用する 現時点ではprovisioning profileは未使用 現時点ではfastlaneの設定はGitHub Actionsでの利用前提となってしまっています ワークフローの概要 1.ワークフローは実行者が手動で起動する 2.アプリのバージョンはWorkflow起動時に指定する 3.ビルド時にワークフロー起動時に指定されたバージョン番号を付与する 4.ビルドに成功したら バージョンに対応するgitのtagを生成する 5.GitHubのリリースページをドラフトで作成する 6.後始末 workflowファイル 説明は直接コメントに記載しました。 release.yml name: Release app on: # Allows you to run this workflow manually from the Actions tab # Workflowの手動起動の設定です。 # version_numberを入力できるようにしています。 workflow_dispatch: inputs: version_number: description: 'アプリのバージョン番号。入力例 0.3.0, 1.0.0 など。' required: true # A workflow run is made up of one or more jobs that can run sequentially or in parallel jobs: # This workflow contains a single job called "build" release: # 実行環境をmacに設定。 runs-on: macos-latest steps: - name: Show Xcode version run: xcodebuild -version # defaultでWorkflowを実行したbranchがcheckoutされる。 - uses: actions/checkout@v2 - name: Show Git Branch run: git symbolic-ref --short HEAD # https://docs.github.com/ja/actions/guides/installing-an-apple-certificate-on-macos-runners-for-xcode-development # 上記のドキュメントにcertificateをGitHub Actionsのランナーにimport # する方法が記載されています。 # 下の設定では、GitHubのsecretに設定したp12ファイルの内容を復元しています。 # 実際のcertificateのkeychainへのimportは、fastlaneで行っています。 - name: Generate p12 file env: BUILD_CERTIFICATE_BASE64: ${{ secrets.BUILD_CERTIFICATE_BASE64 }} # 以下で定義しているP12_PATHは、${{ runner.temp }}を利用することで、 # env: 内で定義可能かもしれません。(未検証) run: | P12_PATH=$RUNNER_TEMP/build_certificate.p12 echo $P12_PATH # シークレットから証明書とプロビジョニングプロファイルをインポートする echo -n "$BUILD_CERTIFICATE_BASE64" | base64 --decode --output $P12_PATH # commitに利用する情報を設定しています。 - name: git config run: | echo github.actor: ${{github.actor}} git config user.name ${{github.actor}} # fastlaneをbundle execで利用するためにbundlerを利用しています。 - name: bundle install run: | bundle install # アプリのビルド処理です。 - name: Build DokodemoDragLauncher & DokodemoDrag # ここもP12_PATHは ${{ runner.temp }} を利用した方が良いかもしれません。 env: # これらの環境変数はfastlaneの処理で利用します。 P12_PASSWORD: ${{ secrets.P12_PASSWORD }} KEYCHAIN_PASSWORD: ${{ secrets.KEYCHAIN_PASSWORD }} run: | export P12_PATH=$RUNNER_TEMP/build_certificate.p12 echo $P12_PATH # certificateのimport, アプリのビルド, 成果物用のzipファイル生成を行います。 bundle exec fastlane release version_number:${{github.event.inputs.version_number}} # .はかなり雑かもしれません。 git add . git commit -m "Release v${{github.event.inputs.version_number}}" git push origin # タグ付けをします。 - name: Create git tag run: | echo Create tag ${{github.event.inputs.version_number}} git tag v${{github.event.inputs.version_number}} git push origin v${{github.event.inputs.version_number}} # GitHubのリリースページを作成します。(draftで作成します) # actions/create-release@v1は既にメンテ終了しているので、 # 他のアクションを利用した方が良いです。 - name: Create release id: create_release uses: actions/create-release@v1 env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # This token is provided by Actions, you do not need to create your own token with: tag_name: v${{github.event.inputs.version_number}} release_name: Release v${{github.event.inputs.version_number}} body: | Changes in this Release - First Change - Second Change draft: true prerelease: false # リリースページに成果物のzipファイルをアップロードします。 - name: Upload Release Assets id: upload-release-asset uses: actions/upload-release-asset@v1 env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} with: upload_url: ${{ steps.create_release.outputs.upload_url }} # This pulls from the CREATE RELEASE step above, referencing it's ID to get its outputs object, which include a `upload_url`. See this blog post for more info: https://jasonet.co/posts/new-features-of-github-actions/#passing-data-to-future-steps asset_path: ./build/DokodemoDrag.app.zip asset_name: DokodemoDrag.app.zip asset_content_type: application/zip fastfile こちらも説明は直接コメントに記載しました。 fastfile default_platform(:mac) XCODE_PROJ_PATH = %w(./DokodemoDragLauncher/DokodemoDragLauncher.xcodeproj ./DokodemoDrag.xcodeproj) BUILD_PATH="build" APP_NAME="DokodemoDrag.app" KEYCHAIN_NAME="map_app_keychain" # macのtypo... OUTPUT_PATH="build" platform :mac do after_all do |lane| # GitHubホストランナーの場合は不要です。 # create_keychainが呼ばれずにdelete_keychainを呼ぶと # エラーになってしまいます。 delete_keychain(name: KEYCHAIN_NAME) end error do |lane| # GitHubホストランナーの場合は不要です。 # create_keychainが呼ばれずにdelete_keychainを呼ぶと # エラーになってしまいます。 delete_keychain(name: KEYCHAIN_NAME) end desc "release. option version: 0.3.0 ..." lane :release do |options| # プロジェクトにversion_numberを設定します。 set_version_number( version_number: options[:version_number] ) increment_all_app_build_numbers build end desc "set version number" lane :set_version_number do |options| # increment_version_numberだと主のschemeに対してのみ # 実行されるようなので、プロジェクト毎に設定しています。 XCODE_PROJ_PATH.each do |path| increment_version_number( xcodeproj: "#{path}", version_number: options[:version_number] ) end end desc "increment all app's build_numbers" lane :increment_all_app_build_numbers do # increment_xxx_numberだと主のschemeに対してのみ # 実行されるようなので、プロジェクト毎に設定しています。 XCODE_PROJ_PATH.each do |path| increment_build_number( xcodeproj: "#{path}" ) end end desc "build and zip app-file" lane :build do # certificateをimportしています。 # ここはciの時のみ実行するなど分岐があったほうが良さそうです。 import_my_certificate build_mac_app( workspace: "DokodemoDrag.xcworkspace", configuration: "Release", scheme: "DokodemoDrag", export_method: "development", clean: true, output_directory: BUILD_PATH, skip_package_pkg: true, output_name: APP_NAME ) zip( path: "#{BUILD_PATH}/#{APP_NAME}", output_path: "#{OUTPUT_PATH}/#{APP_NAME}.zip" ) end # ワークフローファイル側で設定しても問題ないと思いますが、fastlaneで設定しています。 # 参考: https://note.com/hayabusabusa/n/nbf4a1a095bf5 private_lane :import_my_certificate do create_keychain( name: KEYCHAIN_NAME, password: ENV["KEYCHAIN_PASSWORD"], timeout: 1800 ) import_certificate( certificate_path: ENV["P12_PATH"], certificate_password: ENV["P12_PASSWORD"], keychain_name: KEYCHAIN_NAME, keychain_password: ENV["KEYCHAIN_PASSWORD"] ) end end 参考資料 fastfileとワークフローファイルを作成時に参考したページです。 上の内容と一部重複しています。 GitHub Actions GitHub Actionsについて学ぶ GitHub Actionsの概要を把握するために一読をお勧めします。 Xcode 開発用の macOS ランナーに Apple 証明書をインストールする Github Actions上でCertificateのインストールする手順が記載されています。 今回はp12ファイルをGithub Actions上で生成するところを参考にし、keychainへの証明書のimportは、fastlaneを利用しています。 GitHub Actionsのメタデータ構文 アクションの入力パラメータの設定方法等が記載されています。 今回はworkflowを手動実行する際入力パラメータの設定の参考にしています。 GitHub Actionsのワークフロー構文 GitHub Actionsのworkflowのyamlファイルのリファレンスです。 ワークフローをトリガーするイベント pullreqのタイミングやbranchがpushされた時などをtriggerとしてworkflowを起動できます。 手動でworkflowを実行する設定(on: workflow_dispatch)も記載されています。 環境変数 GitHub Actionsで利用可能な環境変数についての説明。 ワークフローの定義ファイル中には環境変数とコンテキストの2種類の変数がありますが、その違いについても 記載されています。 暗号化されたシークレット パスワードやp12ファイルなどの機密情報の設定方法およびワークフローファイルからの参照方法について記載されています。 シークレットの設定方法(リポジトリ単位) ワークフローファイいるからのシークレットの参照方法 セルフホストランナーについて 通常ワークフローは、GitHub管理下で実行されますが、独自のワークフローのランナー環境が構築できるようです。 ドキュメント内では、GitHub管理下のランナーをGitHubホストランナー、独自のランナーをセルフホストランナーと表記されています。 Action actions/checkout Gitのリポジトリをcheckoutするアクション。 デフォルトでは、workflowを実行したbranchを参照するようです。 actions/create-release GitHubのreleaseページを作成するために利用したのですが、 このリポジトリはarchivedされていてメンテナンスされていない模様。 actions/upload-release-asset GitHubのリリースページに成果物をアップロードするアクションです。 Fastlane increment_build_number increment_build_numberは、デフォルトだと1projectにしか適用されないので increment_build_number( build_number: 75, # specify specific build number (optional, omitting it increments by one) xcodeproj: "./path/to/MyApp.xcodeproj" # (optional, you must specify the path to your main Xcode project if it is not in the project root directory) ) のようにxcodeprojを明示しています。 increment_version_number workflow実行時に入力したバージョンを設定しています。 increment_version_numberは、デフォルトだと1projectのみ適用なので、全プロジェクトに 適用するようにしています。 create_keychain delete_keychain 今回の設定では、お行儀よくfastlaneの最後にkeychainを削除しました。 GitHubホストランナー上で実行する場合、この設定は不要です。 参考:Xcode 開発用の macOS ランナーに Apple 証明書をインストールする Technical Q&A QA1827 Automating Version and Build Numbers Using agvtool Github Actionsとは直接関係ありませんが、fastlaneのincrement_build_no, increment_version_numberは、Appleが提供しているagvtoolに依存しています。 またそのための設定をXcode のプロジェクトに行う必要があるため、その手順が記載されています。 注意点として、同一ディレクトリにxcodeprojが複数あるとagvtoolは動作しません。 (私の場合、過去にproject名をリネームしていてその残骸のxcodeprojが残っていたため、知らぬ間に同一ディレクトリに複数のxcodeprojが存在していて、agvtoolが実行できないというポカをしていました) 先人の知見 GitHub ActionsでReleaseを自動化する方法としたときに得た学び Github Actions と fastlane で iOS アプリをアーカイブしてビルドを提出する Github ActionsでiOSアプリのデプロイを自動化する 以上です。分かってしまえば1つ1つの設定は単純ですが、Actionsのデバッグに想像以上の時間がかかりました。 デバッグ時にはワークフロー中のcommitを省略するなどの仕組みが必要だったなぁとか、キャッシュは先に有効にしておくべきだったとか...改善の余地は多々あります。
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

MacのDocker Desktopを利用せずにLaravel 8.xのsailコマンドを実行できるようにする

Docker Desktopが有料化されることでDocker Desktopを利用しない選択肢を迫られるケースが出てくると思います。 Docker Desktopが有料化へ 従業員数250人以下・年間売り上げ1000万ドル以下の組織などは引き続き無料 - ITmedia NEWS https://www.itmedia.co.jp/news/articles/2109/02/news112.html Docker is Updating and Extending Our Product Subscriptions - Docker Blog https://www.docker.com/blog/updating-product-subscriptions/ Docker Desktopを利用しない方法にはいくつか選択肢がありますがLaravel 8.xのsailコマンドを利用する場合、選択肢が狭まってしまうのがわかったのでメモ。 結論 sailコマンドはDockerデーモンの起動確認をしてて、Lima + nerdctlはだめだったので、docker + docker-machine + docker-composeで!(2021/09/06時点) インストール手順 # Docker Desktopのアンインストール # GUIからアンインストールする # ※brew caskでインストールしているなら # brew uninsutall docker # brewでvirtualbox、docker cli、docker-machine、docker-composeのインストール $ brew install virtualbox docker docker-machine docker-compose # インストール確認 # アプリが立ち上がったらOK $ virtualbox $ docker -v $ docker-machine -v $ docker-compose -v インストールできたらVirtualBoxでVMインスタンス作成します。 # VirtualBoxでVMインスタンス作成 $ docker-machine create --driver virtualbox default # 環境変数を.bash_profileに設定 # zsh、fishの場合は任意のファイルに $ echo "$(docker-machine env default)" >> .bash_profile . .bash_profile 'docker.credentials.errors.InitializationError: docker-credential-desktop not installed or not available in PATH' エラーが出る場合以下のコマンドで設定ファイルを削除します。 $ rm ~/.docker/config.json 動作確認 Laravelのプロジェクトを新規作成してSailコマンドが実行できるか確認します。 $ cd 任意のディレクトリ # hogeは任意のプロジェクト名 $ curl -s https://laravel.build/hoge | bash $ cd hoge && ./vendor/bin/sail up プロジェクト作成できたらdocker-compose.ymlを編集してextra_hosts: 定義を無効化します。 これはERROR: for hoge_laravel.test_1 Cannot create container for service laravel.test: invalid IP address in add-host: "host-gateway" を回避するためです。 yaml services: laravel.test: build: context: ./vendor/laravel/sail/runtimes/8.0 dockerfile: Dockerfile args: WWWGROUP: '${WWWGROUP}' image: sail-8.0/app # extra_hosts: # - 'host.docker.internal:host-gateway' ports: - '${APP_PORT:-80}:80' $ cd hoge $ ./vendor/bin/sail up -d Creating network "hoge_sail" with driver "bridge" Creating hoge_selenium_1 ... done Creating hoge_mailhog_1 ... done Creating hoge_meilisearch_1 ... done Creating hoge_redis_1 ... done Creating hoge_mysql_1 ... done Creating hoge_laravel.test_1 ... done いえぃ! 、、、かと思いきやこのままホストからhttp://localhost/ にアクセスできません。Docker Machineを利用しているのでVMのIPアドレスをdocker-machine ip で取得する必要があります。 $ docker-machine ip <取得できたIPアドレス> で、http://<取得できたIPアドレス> にブラウザでアクセスします。 oh... 権限エラー 必要な権限を付与します。 $ ./vendor/bin/sail exec laravel.test bash # chmod -R guo+w storage で、http://<取得できたIPアドレス> にブラウザでアクセスします。 やってやったぜー ただ、動作確認は不十分なので、このあといろいろと不具合は出てくると思います。。。辛い あとがき Docker Desktopを利用しなくても起動できるにはできますが、いろいろと、とてもとても、、、とても手間がかかってしまうので、素直に所属する企業に状況を説明してお金を払ってもらうようにお願いするのが一番ストレスがないなぁって思いましたマル 参考 containerd & Lima: Open source alternative to Docker for Mac | by Akihiro Suda | nttlabs | Sep, 2021 | Medium https://medium.com/nttlabs/containerd-and-lima-39e0b64d2a59 lima-vm/lima: Linux virtual machines, on macOS (aka "Linux-on-Mac", "macOS subsystem for Linux", "containerd for Mac", unofficially) https://github.com/lima-vm/lima Docker Desktopの代替方法 - Windows and Mac編 https://zenn.dev/koduki/articles/ba54daaba28f93 macOSでDocker Desktopをアンインストールしてdocker-cli + docker-machineで動かすようにする - その手の平は尻もつかめるさ https://moznion.hatenadiary.com/entry/2021/09/01/112601 Get started with Docker Machine and a local VM | Docker Documentation https://docs.docker.com/machine/get-started/#run-containers-and-experiment-with-machine-commands docker-machine env | Docker ドキュメント https://matsuand.github.io/docs.docker.jp.onthefly/machine/reference/env/ Error with docker-compose: docker-credential-desktop not installed or not available in PATH · Issue #3785 · docker/for-mac https://github.com/docker/for-mac/issues/3785 docker-credential-desktop.exe not installed or not available in PATH https://sunday-morning.app/posts/2020-07-28-docker-credential-desktop-exe-not-installed-or-not-available-in-path What is linux equivalent of "host.docker.internal" - Stack Overflow https://stackoverflow.com/questions/48546124/what-is-linux-equivalent-of-host-docker-internal/61001152 DockerでLaravel+Apache+MySQLの開発環境を構築する - Qiita https://qiita.com/neneta0921/items/22f9864b6f6ff6d36004 Docker Machine をローカル VM で始めるには — Docker-docs-ja 1.11.0 ドキュメント http://docs.docker.jp/v1.11/machine/get-started.html docker-machine で localhost でポートを公開する - Qiita https://qiita.com/superbrothers/items/9505d6ebab6b10a585dd
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

【Mac】Docker+rails6+postgreSQLでの環境構築完全版

環境 MacOS Big Sur 11.5.2 ruby 3.0.2 rails 6.1.4 Docker 20.10.8 postgreSQL 13.4 ディレクトリ myapp 全て現時点(2021/09/06)での最新バージョンです。 1.用意するファイル ・Dockerfile ・Gemfile ・Gemfile.lock ・entrypoint.sh ・docker-compose.yml Dockerfile FROM ruby:3.0.2 # yarnパッケージ管理ツールをインストール RUN apt-get update && apt-get install -y curl apt-transport-https wget && \ curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && \ echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list && \ apt-get update && apt-get install -y yarn RUN apt-get update -qq && apt-get install -y nodejs postgresql-client RUN mkdir /myapp WORKDIR /myapp COPY Gemfile /myapp/Gemfile COPY Gemfile.lock /myapp/Gemfile.lock RUN bundle install COPY . /myapp # Add a script to be executed every time the container starts. COPY entrypoint.sh /usr/bin/ RUN chmod +x /usr/bin/entrypoint.sh ENTRYPOINT ["entrypoint.sh"] EXPOSE 3000 # Start the main process. CMD ["rails", "server", "-b", "0.0.0.0"] ここでyarnをインストールしておくのがポイントです。しないと後でwebpackerがインストールできずエラーになります。 Gemfile source 'https://rubygems.org' gem 'rails', '~>6' Gemfile.lockは空のままで大丈夫です entrypoint.sh #!/bin/bash set -e # Remove a potentially pre-existing server.pid for Rails. rm -f /myapp/tmp/pids/server.pid # Then exec the container's main process (what's set as CMD in the Dockerfile). exec "$@" docker-compose.yml version: '3' services: db: image: postgres volumes: - ./tmp/db:/var/lib/postgresql/data environment: - POSTGRES_PASSWORD=password web: build: . command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'" volumes: - .:/myapp ports: - "3000:3000" depends_on: - db 2.実行 $ docker-compose run web rails new . --force --no-deps --database=postgresql ↓ $ docker-compose build ここでdatabase.ymlを編集します config/database.yml default: &default adapter: postgresql encoding: unicode host: db username: postgres password: password pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> development: <<: *default database: app_development test: <<: *default database: app_test production: <<: *default database: app_production username: app password: <%= ENV['APP_DATABASE_PASSWORD'] %> パスワードなど設定した後データベースと接続します。 $ docker-compose run web rake db:create ↓ $ docker-compose up 終わったらlocalhost:3000とネットで検索すればこの画面が出てきます。 そしたら環境構築成功です! おわりに 【Mac】Docker+rails6+MySQLでの環境構築完全版 MySQLバージョンも作ったのでぜひ参考にしてください!
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

【Mac】Docker+rails6+MySQLでの環境構築完全版

環境 MacOS Big Sur 11.5.2 ruby 3.0.2 rails 6.1.4 Docker 20.10.8 MySQL 8.0.23 ディレクトリ myapp 全て現時点(2021/09/06)での最新バージョンです。 1.用意するファイル ・Dockerfile ・Gemfile ・Gemfile.lock ・entrypoint.sh ・docker-compose.yml Dockerfile FROM ruby:3.0.2 RUN apt-get update -qq && apt-get install -y nodejs # yarnパッケージ管理ツールをインストール # https://classic.yarnpkg.com/en/docs/install/#debian-stable RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list RUN apt-get update && apt-get install yarn WORKDIR /myapp COPY Gemfile /myapp/Gemfile COPY Gemfile.lock /myapp/Gemfile.lock RUN bundle install COPY . /myapp # Add a script to be executed every time the container starts COPY entrypoint.sh /usr/bin/ RUN chmod +x /usr/bin/entrypoint.sh ENTRYPOINT ["entrypoint.sh"] EXPOSE 3000 # Start the main process. CMD ["rails", "server", "-b", "0.0.0.0"] ここでyarnをインストールしておくのがポイントです。しないと後でwebpackerがインストールできずエラーになります。 Gemfile source 'https://rubygems.org' gem 'rails', '~>6' Gemfile.lockは空のままで大丈夫です entrypoint.sh #!/bin/bash set -e # Remove a potentially pre-existing server.pid for Rails. rm -f /myapp/tmp/pids/server.pid # Then exec the container's main process (what's set as CMD in the Dockerfile). exec "$@" docker-compose.yml version: "3" services: db: image: mysql:8.0 command: mysqld --default-authentication-plugin=mysql_native_password environment: MYSQL_USER: user MYSQL_PASSWORD: password MYSQL_ROOT_PASSWORD: password ports: - 3306:3306 volumes: - ./tmp/db:/var/lib/mysql web: build: . command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'" environment: MYSQL_HOST: db volumes: - .:/myapp ports: - "3000:3000" depends_on: - db 2.実行 $ docker-compose run web rails new . --force --no-deps --database=mysql ↓ $ docker-compose build ↓ ここでdatabase.ymlを編集します config/database.yml default: &default adapter: mysql2 encoding: utf8mb4 pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> username: root password: <%= ENV.fetch("MYSQL_ROOT_PASSWORD", "root") %> host: db development: <<: *default database: myapp_development test: <<: *default database: myapp_test production: <<: *default database: myapp_production username: myapp password: <%= ENV['MYAPP_DATABASE_PASSWORD'] %> パスワードなど設定した後データベースと接続します。 $ docker-compose run web rake db:create ↓ $ docker-compose up 終わったらlocalhost:3000とネットで検索すればこの画面が出てきます。 そしたら環境構築成功です! おわりに 未経験でしかも初心者なのでかなり苦労しましたが必要な情報は以上だけでした 一人でも多くの方の役に立てば嬉しいです 【Mac】Docker+rails6+postgreSQLでの環境構築完全版 PostgreSQLバージョンも作ったのでよかったら参考にしてください!
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

M1 Mac(Big Sur)でC++の競プロ環境を作る

最近メインのPCをM1 Macにしたのですが、C++で競プロをやる際に、コンパイルあたりで躓いたので記事にしました。 この記事は以下の2つの記事・サイトをBig Surに対応させただけの記事です 環境 macOS Big Sur g++ 11 Apple Silicon 目標 bits/stdc++.hを使いたい デバッグとかは使わない 解決したいこと macOSに標準搭載のC++用コンパイラはClang系らしい。 目標の通り、bits/stdc++.hを使いたいので、コンパイラをgcc系に切り替える 1.確認 $ which g++ /usr/bin/g++ 何もしてないから、Clangが出てくる 2.gccのインストール $ brew install gcc 3.PATH 上記の記事では/usr/local/bin/にgccがあると書いている。 が、Apple SiliconのArm64では、homebrewのインストール先は/opt/homebrew配下になるので、/opt/homebrew/binにgccがある。 ここからシンボリックリンクを貼る $ ln -s /opt/homebrew/bin/g++-11 /usr/local/bin/g++ これでOK。確認してみる $ which g++ /usr/local/bin/g++ 4. Visual Studio Code このままだとVScodeでbits/stdc++.hをincludeしようとすると警告が出てうざいので治す。 上記の記事の通り、/opt/homebrew/Celler/gcc/11.2.0/include/c++/11.1.0/aarch64-apple-darwin20/bits/stdc++.hを/opt/homebrew/include以下に配置する。 $ cd /opt/homebrew $ mkdir ./include/bits $ cp ./Celler/gcc/11.2.0/include/c++/11.1.0/aarch64-apple-darwin20/bits/stdc++.h ./include/bits Intellisenseにbits/stdc++.hを認識させる c_cpp_properties.json { "configurations": [ { "name": "Mac", "includePath": [ "${workspaceFolder}/**", "/opt/homebrew/include/**" ], "defines": [], "macFrameworkPath": [ "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks" ], "compilerPath": "/usr/local/bin/g++", "cStandard": "c17", "cppStandard": "c++14", "intelliSenseMode": "macos-gcc-arm64" } ], "version": 4 } 終わり
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

C++の競プロ環境を作る M1 Mac(Big Sur)

最近メインのPCをM1 Macにしたのですが、C++で競プロをやる際に、コンパイルあたりで躓いたので記事にしました。 この記事は以下の2つの記事・サイトをBig Surに対応させただけの記事です 環境 macOS Big Sur g++ 11 Apple Silicon 目標 bits/stdc++.hを使いたい デバッグとかは使わない 解決したいこと macOSに標準搭載のC++用コンパイラはClang系らしい。 目標の通り、bits/stdc++.hを使いたいので、コンパイラをgcc系に切り替える 1.確認 $ which g++ /usr/bin/g++ 何もしてないから、Clangが出てくる 2.gccのインストール $ brew install gcc 3.PATH 上記の記事では/usr/local/bin/にgccがあると書いている。 が、Apple SiliconのArm64では、homebrewのインストール先は/opt/homebrew配下になるので、/opt/homebrew/binにgccがある。 ここからシンボリックリンクを貼る $ ln -s /opt/homebrew/bin/g++-11 /usr/local/bin/g++ これでOK。確認してみる $ which g++ /usr/local/bin/g++ 4. Visual Studio Code このままだとVScodeでbits/stdc++.hをincludeしようとすると警告が出てうざいので治す。 上記の記事の通り、/opt/homebrew/Celler/gcc/11.2.0/include/c++/11.1.0/aarch64-apple-darwin20/bits/stdc++.hを/opt/homebrew/include以下に配置する。 $ cd /opt/homebrew $ mkdir ./include/bits $ cp ./Celler/gcc/11.2.0/include/c++/11.1.0/aarch64-apple-darwin20/bits/stdc++.h ./include/bits Intellisenseにbits/stdc++.hを認識させる c_cpp_properties.json { "configurations": [ { "name": "Mac", "includePath": [ "${workspaceFolder}/**", "/opt/homebrew/include/**" ], "defines": [], "macFrameworkPath": [ "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks" ], "compilerPath": "/usr/local/bin/g++", "cStandard": "c17", "cppStandard": "c++14", "intelliSenseMode": "macos-gcc-arm64" } ], "version": 4 } 終わり
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

M1 Mac(Big Sur)でC++の競プロ環境作る

最近メインのPCをM1 Macにしたのですが、C++で競プロをやる際に、コンパイルあたりで躓いたので記事にしました。 この記事は以下の2つの記事・サイトをBig Surに対応させただけの記事です 環境 macOS Big Sur g++ 11 Apple Silicon 目標 bits/stdc++.hを使いたい デバッグとかは使わない 解決したいこと macOSのC++用のコンパイラはClang系らしい。 目標の通り、bits/stdc++.hを使いたいので、コンパイラをgcc系に切り替える 1.確認 $ which g++ /usr/bin/g++ 何もしてないから、Clangが出てくる 2.gccのインストール $ brew install gcc 3.PATH 上記の記事では/usr/local/bin/にgccがあると書いている。 が、Apple SiliconのArm64では、homebrewのインストール先は/opt/homebrew配下になるので、/opt/homebrew/binにgccがある。 ここからシンボリックリンクを貼る $ ln -s /opt/homebrew/bin/g++-11 /usr/local/bin/g++ これでOK。確認してみる $ which g++ /usr/local/bin/g++ 4. Visual Studio Code このままだとVScodeでbits/stdc++.hをincludeしようとすると警告が出てうざいので治す。 上記の記事の通り、/opt/homebrew/Celler/gcc/11.2.0/include/c++/11.1.0/aarch64-apple-darwin20/bits/stdc++.hを/opt/homebrew/include以下に配置する。 $ cd /opt/homebrew $ mkdir ./include/bits $ cp ./Celler/gcc/11.2.0/include/c++/11.1.0/aarch64-apple-darwin20/bits/stdc++.h ./include/bits Intellisenseにbits/stdc++.hを認識させる c_cpp_properties.json { "configurations": [ { "name": "Mac", "includePath": [ "${workspaceFolder}/**", "/opt/homebrew/include/**" ], "defines": [], "macFrameworkPath": [ "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks" ], "compilerPath": "/usr/local/bin/g++", "cStandard": "c17", "cppStandard": "c++14", "intelliSenseMode": "macos-gcc-arm64" } ], "version": 4 } 終わり
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む