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

Secrets/deploy.yml Location & NPM Commands #29

Open
jasperf opened this issue Feb 17, 2022 · 14 comments
Open

Secrets/deploy.yml Location & NPM Commands #29

jasperf opened this issue Feb 17, 2022 · 14 comments

Comments

@jasperf
Copy link

jasperf commented Feb 17, 2022

When I use this action, can I keep deploy.yml:

import:
  - recipe/laravel.php
  - contrib/php-fpm.php
  - contrib/npm.php

config:
  application: 'smt-app'
  remote_user: forge
  deploy_path: '~/{{hostname}}'
  repository: 'git@github.com:site/site-app.git'
  php_fpm_version: '8.0'
  keep_releases: '10'
  shared_files: 
    - '.env'
    - '.transip_private_key'
    - 'storage/app/exact.api.json'
  shared_dirs:
    - 'bootstrap/cache'
    - 'public/uploads'
    - 'public/published'
    - 'storage/framework/cache'
    - 'storage/framework/sessions'
    - 'storage/framework/views'
    - 'storage/logs'
    - 'storage/tls'
    - 'storage/app/public'
    - 'storage/app/modules'
  writable_dirs:
    - 'public/uploads'
    - 'public/published'
    - 'storage/framework/cache/data'
    - 'storage/logs'
    - 'storage/tls'
    - 'storage/app/public'
    - 'storage/app/modules'

hosts:
  prod:
    hostname: 'site.com'
  staging:
    hostname: 'staging.site.com'

tasks:
  deploy:
    - deploy:prepare
    - deploy:vendors
    - artisan:storage:link
    - artisan:view:cache
    - artisan:config:cache
    - artisan:optimize
    - artisan:migrate
    - artisan:queue:restart    
    - artisan:horizon:terminate
    - deploy:publish

after:
  deploy:symlink: php-fpm:reload
  deploy:failed: deploy:unlock

in the root of the Laravel/Vue project?

Two, where do we add the secrets?

Three, will the action run npm install and npm run prod inside the image as well? I mean, I can add this to the deploy.yml but better to get that done in the image/ on Github and then deploy like we do now. Only now we build locally.

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • We receive the funding once the issue is completed & confirmed by you.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar
@jasperf jasperf changed the title Secrets Location & deploy.yml Secrets/deploy.yml Location & NPM Commands Feb 17, 2022
@jasperf
Copy link
Author

jasperf commented Feb 17, 2022

Perhaps I need to something like mentioned at #4

name: Deploy

on:
  # Run on pushes to `staging` branch only.
  push:
    paths-ignore:
      - '**.md'
    branches:
      - staging
      - '!main'
  # Allow manually triggering the workflow. Used for production deploy
  workflow_dispatch:

jobs:
  deploy:
    name: Deploy
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        php: [7.4]
        node: [12.x]

    steps:
      - name: Checkout the repo
        uses: actions/checkout@v2

      - name: PHP setup
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php }}
          coverage: none
          tools: composer:v2, deployer, wp-cli

      - name: Use Node.js ${{ matrix.node }}
        uses: actions/setup-node@v1
        with:
          node-version: ${{ matrix.node }}

      - name: Deploy to staging
        if: ${{ github.ref == 'refs/heads/staging' }}
        uses: deployphp/action@master
        with:
          private-key: ${{ secrets.STAGING_KEY }}
          dep: deploy ${{ secrets.ADMIN_USER }} ${{ secrets.ADMIN_EMAIL }} ${{ secrets.ADMIN_PASSWORD }} staging -v

      - name: Deploy to production
        if: ${{ github.ref == 'refs/heads/main' }}
        uses: deployphp/action@master
        with:
          private-key: ${{ secrets.PRODUCTION_KEY }}
          dep: deploy ${{ secrets.ADMIN_USER }} ${{ secrets.ADMIN_EMAIL }} ${{ secrets.ADMIN_PASSWORD }} production

Just not sure how {{ matrix.node }} and {{ secrets.ADMIN_PASSWORD }} would load. Also i do not need the password stuff I would think doing all with ssh keys so that part more like the README example:

name: deploy

on: push

# It is important to specify "concurrency" for the workflow,
# to prevent concurrency between different deploys.
concurrency: production_environment

jobs:
  deploy:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2

      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: '8.0'

      - name: Deploy
        uses: deployphp/action@v1
        with:
          private-key: ${{ secrets.PRIVATE_KEY }}
          dep: deploy

@antonmedv
Copy link
Member

Secret come from girl ha s had js ssh

@jasperf
Copy link
Author

jasperf commented Feb 17, 2022

@antonmedv Not quite certain what you mean here.. You mean that secrets.PRIVATE_KEY loads ssh key from Github settings?

