Merge pull request #684 from CodingBlackFemales/dependabot/composer/d… #1502
This file contains 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
# This workflow deploys the CBF WordPress application on a successful push or PR | |
name: CI | |
# Controls when the action will run. Triggers the workflow on push or pull request events | |
on: | |
push: | |
branches: [ develop, main ] | |
pull_request: | |
branches: [ develop, main ] | |
# A workflow run is made up of one or more jobs that can run sequentially or in parallel | |
jobs: | |
setup: | |
if: ${{ !contains(github.event.head_commit.message, 'chore(release)') }} | |
# The type of runner that the job will run on | |
runs-on: ubuntu-latest | |
# Steps represent a sequence of tasks that will be executed as part of the job | |
steps: | |
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it | |
- uses: actions/checkout@v4 | |
- name: Setup PHP | |
uses: shivammathur/setup-php@v2 | |
with: | |
php-version: 8.2 | |
- name: Cache Composer dependencies | |
uses: actions/cache@v4 | |
with: | |
path: /tmp/composer-cache | |
key: ${{ runner.os }}-${{ hashFiles('**/composer.lock') }} | |
- name: Install dependencies | |
uses: php-actions/composer@v6 | |
- name: Install phpcs | |
run: vendor/bin/phpcs -i | |
- name: PHP Code Style (phpcs) | |
run: composer lint:all | |
version: | |
if: ${{ github.ref == 'refs/heads/main' && github.event_name == 'push' && !contains(github.event.head_commit.author.name, 'GitHub Action') }} | |
needs: [ setup ] | |
runs-on: ubuntu-latest | |
outputs: | |
executed: ${{ steps.tag_release.outputs.executed }} | |
steps: | |
- name: Checkout repo | |
uses: actions/checkout@v4 | |
with: | |
token: ${{ secrets.ADMIN_GITHUB_TOKEN }} | |
- name: Setup Node.js | |
uses: actions/setup-node@v4 | |
with: | |
node-version: '20.x' | |
- name: Install GitHub CLI | |
run: | | |
type -p curl >/dev/null || (sudo apt update && sudo apt install curl -y) | |
curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg \ | |
&& sudo chmod go+r /usr/share/keyrings/githubcli-archive-keyring.gpg \ | |
&& echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null \ | |
&& sudo apt update \ | |
&& sudo apt install gh -y | |
- name: Create release branch and pull request | |
id: tag_release | |
env: | |
GH_TOKEN: ${{ secrets.ADMIN_GITHUB_TOKEN }} | |
run: | | |
git fetch --unshallow --tags | |
git config --global user.email "${{github.event.pusher.email}}" | |
git config --global user.name "${{github.event.pusher.name}}" | |
# Create a new branch for the release | |
git checkout -b release-$(date +'%Y%m%d%H%M%S') | |
# Run version bump and changelog update | |
npx --yes commit-and-tag-version | |
# Get the new version | |
new_version=$(git describe --tags --abbrev=0) | |
# Push the new branch and tag | |
git push --set-upstream origin HEAD | |
git push origin $new_version | |
# Create a pull request | |
gh pr create --title "Release $new_version" --body "This PR contains version bump and changelog updates for release $new_version" --base develop | |
echo 'executed=true' >> $GITHUB_OUTPUT | |
deploy: | |
# Ensure execution for stage deployments, as dependent version job is skipped | |
if: ${{ github.event_name == 'push' && !failure() && (success() || !needs.version.outputs.executed) }} | |
needs: [ setup, version ] | |
# The type of runner that the job will run on | |
runs-on: ubuntu-latest | |
# Steps represent a sequence of tasks that will be executed as part of the job | |
steps: | |
# Triggers release generation | |
- name: Deploy to hosting environment | |
env: | |
WORKFLOW_ID: ${{ 'deploy-application.yml' }} | |
uses: actions/github-script@v7 | |
with: | |
script: |- | |
await github.rest.actions.createWorkflowDispatch({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
workflow_id: '${{ env.WORKFLOW_ID }}', | |
ref: '${{ github.ref }}', | |
inputs: { | |
destinationEnvironment: '${{github.ref}}' === 'refs/heads/main' ? 'production' : 'staging', | |
sourceBranch: '${{ github.ref }}', | |
}, | |
}) | |
.catch(error => error) | |
.then(response => { | |
core.debug(response); | |
if (response.status !== 204) { | |
core.setFailed(`createWorkflowDispatch to ${{ env.WORKFLOW_ID }} received status code ${response.status}`) | |
} | |
}) |