Run composer on Github Actions #48
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Deploy to DreamHost | |
| on: | |
| push: | |
| branches: | |
| - main | |
| - staging | |
| workflow_dispatch: | |
| inputs: | |
| environment: | |
| description: "Target environment" | |
| required: true | |
| default: "staging" | |
| type: choice | |
| options: | |
| - staging | |
| - production | |
| jobs: | |
| deploy: | |
| runs-on: ubuntu-latest | |
| environment: ${{ github.event_name == 'workflow_dispatch' && inputs.environment || (github.ref_name == 'main' && 'production' || 'staging') }} | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| - name: Set up PHP | |
| uses: shivammathur/setup-php@v2 | |
| with: | |
| php-version: "8.2" | |
| tools: composer | |
| coverage: none | |
| - name: Cache Composer dependencies | |
| id: composer-cache | |
| run: echo "dir=$(composer config cache-dir)" >> $GITHUB_OUTPUT | |
| - name: Cache Composer downloads | |
| uses: actions/cache@v4 | |
| with: | |
| path: ${{ steps.composer-cache.outputs.dir }} | |
| key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }} | |
| restore-keys: | | |
| ${{ runner.os }}-composer- | |
| - name: Set release metadata | |
| run: | | |
| echo "RELEASE_TS=$(date +%Y%m%d%H%M%S)" >> $GITHUB_ENV | |
| echo "BUILD_ID=$(git describe --always --dirty --tags)" >> $GITHUB_ENV | |
| - name: Set deploy target | |
| run: | | |
| if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then | |
| DEPLOY_ENV="${{ inputs.environment }}" | |
| elif [ "${{ github.ref_name }}" = "main" ]; then | |
| DEPLOY_ENV="production" | |
| else | |
| DEPLOY_ENV="staging" | |
| fi | |
| if [ "$DEPLOY_ENV" = "production" ]; then | |
| DEPLOY_PATH="${{ secrets.DEPLOY_PATH_PROD }}" | |
| else | |
| DEPLOY_PATH="${{ secrets.DEPLOY_PATH_STAGING }}" | |
| fi | |
| echo "DEPLOY_ENV=$DEPLOY_ENV" >> $GITHUB_ENV | |
| echo "DEPLOY_PATH=$DEPLOY_PATH" >> $GITHUB_ENV | |
| - name: Configure SSH | |
| run: | | |
| mkdir -p ~/.ssh | |
| echo "${{ secrets.DEPLOY_SSH_KEY }}" > ~/.ssh/id_rsa | |
| chmod 600 ~/.ssh/id_rsa | |
| ssh-keyscan -H "${{ secrets.DEPLOY_HOST }}" >> ~/.ssh/known_hosts | |
| - name: Prepare release directories | |
| run: | | |
| ssh "${{ secrets.DEPLOY_USER }}@${{ secrets.DEPLOY_HOST }}" \ | |
| "mkdir -p ${DEPLOY_PATH}/releases/${RELEASE_TS} ${DEPLOY_PATH}/shared/storage" | |
| - name: Rsync release | |
| run: | | |
| rsync -az --delete \ | |
| --exclude ".git" \ | |
| --exclude ".github" \ | |
| --exclude "storage" \ | |
| --exclude ".env" \ | |
| ./ "${{ secrets.DEPLOY_USER }}@${{ secrets.DEPLOY_HOST }}:${DEPLOY_PATH}/releases/${RELEASE_TS}/" | |
| - name: Install Composer dependencies | |
| run: | | |
| composer install --no-dev --optimize-autoloader --prefer-dist | |
| - name: Rsync vendor | |
| run: | | |
| rsync -az --delete \ | |
| ./vendor/ "${{ secrets.DEPLOY_USER }}@${{ secrets.DEPLOY_HOST }}:${DEPLOY_PATH}/releases/${RELEASE_TS}/vendor/" | |
| - name: Write shared env | |
| run: | | |
| ssh "${{ secrets.DEPLOY_USER }}@${{ secrets.DEPLOY_HOST }}" \ | |
| "cat > ${DEPLOY_PATH}/shared/.env <<'ENVFILE'\n${{ secrets.ENV_FILE }}\nENVFILE\nprintf '\nTHRIFTSTACK_BUILD_ID=%s\n' '${BUILD_ID}' >> ${DEPLOY_PATH}/shared/.env" | |
| - name: Link shared assets and switch release | |
| run: | | |
| ssh "${{ secrets.DEPLOY_USER }}@${{ secrets.DEPLOY_HOST }}" \ | |
| "ln -sfn ${DEPLOY_PATH}/shared/storage ${DEPLOY_PATH}/releases/${RELEASE_TS}/storage && \ | |
| ln -sfn ${DEPLOY_PATH}/shared/.env ${DEPLOY_PATH}/releases/${RELEASE_TS}/.env && \ | |
| php ${DEPLOY_PATH}/releases/${RELEASE_TS}/scripts/migrate.php && \ | |
| php ${DEPLOY_PATH}/releases/${RELEASE_TS}/scripts/seed.php && \ | |
| ln -sfn ${DEPLOY_PATH}/releases/${RELEASE_TS} ${DEPLOY_PATH}/current && \ | |
| bash ${DEPLOY_PATH}/releases/${RELEASE_TS}/scripts/deploy_helpers.sh cleanup ${DEPLOY_PATH}/releases 5" |