Would be nice to have the README example action.yml updated with staging and master like the other user showed. Especially

steps:
  - name: Checkout the repo
    uses: actions/checkout@v2

  - name: PHP setup
    uses: shivammathur/setup-php@v2
    with:
      php-version: ${{ matrix.php }}
      coverage: none
      tools: composer:v2, deployer, wp-cli

  - name: Use Node.js ${{ matrix.node }}
    uses: actions/setup-node@v1
    with:
      node-version: ${{ matrix.node }}

Is useful, but again I wonder what matrix.node loads..? It loads Node from the setup-node here?

@antonmedv
Copy link
Member

Yes this can be loaded from the github secrets this is like a special field in settings with the hub

@antonmedv
Copy link
Member

And for simple environment variables can be also just need to assign environment variable from the stickers and of course go to settings of your repository and seconds down.

@jasperf
Copy link
Author

jasperf commented Feb 18, 2022

On using build matrices I found an explanation here https://docs.github.com/en/actions/using-jobs/using-a-build-matrix-for-your-jobs and example

strategy:
  matrix:
    node: [10, 12, 14]
steps:
  # Configures the node version used on GitHub-hosted runners
  - uses: actions/setup-node@v2
    with:
      # The Node.js version to configure
      node-version: ${{ matrix.node }}

Thanks to @antonmedv feedback and some more reading I am slowly starting to understand. Thanks Anton!

@jasperf
Copy link
Author

jasperf commented Feb 18, 2022

Got this now

name: deploy

on:
  # Trigger the workflow on push or pull request,
  # but only for the master branch
  # https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions
  push:
    branches:
      - master
  pull_request:
    branches:
      - master

concurrency: production_environment

# on: push

# It is important to specify "concurrency" for the workflow,
# to prevent concurrency between different deploys.
# concurrency: production_environment

jobs:
  php-cs-fixer:
    runs-on: ubuntu-latest

    steps:
        -   name: Checkout code
            uses: actions/checkout@v2
            with:
                ref: ${{ github.head_ref }}

        -   name: Run PHP CS Fixer
            uses: docker://oskarstark/php-cs-fixer-ga
            with:
                args: --config=.php-cs-fixer.dist.php --allow-risky=yes

        -   name: Commit changes
            uses: stefanzweifel/git-auto-commit-action@v4
            with:
                commit_message: Fix styling
  deploy:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        php: [8.1]
        node: [14.x]

    steps:
      - name: Checkout the repo
        uses: actions/checkout@v2

      - name: PHP setup
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php }}
          coverage: none
          tools: composer:v2, deployer
          
      - name: Use Node.js ${{ matrix.node }}
        uses: actions/setup-node@v1
        with:
          node-version: ${{ matrix.node }}
        run: npm install
        run: npm run prod

      - name: Deploy
        uses: deployphp/action@v1
        with:
          # Private key for connecting to remote hosts. To generate private key:
          # `ssh-keygen -o -t rsa -C 'action@deployer.org'`.
          # Required.
          private-key: ${{ secrets.PRIVATE_KEY }}
          dep: deploy

But hitting

Annotations
1 error
Invalid workflow file: .github/workflows/ci.yml#L64
The workflow is not valid. .github/workflows/ci.yml (Line: 64, Col: 9): Unexpected value 'run' .github/workflows/ci.yml (Line: 65, Col: 9): 'run' is already defined

and it ran right away, which I did not really want... perhaps I need to change the branch as well.

@antonmedv
Copy link
Member

Looks yaml file has some different from different spaces instant before run

@jasperf
Copy link
Author

jasperf commented Feb 27, 2022

@antonmedv was true on some of the spacing but running npm run prod I still cannot manage. May need to define command here like we can in deploy.yml but not sure yet how. Now have

name: deploy

on:
  # Run on pushes to `staging` branch only.
  push:
    paths-ignore:
      - '**.md'
    branches:
      - staging
      - '!main'
  # Allow manually triggering the workflow. Used for production deploy
  workflow_dispatch:

concurrency: production_environment

# on: push

# It is important to specify "concurrency" for the workflow,
# to prevent concurrency between different deploys.
# concurrency: production_environment

