pantheon-systems / circleci-orb

Use CircleCI to push code to Pantheon Dev and Multidev Environments
10 stars 18 forks source link

Run composer install #59

Closed maskedjellybean closed 3 years ago

maskedjellybean commented 3 years ago

Does anyone have an example of configuring CircleCI to run composer install after this orb has finished?

I was relying on Pantheon to run composer install by adding build_step: true to pantheon.yml or pantheon.upstream.yml, but realized it conflicts with my Quicksilver script to import config after the sync_code workflow. I believe that Pantheon runs composer install after sync_code, which means I can't use it to trigger a config import because all my dependencies are missing at that point. There is somehow no composer_install workflow to use as a trigger as far as I can tell.

maskedjellybean commented 3 years ago

Alright this is what I have so far. I thought it was working because all steps pass, but after requiring a new module, I can see that it does not exist on the server at /web/modules/contrib using SFTP. I based this mostly on the npm build example included in this repo. Any help would be greatly appreciated.

# PHP CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-php/ for more details
#
# This is customized to use the Pantheon CircleCI orb in order to automatically
# create a Pantheon Multidev environment for every Github PR that is created.
# See: https://github.com/pantheon-systems/circleci-orb
# @TODO: When we need sass > css compilation, see Overlake for example.
# If we need tests, consider replacing with: https://github.com/pantheon-systems/example-drops-8-composer
version: 2.1
workflows:
  version: 2
  build_and_push:
    jobs:
      - composer_install
      - pantheon/push:
          # This "requires" section tells CircleCI the order in which
          # jobs must be run.
          requires:
            - composer_install
          # Do not clone database and files from prod/live on every push.
          clone_content: false
          pre-steps:
            # Perform a git checkout of the code from GitHub so that
            # custom commands (the rm below) can alter the code before it is
            # pushed to Pantheon.
            - checkout
            # Attach this dist directory created in composer_install.
            - attach_workspace:
                at: .
            # The dist directories that hold the dependencies is git ignored.
            # It needs to be committed on Pantheon.
            # Removing this .gitignore file makes it available for committing.
            # Pantheon's Composer examples use a more complicated
            # technique of "cutting" the top level .gitignore
            # file so that lines specifying build artifact directories are removed.
            # https://github.com/pantheon-systems/example-drops-8-composer/blob/670ae310c601dabbb7b35411ff3e08e4b1fac7a3/composer.json#L67
            - run: rm .gitignore
          post-steps:
            - run:
                name: Reset Development Mode
                command: terminus connection:set ${TERMINUS_SITE}.${TERMINUS_ENV} git
orbs:
  pantheon: pantheon-systems/pantheon@0.5.2
jobs:
  composer_install:
    docker:
      # Using PHP 7.4 image described here: https://drupalize.me/tutorial/dockerize-existing-project
      # Thinking it may include extensions commonly required for Drupal.
      - image: osiolabs/drupaldevwithdocker-php:7.4
    steps:
      - checkout
      - run:
          name: Install composer dependencies
          no_output_timeout: 1h
          # Install missing PHP extensions and run composer install.
          # sed changes mirror for apt-get to avoid time out.
          # See: https://discuss.circleci.com/t/anyone-finding-build-speeds-to-be-slow-at-present/29457/3
          command: |
            sed -i 's#archive.ubuntu.com/ubuntu#ftp.heanet.ie/pub/ubuntu#g' /etc/apt/sources.list
            sed -i 's#security.ubuntu.com/ubuntu#ftp.heanet.ie/pub/ubuntu#g' /etc/apt/sources.list
            apt-get -q -y update
            apt-get -q -y install sudo
            sudo apt-get -q -y install php7.4-bcmath dialog apt-utils
            composer install --no-dev -n -o
      - persist_to_workspace:
          root: .
          paths:
            - '*'
maskedjellybean commented 3 years ago

I can see in the logs for "Copy code to the local clone of the Pantheon repository" that supposedly the files for the new module were copied. Yet they do not exist on the server.

maskedjellybean commented 3 years ago

