LoginSignup
1
0

More than 3 years have passed since last update.

Herokuでデプロイ時にスクリプトを実行したい

Posted at

はじめに

Herokuでデプロイ時にスクリプトを実行したい!
具体的にやりたいことは、特定のディレクトリに権限を付加したい!

結論

ルートディレクトリに.profile1を作成し、その中にコマンドを書けばよい。

.profile
chmod -R 777 /app/web/app/ewww

経緯

こちらの記事2を参考にHerokuにWordPressを入れて、プラグインのEWWW Image Optimizerをインストールしたのですが、権限がないよってエラー3が出て、指定でのディレクトリに権限をつけようといろいろ奮闘したのが始まりでした。。。

エラー内容

EWWW Image Optimizer could not create the tool folder: /app/web/app/ewww/. Please adjust permissions or create the folder.

作戦1: composer.jsonのscriptsにスクリプト書く → 失敗

How can I give permissions to specific folders on heroku?

composer.json
{
...

  "scripts": {
    "compile": [
      "chmod -R 777 /app/web/app/ewww"
    ]
  }
}

logを見る限り、正常に権限は付加されていたが、デプロイ完了後にheroku run bashして直接確認すると権限は元通り。。。

log
-----> Installing dependencies...
       Composer version 1.10.13 2020-09-09 11:46:34
       > pwd
       /tmp/build_fbd6e5b3_
       > ls -la web/app/
       total 28
       drwx------ 7 u11818 dyno 4096 Sep 21 12:23 .
       drwx------ 3 u11818 dyno 4096 Sep 21 12:23 ..
       drwx------ 2 u11818 dyno 4096 Sep 21 12:23 ewww
       drwx------ 2 u11818 dyno 4096 Sep 21 12:23 mu-plugins
       drwx------ 2 u11818 dyno 4096 Sep 21 12:23 plugins
       drwx------ 4 u11818 dyno 4096 Sep 21 12:23 themes
       drwx------ 2 u11818 dyno 4096 Sep 21 12:23 uploads
       > chmod -R 777 web/app/ewww
       > ls -la web/app/
       total 28
       drwx------ 7 u11818 dyno 4096 Sep 21 12:23 .
       drwx------ 3 u11818 dyno 4096 Sep 21 12:23 ..
       drwxrwxrwx 2 u11818 dyno 4096 Sep 21 12:23 ewww
       drwx------ 2 u11818 dyno 4096 Sep 21 12:23 mu-plugins
       drwx------ 2 u11818 dyno 4096 Sep 21 12:23 plugins
       drwx------ 4 u11818 dyno 4096 Sep 21 12:23 themes
       drwx------ 2 u11818 dyno 4096 Sep 21 12:23 uploads

作戦2: Procfileにスクリプト書く → 失敗

herokuでデプロイ時に自動でスクリプトを実行する

Procfile
release: chmod -R 777 /app/web/app/ewww
web: vendor/bin/heroku-php-nginx -C config/heroku/nginx.conf web

logを見る限り、動いているっぽいが、デプロイ完了後にheroku run bashして直接確認すると権限は元通り。。。

log
-----> Discovering process types
       Procfile declares types -> release, web

作戦3: .profileにスクリプト書く → 成功!

.procfile
chmod -R 777 /app/web/app/ewww

おわりに

chmodに関してはこれでないとだめかもですが、composerに限らずパッケージ管理系のコマンドとかは作戦1とかでも動くと思います。しっかり調べてないのですが、、、。きっと、デプロイタイミングでdynoにファイル等々コピーされて権限等は戻っちゃうんではないでしょうか :thinking:

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