jobs:
  deploy:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        php: [8.1]
        node: [14.x]

    steps:
      - name: Checkout the repo
        uses: actions/checkout@v2

      - name: PHP setup
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php }}
          coverage: none
          tools: composer:v2, deployer
          
      - name: Use Node.js ${{ matrix.node }}
        uses: actions/setup-node@v1
        with:
          node-version: ${{ matrix.node }}
        run: npm install
        # run: npm run prod
        # npm:run:prod:
        # - run: 'cd {{release_path}} && npm run prod'

      - name: Deploy to staging
        if: ${{ github.ref == 'refs/heads/staging' }}
        uses: deployphp/action@master
        with:
          # Private key for connecting to remote hosts. To generate private key:
          # `ssh-keygen -o -t rsa -C 'action@deployer.org'`.
          # Required.
          private-key: ${{ secrets.PRIVATE_KEY }}
          dep: deploy


      - name: Deploy to production
        if: ${{ github.ref == 'refs/heads/master' }}
        uses: deployphp/action@master
        with:
          # Private key for connecting to remote hosts. To generate private key:
          # `ssh-keygen -o -t rsa -C 'action@deployer.org'`.
          # Required.
          private-key: ${{ secrets.PRIVATE_KEY }}
          dep: deploy

@jasperf
Copy link
Author

jasperf commented Feb 27, 2022

Checking https://gist.github.com/cagartner/27052b0bccb99c74d8616a943426b390 I might need

- run: npm install
- run: npm run prod
# npm:run:prod:
# - run: 'cd {{release_path}} && npm run prod'

but wonder if I need to change path inside the image and if all this then adds assets to repository. Not sure yet.

@jasperf
Copy link
Author

jasperf commented Feb 27, 2022

Well, perhaps I need to use this block

- name: Commit built assets
  run: |
    git config --local user.email "action@github.com"
    git config --local user.name "GitHub Action"
    git checkout -B deploy
    git add -f public/
    git commit -m "Build front-end assets"
    git push -f origin deploy

in my ci.yml action as well:

name: deploy

on:
  # Run on pushes to `staging` branch only.
  push:
    paths-ignore:
      - '**.md'
    branches:
      - staging
      - '!main'
  # Allow manually triggering the workflow. Used for production deploy
  workflow_dispatch:

concurrency: production_environment

# on: push

# It is important to specify "concurrency" for the workflow,
# to prevent concurrency between different deploys.
# concurrency: production_environment

jobs:
  deploy:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        php: [8.1]
        node: [14.x]

    steps:
      - name: Checkout the repo
        uses: actions/checkout@v2

      - name: PHP setup
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php }}
          coverage: none
          tools: composer:v2, deployer
          
      - name: Use Node.js ${{ matrix.node }}
        uses: actions/setup-node@v1
        with:
          node-version: ${{ matrix.node }}
      - run: npm install
      - run: npm run production
      - name: Commit built assets
        run: |
          git config --local user.email "action@github.com"
          git config --local user.name "GitHub Action"
          git checkout -B deploy
          git add -f public/
          git commit -m "Build front-end assets"
          git push -f origin deploy

      - name: Deploy to staging
        if: ${{ github.ref == 'refs/heads/staging' }}
        uses: deployphp/action@master
        with:
          # Private key for connecting to remote hosts. To generate private key:
          # `ssh-keygen -o -t rsa -C 'action@deployer.org'`.
          # Required.
          private-key: ${{ secrets.PRIVATE_KEY }}
          dep: deploy


      - name: Deploy to production
        if: ${{ github.ref == 'refs/heads/master' }}
        uses: deployphp/action@master
        with:
          # Private key for connecting to remote hosts. To generate private key:
          # `ssh-keygen -o -t rsa -C 'action@deployer.org'`.
          # Required.
          private-key: ${{ secrets.PRIVATE_KEY }}
          dep: deploy

Question remains though... if I build these assets on the image/github do these github acctions including push also happen on Github?

@antonmedv Do you build locally? Or do you add it to deploy.yml and bujld npm assets on the server?

@antonmedv
Copy link
Member

Yes you can simply Luther and then upload it to destination this is what I do and you can do it both ways I prefer to do from the plural

@jasperf
Copy link
Author

jasperf commented Feb 27, 2022

@antonmedv think you made typo there. What do you mean by "Luther then upload it to destination"? You build on Github using a Github action like I am suggesting here? I mean like:

  • locally edit/add files
  • push to Github repository
  • run Github action to set up image
  • run action to npm install and npm run production
  • commit on same image / repository
  • deploy to server
  • run deploy.yml there including:
    • deploy:prepare
    • deploy:vendors
    • artisan:storage:link
    • artisan:view:cache
    • artisan:config:cache
    • artisan:optimize
    • artisan:migrate
    • artisan:queue:restart
    • artisan:horizon:terminate
    • deploy:publish

Or do you do more on Github action?

deploy.yml now with npm run prod included and ci.yml not in use:

import:
  - recipe/laravel.php
  - contrib/php-fpm.php
  - contrib/npm.php

