name: tag
on:
  push:
    tags:
    - '*'

# Environment variables available to all jobs and steps in this workflow
env:
  COMPOSER_MEMORY_LIMIT: -1

jobs:
  build:
    runs-on: ubuntu-22.04
    steps:
    - uses: actions/checkout@master

    - name: Setup PHP 8.3
      uses: shivammathur/setup-php@v2
      with:
        php-version: '8.3'

    - name: Export github reference var
      run: echo "GITHUB_REFERENCE=$(echo ${GITHUB_REF#refs/tags/})" >> $GITHUB_ENV

    - name: Setup dependencies
      run: |
        git config --global user.email "development+drupalwxt@zacharyseguin.ca"
        git config --global user.name "drupalwxt-svc"
        git clone --branch 11.1.x https://drupalwxt-svc:${{ secrets.PERSONAL_ACCESS_TOKEN }}@github.com/drupalwxt/site-wxt.git
        cd site-wxt
        rm composer.lock

    - name: Require patched wxt with Composer
      run: composer require drupalwxt/wxt:6.1.x-dev#${{ github.sha }} --working-dir=./site-wxt --ignore-platform-reqs

    - name: Build out the Drupal infrastructure
      run: |
        export DB_TYPE=mysql
        export DB_PORT=3306
        cd site-wxt
        git clone --branch 11.1.x https://github.com/drupalwxt/docker-scaffold.git docker
        make build
        docker compose -f docker-compose.ci.yml up -d
        docker ps -a
        sleep 10

    - name: Run installation and tests
      run: |
        export DB_TYPE=mysql
        export DB_PORT=3306
        cd site-wxt
        make drupal_install
        make drupal_migrate
        # Change ownership on html/core to allow files to be copied (see drupal_cs in Makefile)
        sudo chown -R $(id -u) html/core
        make test

    - name: Generate tarball
      run: |
        sudo rm -rf site-wxt
        composer global require grasmash/yaml-cli
        export PATH=/home/runner/.composer/vendor/bin:$PATH
        ./tarball.sh ${{ env.GITHUB_REFERENCE }}

    - name: Create Release
      id: create_release
      uses: actions/create-release@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        tag_name: ${{ env.GITHUB_REFERENCE }}
        release_name: ${{ env.GITHUB_REFERENCE }}
        draft: false
        prerelease: false

    - name: Upload Release Asset
      id: upload-release-asset
      uses: actions/upload-release-asset@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        upload_url: ${{ steps.create_release.outputs.upload_url }}
        asset_path: ./wxt-${{ env.GITHUB_REFERENCE }}.tar.gz
        asset_name: wxt-${{ env.GITHUB_REFERENCE }}.tar.gz
        asset_content_type: application/tar+gzip

    - name: Checkout repos to update tags
      run: |
        git clone --branch 11.1.x https://drupalwxt-svc:${{ secrets.PERSONAL_ACCESS_TOKEN }}@github.com/drupalwxt/site-wxt.git
        git clone --branch 11.1.x https://drupalwxt-svc:${{ secrets.PERSONAL_ACCESS_TOKEN }}@github.com/drupalwxt/wxt-project.git
        git clone --branch main   https://drupalwxt-svc:${{ secrets.PERSONAL_ACCESS_TOKEN }}@github.com/drupalwxt/helm-drupal.git
        git clone --branch master https://drupalwxt-svc:${{ secrets.PERSONAL_ACCESS_TOKEN }}@github.com/drupalwxt/terraform-kubernetes-drupalwxt.git

    - name: Update tags for site-wxt
      uses: jacobtomlinson/gha-find-replace@master
      with:
        find: "\"drupalwxt/wxt\": \"[3-5]\\.[0-9]+\\.[0-9|x]+(-(beta|rc|dev)[0-9]?)?\""
        replace: "\"drupalwxt/wxt\": \"${{ env.GITHUB_REFERENCE }}\""
        exclude: ".git/**"
        include: "site-wxt/**"

    - name: Update tags for wxt-project
      uses: jacobtomlinson/gha-find-replace@master
      with:
        find: "\"drupalwxt/wxt\": \"[3-5]\\.[0-9]+\\.[0-9]+(-(beta|rc)[0-9])?\""
        replace: "\"drupalwxt/wxt\": \"${{ env.GITHUB_REFERENCE }}\""
        exclude: ".git/**"
        include: "wxt-project/**"

    - name: Update tags for wxt-project
      uses: jacobtomlinson/gha-find-replace@master
      with:
        find: "wxt-project:[3-5]\\.[0-9]+\\.[0-9]+(-(beta|rc)[0-9])?"
        replace: "wxt-project:${{ env.GITHUB_REFERENCE }}"
        exclude: ".git/**"
        include: "wxt-project/**"

    - name: Update tags for helm-drupal
      uses: jacobtomlinson/gha-find-replace@master
      with:
        find: "appVersion: [3-5]\\.[0-9]+\\.[0-9]+(-(beta|rc)[0-9])?"
        replace: "appVersion: ${{ env.GITHUB_REFERENCE }}"
        exclude: ".git/**"
        include: "helm-drupal/**"

    - name: Update tags for helm-drupal
      uses: jacobtomlinson/gha-find-replace@master
      with:
        find: "tag: [3-5]\\.[0-9]+\\.[0-9]+(-(beta|rc)[0-9])?"
        replace: "tag: ${{ env.GITHUB_REFERENCE }}"
        exclude: ".git/**"
        include: "helm-drupal/**"

    - name: Update tags for terraform-kubernetes-drupalwxt
      uses: jacobtomlinson/gha-find-replace@master
      with:
        find: "tag: [3-5]\\.[0-9]+\\.[0-9]+(-(beta|rc)[0-9])?"
        replace: "tag: ${{ env.GITHUB_REFERENCE }}"
        exclude: ".git/**"
        include: "terraform-kubernetes-drupalwxt/**"

    - name: Enter the site-wxt directory
      working-directory: ./site-wxt
      run: |
        rm composer.lock

    - name: Install with Composer
      run: composer install --working-dir=./site-wxt --ignore-platform-reqs

    - name: Push to site-wxt the latest stable tag
      working-directory: ./site-wxt
      run: |
        git add .
        git commit -m "feat(composer): Update version in the composer.json file ${{ env.GITHUB_REFERENCE }}" || true
        git tag ${{ env.GITHUB_REFERENCE }}
        git push origin 11.1.x
        git push origin ${{ env.GITHUB_REFERENCE }}

    - name: Update tags for site-wxt
      uses: jacobtomlinson/gha-find-replace@master
      with:
        find: "\"drupalwxt/wxt\": \"[3-5]\\.[0-9]+\\.[0-9|x]+(-(beta|rc|dev)[0-9]?)?\""
        replace: "\"drupalwxt/wxt\": \"5.4.x-dev\""
        exclude: ".git/**"
        include: "site-wxt/**"

    - name: Enter the site-wxt directory
      working-directory: ./site-wxt
      run: |
        rm composer.lock

    - name: Install with Composer
      run: composer install --working-dir=./site-wxt --ignore-platform-reqs

    - name: Push to site-wxt the latest build sha
      working-directory: ./site-wxt
      run: |
        git add .
        git commit -m "feat(composer): Update version in the composer.json file" || true
        git push origin 11.1.x

    - name: Push to wxt-project the latest tags
      run: |
        cd wxt-project
        git add .
        git commit -m "Update to latest version of WxT ${{ env.GITHUB_REFERENCE }}" || true
        git tag ${{ env.GITHUB_REFERENCE }}
        git push origin 11.1.x
        git push origin ${{ env.GITHUB_REFERENCE }}

    - name: Push to helm-drupal the latest tags
      run: |
        cd helm-drupal
        git add .
        git commit -m "Update to latest version of WxT ${{ env.GITHUB_REFERENCE }}" || true
        git push origin main

    - name: Push to terraform-kubernetes-drupalwxt the latest tags
      run: |
        cd terraform-kubernetes-drupalwxt
        git add .
        git commit -m "Update to latest version of WxT ${{ env.GITHUB_REFERENCE }}" || true
        git push origin master

    # Connect to Container registry
    - uses: azure/docker-login@v1
      with:
        username: ${{ secrets.REGISTRY_USERNAME }}
        password: ${{ secrets.REGISTRY_PASSWORD }}

    # Container build and push to a Container registry
    - run: |
        docker tag site-wxt-web:latest drupalwxt/site-wxt:${{ env.GITHUB_REFERENCE }}
        docker tag site-wxt-nginx:latest drupalwxt/site-wxt:${{ env.GITHUB_REFERENCE }}-nginx
        docker tag site-wxt-web:latest drupalwxt/site-wxt:latest
        docker tag site-wxt-nginx:latest drupalwxt/site-wxt:latest-nginx
        docker push drupalwxt/site-wxt:${{ env.GITHUB_REFERENCE }}
        docker push drupalwxt/site-wxt:${{ env.GITHUB_REFERENCE }}-nginx
