cozy-labs/cozy-desktop

View on GitHub
.github/actions/setup-couchdb/action.yaml

Summary

Maintainability
Test Coverage
name: Setup CouchDB
author: Erwan Guyader
description: Starts a CouchDB server of the given version on the given port.
inputs:
  couchdb-version:
    description: 'The version of CouchDB to run'
    required: true
  couchdb-port:
    description: 'The port on which to expose the internal CouchDB port'
    required: false
    default: 5984
  couchdb-user:
    description: 'Username of the admin user'
    required: true
  couchdb-password:
    description: 'Password of the admin user'
    required: true
outputs:
  couchdb-url:
    description: 'The base URL to make requests to CouchDB'
    value: ${{ steps.setup-couchdb.outputs.couchdb-url }}
runs:
  using: composite
  steps:
    - name: Set env variables
      shell: bash
      run: |
        if [[ -z $PODMAN_MACHINE_PATH ]]; then
          echo "PODMAN_MACHINE_PATH=/usr/local/bin/podman-machine" >> $GITHUB_ENV
        fi
        if [[ -z $BOOT2PODMAN_IMAGE_PATH ]]; then
          echo "BOOT2PODMAN_IMAGE_PATH=${{ github.workspace }}/boot2podman-fedora.iso" >> $GITHUB_ENV
        fi

    - name: Cache podman machine
      id: cache-podman-machine
      uses: actions/cache@v3
      env:
        cache-name: cache-podman-machine
      with:
        path: |
          ${{ env.PODMAN_MACHINE_PATH }}
        key: ${{ runner.os }}-${{ env.cache-name }}

    - name: Download podman-machine
      if: ${{ steps.cache-podman-machine.outputs.cache-hit != 'true' }}
      shell: bash
      run: |
        until curl -L https://github.com/boot2podman/machine/releases/download/v0.17/podman-machine.darwin-amd64 --output $PODMAN_MACHINE_PATH
        do
          echo "Waiting for the podman-machine executable..."
          sleep 1
          curl -L https://github.com/boot2podman/machine/releases/download/v0.17/podman-machine.darwin-amd64 --output /usr/local/bin/podman-machine
        done
        chmod +x $PODMAN_MACHINE_PATH

    - name: Cache boot2podman image
      id: cache-boot2podman-image
      uses: actions/cache@v3
      env:
        cache-name: cache-boot2podman-image
      with:
        path: |
          ${{ env.BOOT2PODMAN_IMAGE_PATH }}
        key: ${{ runner.os }}-${{ env.cache-name }}

    - name: Download boot2podman image
      if: ${{ steps.cache-boot2podman-image.outputs.cache-hit != 'true' }}
      shell: bash
      run: |
        until curl -L https://github.com/boot2podman/boot2podman-fedora-iso/releases/download/af598af/boot2podman-fedora.iso --output $BOOT2PODMAN_IMAGE_PATH
        do
          echo "Waiting for the boot2podman image..."
          sleep 1
        done

    - name: Start podman machine
      shell: bash
      run: |
        until [[ $(podman-machine status box) == "Running" ]]
        do
          podman-machine create \
          --virtualbox-boot2podman-url  "file://$BOOT2PODMAN_IMAGE_PATH" \
          --virtualbox-hostonly-cidr "192.168.56.1/24" \
          box || true
          sleep 1
        done

    - name: Setup CouchDB
      id: setup-couchdb
      shell: bash
      run: |
        podman-machine ssh box -L ${{ inputs.couchdb-port }}:localhost:5984 -N &

        echo "Downloading CouchDB docker image..."
        _=$(podman-machine ssh box -- sudo podman pull couchdb:${{ inputs.couchdb-version }}) # assign result to var to avoid exiting if the command errors out
        until [[ $? -eq 0 ]]
        do
          echo "Retrying CouchDB docker image download..."
          _=$(podman-machine ssh box -- sudo podman pull couchdb:${{ inputs.couchdb-version }})
        done

        echo "Starting CouchDB..."
        podman-machine ssh box \
          -- sudo podman run -d \
          -p 5984:5984 \
          -e "COUCHDB_USER=${{ inputs.couchdb-user }}" \
          -e "COUCHDB_PASSWORD=${{ inputs.couchdb-password }}" \
          --name couch \
          couchdb:${{ inputs.couchdb-version }}

        echo "Waiting for CouchDB server to be ready..."
        sleep 5

        COUCHDB_URL="http://${{ inputs.couchdb-user }}:${{ inputs.couchdb-password }}@localhost:${{ inputs.couchdb-port }}/"
        echo "couchdb-url=$(echo $COUCHDB_URL)" >> $GITHUB_OUTPUT