|
| 1 | +name: Deploy to Pantheon |
| 2 | + |
| 3 | +permissions: |
| 4 | + contents: write |
| 5 | + |
| 6 | +on: |
| 7 | + workflow_call: |
| 8 | + inputs: |
| 9 | + SITE_PATH: |
| 10 | + description: Destination filepath, must include trailing slash and no leading slash (default is empty string, or the / site root) |
| 11 | + default: '' |
| 12 | + type: string |
| 13 | + PANTHEON_SITE_ID: |
| 14 | + description: Human-readable site ID |
| 15 | + type: string |
| 16 | + required: true |
| 17 | + PANTHEON_STAGING_ENV_NAME: |
| 18 | + type: string |
| 19 | + default: "env-${{ github.sha }}" |
| 20 | + description: Name of the staging environment (11-char max) |
| 21 | + PANTHEON_DESTINATION: |
| 22 | + description: Target Pantheon environment, either 'dev' or 'staging' |
| 23 | + required: true |
| 24 | + type: string |
| 25 | + secrets: |
| 26 | + PANTHEON_SSH_KEY: |
| 27 | + required: true |
| 28 | + PANTHEON_KNOWN_HOSTS: |
| 29 | + required: true |
| 30 | + PANTHEON_USER_EMAIL: |
| 31 | + required: true |
| 32 | + PANTHEON_AUTH_USER: |
| 33 | + required: true |
| 34 | + PANTHEON_AUTH_PASSWORD: |
| 35 | + required: true |
| 36 | + PANTHEON_MACHINE_TOKEN: |
| 37 | + required: true |
| 38 | + |
| 39 | +jobs: |
| 40 | + deploy-to-pantheon: |
| 41 | + runs-on: ubuntu-latest |
| 42 | + steps: |
| 43 | + - uses: actions/checkout@v4 |
| 44 | + with: |
| 45 | + fetch-depth: 0 |
| 46 | + - name: Install SSH key |
| 47 | + uses: shimataro/ssh-key-action@v2 |
| 48 | + with: |
| 49 | + key: ${{ secrets.PANTHEON_SSH_KEY }} |
| 50 | + config: | |
| 51 | + Host *.drush.in |
| 52 | + StrictHostKeyChecking no |
| 53 | + known_hosts: ${{ secrets.PANTHEON_KNOWN_HOSTS }} |
| 54 | + - name: Setup PHP |
| 55 | + uses: shivammathur/setup-php@v2 |
| 56 | + with: |
| 57 | + php-version: "8.2" |
| 58 | + - name: Install Terminus |
| 59 | + run: | |
| 60 | + sudo apt update |
| 61 | + sudo apt install -y curl php-common php-cli php-xml php-mbstring php-curl git jq |
| 62 | + php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" |
| 63 | + php -r "if (hash_file('sha384', 'composer-setup.php') === file_get_contents('https://composer.github.io/installer.sig')) { echo 'Installer verified'.PHP_EOL; } else { echo 'Installer corrupt'.PHP_EOL; unlink('composer-setup.php'); exit(1); }" |
| 64 | + php composer-setup.php |
| 65 | + php -r "unlink('composer-setup.php');" |
| 66 | + mv composer.phar /usr/local/bin/composer |
| 67 | + curl -L https://github.com/pantheon-systems/terminus/releases/download/4.0.3/terminus.phar --output terminus |
| 68 | + chmod +x terminus |
| 69 | + mv terminus /usr/local/bin/terminus |
| 70 | + terminus self:update |
| 71 | + git config -l | grep 'http\..*\.extraheader' | cut -d= -f1 | xargs -L1 git config --unset-all |
| 72 | + git config --global user.email "${{ secrets.PANTHEON_USER_EMAIL }}" |
| 73 | + git config --global user.name "GitHub workflow" |
| 74 | + echo "PANTHEON_BRANCH=master" >> $GITHUB_ENV |
| 75 | + echo "PANTHEON_ENV=dev" >> $GITHUB_ENV |
| 76 | + - name: Retrieve build artifact |
| 77 | + uses: actions/download-artifact@v5 |
| 78 | + with: |
| 79 | + name: build-output |
| 80 | + path: ./build |
| 81 | + - name: Terminus login |
| 82 | + run: terminus auth:login --machine-token ${{ secrets.PANTHEON_MACHINE_TOKEN }} |
| 83 | + - name: Staging environment setup |
| 84 | + if: inputs.PANTHEON_DESTINATION == 'staging' |
| 85 | + run: | |
| 86 | + STAGING_NAME="${{ inputs.PANTHEON_STAGING_ENV_NAME }}" |
| 87 | + NORMLIZED_STAGING_ENV_NAME="${STAGING_NAME:0:11}" |
| 88 | + DEV_SITE_EXISTS="$(terminus env:list "${{ inputs.PANTHEON_SITE_ID }}" --format=list | grep "$NORMLIZED_STAGING_ENV_NAME" | wc -l | xargs)" |
| 89 | + if [ "$DEV_SITE_EXISTS" -eq "0" ]; then |
| 90 | + terminus multidev:create --no-interaction --no-ansi ${{ inputs.PANTHEON_SITE_ID }}.dev "$NORMLIZED_STAGING_ENV_NAME" |
| 91 | + terminus lock:enable ${{ inputs.PANTHEON_SITE_ID }}.$NORMLIZED_STAGING_ENV_NAME -- "${{ secrets.PANTHEON_AUTH_USER }}" "${{ secrets.PANTHEON_AUTH_PASSWORD }}" |
| 92 | + fi |
| 93 | + terminus connection:set "${{ inputs.PANTHEON_SITE_ID }}.$NORMLIZED_STAGING_ENV_NAME" git |
| 94 | + echo "PANTHEON_ENV=helpdocs" >> $GITHUB_ENV |
| 95 | + echo "PANTHEON_BRANCH=$NORMLIZED_STAGING_ENV_NAME" >> $GITHUB_ENV |
| 96 | + - name: Commit build and deploy to Pantheon repo |
| 97 | + run: | |
| 98 | + terminus local:clone --yes --branch="${{ env.PANTHEON_BRANCH }}" ${{ inputs.PANTHEON_SITE_ID }} |
| 99 | + rsync --archive ./build/ "$HOME/pantheon-local-copies/${{ inputs.PANTHEON_SITE_ID }}/${{ inputs.SITE_PATH }}" |
| 100 | + chmod -R 755 $HOME/pantheon-local-copies/${{ inputs.PANTHEON_SITE_ID }}/${{ inputs.SITE_PATH }} |
| 101 | + git -C "$HOME/pantheon-local-copies/${{ inputs.PANTHEON_SITE_ID }}" add . |
| 102 | + git -C "$HOME/pantheon-local-copies/${{ inputs.PANTHEON_SITE_ID }}" commit -m "Added content from ${{ github.repository }} at ${{ github.sha }}" |
| 103 | + WATCH_COMMIT=$(git -C "$HOME/pantheon-local-copies/${{ inputs.PANTHEON_SITE_ID }}" rev-parse --verify HEAD) |
| 104 | + git -C "$HOME/pantheon-local-copies/${{ inputs.PANTHEON_SITE_ID }}" push origin |
| 105 | + terminus workflow:wait --max 600 --commit $WATCH_COMMIT -- ${{ inputs.PANTHEON_SITE_ID }}.${{ env.PANTHEON_ENV }} |
| 106 | + - name: Terminus logout |
| 107 | + if: always() |
| 108 | + run: | |
| 109 | + rm -rf $HOME/pantheon-local-copies/${{ inputs.PANTHEON_SITE_ID }}/${{ inputs.SITE_PATH }} |
| 110 | + terminus auth:logout |
0 commit comments