YES! Got it. I was missing checkout: false. So composer install was running, but then afterward the code was being rechecked out, removing the dependencies that were just installed.

I also improved the way I am unignoring the dependency directories, inspired by the Pantheon composer drops example repo. Instead of removing the entire root .gitignore, I use sed to only remove the lines I need to.

.circleci/config.yml now looks like this:

# PHP CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-php/ for more details
#
# This is customized to use the Pantheon CircleCI orb in order to automatically
# create a Pantheon Multidev environment for every Github PR that is created.
# See: https://github.com/pantheon-systems/circleci-orb
# @TODO: When we need sass > css compilation, see Overlake for example.
# If we need tests, consider replacing with: https://github.com/pantheon-systems/example-drops-8-composer
version: 2.1
workflows:
  version: 2
  build_and_push:
    jobs:
      - composer_install
      - pantheon/push:
          # This "requires" section tells CircleCI the order in which
          # jobs must be run.
          requires:
            - composer_install
          # Because the checkout command is called from pre-steps, it should
          # not be run inside the orb-defined steps.
          checkout: false
          # Do not clone database and files from prod/live on every push.
          clone_content: false
          pre-steps:
            # Perform a git checkout of the code from GitHub so that
            # custom commands (the sed below) can alter the code before it is
            # pushed to Pantheon.
            - checkout
            # Attach this dist directory created in composer_install.
            - attach_workspace:
                at: .
            # The dist directories that hold the dependencies is git ignored.
            # It needs to be committed on Pantheon.
            # Based on Pantheon's Composer examples we use sed to cut the root .gitignore
            # file so that lines specifying build artifact directories are removed.
            # https://github.com/pantheon-systems/example-drops-8-composer/blob/83c0cb69fe098bf1d1eef7dd9d51833b70c9e97e/scripts/composer/ScriptHandler.php
            - run: sed -i '1,/^###### CIRCLECI CUT HERE$/d' .gitignore
          post-steps:
            - run:
                name: Reset Development Mode
                command: terminus connection:set ${TERMINUS_SITE}.${TERMINUS_ENV} git
orbs:
  pantheon: pantheon-systems/pantheon@0.5.2
jobs:
  composer_install:
    docker:
      # Using PHP 7.4 image described here: https://drupalize.me/tutorial/dockerize-existing-project
      # Thinking it may include extensions commonly required for Drupal.
      - image: osiolabs/drupaldevwithdocker-php:7.4
    steps:
      - checkout
      - run:
          name: Install composer dependencies
          no_output_timeout: 1h
          # Install missing PHP extensions and run composer install.
          # sed changes mirror for apt-get to avoid time out.
          # See: https://discuss.circleci.com/t/anyone-finding-build-speeds-to-be-slow-at-present/29457/3
          command: |
            sed -i 's#archive.ubuntu.com/ubuntu#ftp.heanet.ie/pub/ubuntu#g' /etc/apt/sources.list
            sed -i 's#security.ubuntu.com/ubuntu#ftp.heanet.ie/pub/ubuntu#g' /etc/apt/sources.list
            apt-get -q -y update
            apt-get -q -y install sudo
            sudo apt-get -q -y install php7.4-bcmath dialog apt-utils
            composer install --no-dev -o
      - persist_to_workspace:
          root: .
          paths:
            - drush/contrib
            - vendor
            - web/core
            - web/modules/contrib
            - web/themes/contrib
            - web/profiles/contrib
            - web/libraries

And in order for the sed command to cut only the Composer dependency directories from .gitignore, The top of my .gitignore looks like this:

# Ignore directories generated by Composer
/drush/contrib/
/vendor/
/web/core/
/web/modules/contrib/
/web/themes/contrib/
/web/profiles/contrib/
/web/libraries/
# NOTE: DO NOT add anything above this line except
# dependencies managed by Composer. CircleCI will
# remove everything above from this file during the build
# process in order to commit and push these directories to Pantheon.
# See: /.circleci/config.yml
# DO NOT modify the following line.
###### CIRCLECI CUT HERE