config:
  application: 'our-app'
  remote_user: forge
  deploy_path: '~/{{hostname}}'
  repository: 'git@github.com:app/our-app.git'
  php_fpm_version: '8.0'
  keep_releases: '10'
  shared_files: 
    - '.env'
    - '.transip_private_key'
    - 'storage/app/exact.api.json'
  shared_dirs:
    - 'bootstrap/cache'
    - 'public/uploads'
    - 'public/published'
    - 'storage/framework/cache'
    - 'storage/framework/sessions'
    - 'storage/framework/views'
    - 'storage/logs'
    - 'storage/tls'
    - 'storage/app/downloads'
    - 'storage/app/modules'
    - 'storage/app/public'
  writable_dirs:
    - 'public/uploads'
    - 'public/published'
    - 'storage/framework/cache/data'
    - 'storage/logs'
    - 'storage/tls'
    - 'storage/app/downloads'
    - 'storage/app/modules'
    - 'storage/app/public'

hosts:
  prod:
    hostname: 'site.com'
  staging:
    hostname: 'staging.site.com'

tasks:
  deploy:
    - deploy:prepare
    - deploy:vendors
    - artisan:storage:link
    - artisan:view:cache
    - artisan:config:cache
    - artisan:optimize
    - artisan:migrate
    - npm:install
    - npm:run:prod
    - artisan:queue:restart    
    - artisan:horizon:terminate
    - deploy:publish
  npm:run:prod:
    - run: 'cd {{release_path}} && npm run prod'

after:
  deploy:symlink: php-fpm:reload
  deploy:failed: deploy:unlock

@jasperf
Copy link
Author

jasperf commented Apr 22, 2022

Found some code at https://gist.github.com/cagartner/27052b0bccb99c74d8616a943426b390 for building including npm and creating a temporary commit. Can then add your deployment action after I think:

name: CD

on:
  push:
    branches: [ production ]

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
      with:
        token: ${{ secrets.PUSH_TOKEN }}
    - name: Set up Node
      uses: actions/setup-node@v1
      with:
        node-version: '12.x'
    - run: npm install
    - run: npm run production
    - name: Commit built assets
      run: |
        git config --local user.email "action@github.com"
        git config --local user.name "GitHub Action"
        git checkout -B deploy
        git add -f public/
        git commit -m "Build front-end assets"
        git push -f origin deploy
    - name: Deploy
        uses: deployphp/action@v1
        with:
          # Private key for connecting to remote hosts. To generate private key:
          # `ssh-keygen -o -t rsa -C 'action@deployer.org'`.
          # Required.
          private-key: ${{ secrets.PRIVATE_KEY }}
          dep: deploy

Well it is at least closer again. Just do not follow git add -f public/... Do see it at https://github.com/henrylemmon/laravel-8-fortify-auth-with-tests/blob/master/.github/workflows/main.yml as well though. And I could keep deploy.yml for general build details in the repository as well:

import:
  - recipe/laravel.php
  - contrib/php-fpm.php
  - contrib/npm.php

config:
  application: 'app-app'
  remote_user: forge
  deploy_path: '~/{{hostname}}'
  repository: 'git@github.com:app/app-app.git'
  php_fpm_version: '8.0'
  keep_releases: '10'
  shared_files: 
    - '.env'
    - '.transip_private_key'
    - 'storage/app/exact.api.json'
  shared_dirs:
    - 'bootstrap/cache'
    - 'public/uploads'
    - 'public/published'
    - 'public/images'
    - 'public/downloads'
    - 'storage/framework/cache'
    - 'storage/framework/sessions'
    - 'storage/framework/views'
    - 'storage/logs'
    - 'storage/tls'
    - 'storage/app/downloads'
    - 'storage/app/modules'
    - 'storage/app/public'
  writable_dirs:
    - 'public/uploads'
    - 'public/published'
    - 'storage/framework/cache/data'
    - 'storage/logs'
    - 'storage/tls'
    - 'storage/app/downloads'
    - 'storage/app/modules'
    - 'storage/app/public'

hosts:
  prod:
    hostname: 'app.com'
  staging:
    hostname: 'staging.app.com'

tasks:
  deploy:
    - deploy:prepare
    - deploy:vendors
    - artisan:storage:link
    - artisan:view:cache
    - artisan:config:cache
    - artisan:optimize
    - artisan:migrate
    # - npm:install
    # - npm:run:prod
    - artisan:queue:restart    
    - artisan:horizon:terminate
    - deploy:publish
  # npm:run:prod:
  #   - run: 'cd {{release_path}} && npm run prod'

after:
  deploy:symlink: php-fpm:reload
  deploy:failed: deploy:unlock

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

No branches or pull requests

2 participants