Update GH Actions #62
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
name: Testing Symfony with MySQL and deploy server | |
on: | |
push: | |
branches: [ "alpha" ] | |
pull_request: | |
branches: [ "alpha" ] | |
permissions: | |
contents: read | |
jobs: | |
validate: | |
name: Symfony (PHP ${{ matrix.php-versions }}) | |
runs-on: ubuntu-latest | |
# Docs: https://docs.github.com/en/actions/using-containerized-services | |
services: | |
mysql: | |
image: mysql:latest | |
env: | |
MYSQL_ALLOW_EMPTY_PASSWORD: false | |
MYSQL_ROOT_PASSWORD: symfony | |
MYSQL_DATABASE: symfony | |
ports: | |
- 3306/tcp | |
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3 | |
strategy: | |
fail-fast: false | |
matrix: | |
php-versions: ['8.1'] | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
# Docs: https://github.com/shivammathur/setup-php | |
- name: Setup PHP | |
uses: shivammathur/setup-php@v2 | |
with: | |
php-version: ${{ matrix.php-versions }} | |
tools: phpunit-bridge | |
extensions: mbstring, xml, ctype, iconv, intl, pdo_sqlite, mysql | |
coverage: xdebug | |
# Local MySQL service in GitHub hosted environments is disabled by default. | |
# If you are using it instead of service containers, make sure you start it. | |
# - name: Start mysql service | |
# run: sudo systemctl start mysql.service | |
- name: Copy .env.dist | |
# run: php -r "file_exists('.env.local') || copy('.env.${{ github.ref_name }}', '.env.local');" | |
run: php -r "file_exists('.env.local') || copy('.env.dist', '.env.local');" | |
- name: Get composer cache directory | |
id: composer-cache | |
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT | |
- name: Cache composer dependencies | |
uses: actions/cache@v4 | |
with: | |
path: ${{ steps.composer-cache.outputs.dir }} | |
# Use composer.json for key, if composer.lock is not committed. | |
# key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }} | |
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }} | |
restore-keys: ${{ runner.os }}-composer- | |
- name: Install Composer dependencies | |
if: steps.composer-cache.outputs.cache-hit != 'true' # Skip if cache hit | |
run: composer install --no-progress --prefer-dist --optimize-autoloader | |
# run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist | |
- name: Run Migration | |
run: | | |
composer require --dev symfony/orm-pack | |
php bin/console doctrine:schema:update --complete --force || echo "No migrations found or schema update failed" | |
php bin/console doctrine:migrations:migrate || echo "No migrations found or migration failed" | |
env: | |
DATABASE_URL: mysql://root:[email protected]:${{ job.services.mysql.ports['3306'] }}/symfony | |
# - name: Install PHPUnit | |
# run: simple-phpunit install | |
# | |
# - name: Run tests | |
# run: simple-phpunit --coverage-text | |
deploy: | |
name: Deploy image | |
runs-on: ubuntu-latest | |
needs: validate | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Login to registry | |
uses: docker/login-action@v3 | |
with: | |
registry: ${{ secrets.DOCKER_REGISTRY }} | |
username: ${{ secrets.DOCKER_LOGIN }} | |
password: ${{ secrets.DOCKER_PASSWD }} | |
- name: Build and push Docker image | |
uses: docker/build-push-action@v5 | |
with: | |
context: . | |
push: true | |
tags: registry.realt.community/symfony-php-api-realt:${{ github.ref_name }} | |
- name: Configure SSH | |
run: | | |
if "${{ github.ref_name }}" == "alpha" | |
then | |
echo "alpha-branch" | |
else | |
echo "not alpha-branch" | |
fi | |
mkdir -p ~/.ssh/ | |
echo "$SSH_KEY" > ~/.ssh/staging.key | |
chmod 600 ~/.ssh/staging.key | |
cat >>~/.ssh/config <<END | |
Host staging | |
HostName $SSH_HOST | |
User $SSH_USER | |
Port $SSH_PORT | |
IdentityFile ~/.ssh/staging.key | |
StrictHostKeyChecking no | |
END | |
env: | |
SSH_USER: ${{ secrets.SSH_USER }} | |
SSH_KEY: ${{ secrets.SSH_KEY }} | |
SSH_HOST: ${{ secrets.SSH_HOST }} | |
SSH_PORT: ${{ secrets.SSH_PORT }} | |
- name: Update alpha environment | |
run: | | |
ssh staging 'export DOCKER_BRANCH=${{ github.ref_name }} | |
cd /home/realt/docker/api/${DOCKER_BRANCH}-git | |
git pull origin ${{ github.ref_name }} | |
docker compose --file docker-compose.${{ github.ref_name }}.yml pull | |
docker login -u ${{ secrets.DOCKER_LOGIN }} -p ${{ secrets.DOCKER_PASSWD }} ${{ secrets.DOCKER_REGISTRY }} | |
docker compose --project-name ${{ github.ref_name }}_php-api --file docker-compose.${{ github.ref_name }}.yml up -d' | |
# Add migration !? |