# https://docs.github.com/en/actions/using-workflows/reusing-workflows
name: Reusable workflow (verify)

on:
  workflow_call:
    inputs:
      os:
        required: true
        type: string
        description: host OS that CI 'runs-on'
      docker_image:
        required: false
        type: string
        default: ""
        description: Docker image name
      package_installation_command:
        required: false
        type: string
        default: ""
        description: package installation command
    secrets:
      SSH_KEY_PEM:
        required: true
        description: SSH private key (PEM format)
      SSH_KEY_PKCS8:
        required: true
        description: SSH private key (PKCS8 format)
      SSH_KEY_RFC4716:
        required: true
        description: SSH private key (RFC4716 format)

jobs:
  ssh-pem:
    name: Connect to github.com (PEM format)
    runs-on: ${{ inputs.os }}
    container: ${{ inputs.docker_image }}
    steps:
    - name: Install packages
      run: ${{ inputs.package_installation_command }}
      if: ${{ inputs.package_installation_command != '' }}
    - name: Checkout source codes
      uses: actions/checkout@v3
    - name: Install SSH key
      uses: ./.
      with:
        key: ${{ secrets.SSH_KEY_PEM }}
        known_hosts: unnecessary
    - name: git clone through SSH
      run: git clone git@github.com:shimataro/ssh-key-action.git tmp

  ssh-pem-bitbucket:
    name: Connect to bitbucket.org (PEM format)
    runs-on: ${{ inputs.os }}
    container: ${{ inputs.docker_image }}
    steps:
    - name: Install packages
      run: ${{ inputs.package_installation_command }}
      if: ${{ inputs.package_installation_command != '' }}
    - name: Checkout source codes
      uses: actions/checkout@v3
    - name: Install SSH key
      uses: ./.
      with:
        key: ${{ secrets.SSH_KEY_PEM }}
        known_hosts: |
          bitbucket.org ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDQeJzhupRu0u0cdegZIa8e86EG2qOCsIsD1Xw0xSeiPDlCr7kq97NLmMbpKTX6Esc30NuoqEEHCuc7yWtwp8dI76EEEB1VqY9QJq6vk+aySyboD5QF61I/1WeTwu+deCbgKMGbUijeXhtfbxSxm6JwGrXrhBdofTsbKRUsrN1WoNgUa8uqN1Vx6WAJw1JHPhglEGGHea6QICwJOAr/6mrui/oB7pkaWKHj3z7d1IC4KWLtY47elvjbaTlkN04Kc/5LFEirorGYVbt15kAUlqGM65pk6ZBxtaO3+30LVlORZkxOh+LKL/BvbZ/iRNhItLqNyieoQj/uh/7Iv4uyH/cV/0b4WDSd3DptigWq84lJubb9t/DnZlrJazxyDCulTmKdOR7vs9gMTo+uoIrPSb8ScTtvw65+odKAlBj59dhnVp9zd7QUojOpXlL62Aw56U4oO+FALuevvMjiWeavKhJqlR7i5n9srYcrNV7ttmDw7kf/97P5zauIhxcjX+xHv4M=
          bitbucket.org ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBPIQmuzMBuKdWeF4+a2sjSSpBK0iqitSQ+5BM9KhpexuGt20JpTVM7u5BDZngncgrqDMbWdxMWWOGtZ9UgbqgZE=
          bitbucket.org ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIIazEu89wgQZ4bqs3d63QSMzYVa0MuJ2e2gKTKqu+UUO
    - name: git clone through SSH
      run: git clone git@bitbucket.org:shimataro999/ssh-test.git tmp

  ssh-pkcs8:
    name: Connect to github.com (PKCS8 format)
    runs-on: ${{ inputs.os }}
    container: ${{ inputs.docker_image }}
    steps:
    - name: Install packages
      run: ${{ inputs.package_installation_command }}
      if: ${{ inputs.package_installation_command != '' }}
    - name: Checkout source codes
      uses: actions/checkout@v3
    - name: Install SSH key
      uses: ./.
      with:
        key: ${{ secrets.SSH_KEY_PKCS8 }}
        known_hosts: unnecessary
    - name: git clone through SSH
      run: git clone git@github.com:shimataro/ssh-key-action.git tmp

  ssh-pkcs8-bitbucket:
    name: Connect to bitbucket.org (PKCS8 format)
    runs-on: ${{ inputs.os }}
    container: ${{ inputs.docker_image }}
    steps:
    - name: Install packages
      run: ${{ inputs.package_installation_command }}
      if: ${{ inputs.package_installation_command != '' }}
    - name: Checkout source codes
      uses: actions/checkout@v3
    - name: Install SSH key
      uses: ./.
      with:
        key: ${{ secrets.SSH_KEY_PKCS8 }}
        known_hosts: |
          bitbucket.org ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDQeJzhupRu0u0cdegZIa8e86EG2qOCsIsD1Xw0xSeiPDlCr7kq97NLmMbpKTX6Esc30NuoqEEHCuc7yWtwp8dI76EEEB1VqY9QJq6vk+aySyboD5QF61I/1WeTwu+deCbgKMGbUijeXhtfbxSxm6JwGrXrhBdofTsbKRUsrN1WoNgUa8uqN1Vx6WAJw1JHPhglEGGHea6QICwJOAr/6mrui/oB7pkaWKHj3z7d1IC4KWLtY47elvjbaTlkN04Kc/5LFEirorGYVbt15kAUlqGM65pk6ZBxtaO3+30LVlORZkxOh+LKL/BvbZ/iRNhItLqNyieoQj/uh/7Iv4uyH/cV/0b4WDSd3DptigWq84lJubb9t/DnZlrJazxyDCulTmKdOR7vs9gMTo+uoIrPSb8ScTtvw65+odKAlBj59dhnVp9zd7QUojOpXlL62Aw56U4oO+FALuevvMjiWeavKhJqlR7i5n9srYcrNV7ttmDw7kf/97P5zauIhxcjX+xHv4M=
          bitbucket.org ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBPIQmuzMBuKdWeF4+a2sjSSpBK0iqitSQ+5BM9KhpexuGt20JpTVM7u5BDZngncgrqDMbWdxMWWOGtZ9UgbqgZE=
          bitbucket.org ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIIazEu89wgQZ4bqs3d63QSMzYVa0MuJ2e2gKTKqu+UUO
    - name: git clone through SSH
      run: git clone git@bitbucket.org:shimataro999/ssh-test.git tmp

  ssh-rfc4716:
    name: Connect to github.com (RFC4716 format)
    runs-on: ${{ inputs.os }}
    container: ${{ inputs.docker_image }}
    steps:
    - name: Install packages
      run: ${{ inputs.package_installation_command }}
      if: ${{ inputs.package_installation_command != '' }}
    - name: Checkout source codes
      uses: actions/checkout@v3
    - name: Install SSH key
      uses: ./.
      with:
        key: ${{ secrets.SSH_KEY_RFC4716 }}
        known_hosts: unnecessary
    - name: git clone through SSH
      run: git clone git@github.com:shimataro/ssh-key-action.git tmp

  ssh-rfc4716-bitbucket:
    name: Connect to bitbucket.org (RFC4716 format)
    runs-on: ${{ inputs.os }}
    container: ${{ inputs.docker_image }}
    steps:
    - name: Install packages
      run: ${{ inputs.package_installation_command }}
      if: ${{ inputs.package_installation_command != '' }}
    - name: Checkout source codes
      uses: actions/checkout@v3
    - name: Install SSH key
      uses: ./.
      with:
        key: ${{ secrets.SSH_KEY_RFC4716 }}
        known_hosts: |
          bitbucket.org ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDQeJzhupRu0u0cdegZIa8e86EG2qOCsIsD1Xw0xSeiPDlCr7kq97NLmMbpKTX6Esc30NuoqEEHCuc7yWtwp8dI76EEEB1VqY9QJq6vk+aySyboD5QF61I/1WeTwu+deCbgKMGbUijeXhtfbxSxm6JwGrXrhBdofTsbKRUsrN1WoNgUa8uqN1Vx6WAJw1JHPhglEGGHea6QICwJOAr/6mrui/oB7pkaWKHj3z7d1IC4KWLtY47elvjbaTlkN04Kc/5LFEirorGYVbt15kAUlqGM65pk6ZBxtaO3+30LVlORZkxOh+LKL/BvbZ/iRNhItLqNyieoQj/uh/7Iv4uyH/cV/0b4WDSd3DptigWq84lJubb9t/DnZlrJazxyDCulTmKdOR7vs9gMTo+uoIrPSb8ScTtvw65+odKAlBj59dhnVp9zd7QUojOpXlL62Aw56U4oO+FALuevvMjiWeavKhJqlR7i5n9srYcrNV7ttmDw7kf/97P5zauIhxcjX+xHv4M=
          bitbucket.org ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBPIQmuzMBuKdWeF4+a2sjSSpBK0iqitSQ+5BM9KhpexuGt20JpTVM7u5BDZngncgrqDMbWdxMWWOGtZ9UgbqgZE=
          bitbucket.org ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIIazEu89wgQZ4bqs3d63QSMzYVa0MuJ2e2gKTKqu+UUO
    - name: git clone through SSH
      run: git clone git@bitbucket.org:shimataro999/ssh-test.git tmp

  key_if_exists_replace-key_exists:
    name: if_key_exists=replace / key exists
    runs-on: ${{ inputs.os }}
    container: ${{ inputs.docker_image }}
    steps:
    - name: Install packages
      run: ${{ inputs.package_installation_command }}
      if: ${{ inputs.package_installation_command != '' }}
    - name: Checkout source codes
      uses: actions/checkout@v3
    - name: Install SSH key (dummy)
      uses: ./.
      with:
        key: "dummy" # replaced
        known_hosts: unnecessary
    - name: Install SSH key (replaces existing key)
      uses: ./.
      with:
        key: ${{ secrets.SSH_KEY_PEM }}
        known_hosts: unnecessary
        if_key_exists: replace
    - name: git clone through SSH
      run: git clone git@github.com:shimataro/ssh-key-action.git tmp

  key_if_exists_replace-key_doesnt_exist:
    name: if_key_exists=replace / key doesn't exist
    runs-on: ${{ inputs.os }}
    container: ${{ inputs.docker_image }}
    steps:
    - name: Install packages
      run: ${{ inputs.package_installation_command }}
      if: ${{ inputs.package_installation_command != '' }}
    - name: Checkout source codes
      uses: actions/checkout@v3
    - name: Install SSH key
      uses: ./.
      with:
        key: ${{ secrets.SSH_KEY_PEM }}
        known_hosts: unnecessary
        if_key_exists: replace
    - name: git clone through SSH
      run: git clone git@github.com:shimataro/ssh-key-action.git tmp

  key_if_exists_ignore-key_exists:
    name: if_key_exists=ignore / key exists
    runs-on: ${{ inputs.os }}
    container: ${{ inputs.docker_image }}
    steps:
    - name: Install packages
      run: ${{ inputs.package_installation_command }}
      if: ${{ inputs.package_installation_command != '' }}
    - name: Checkout source codes
      uses: actions/checkout@v3
    - name: Install SSH key
      uses: ./.
      with:
        key: ${{ secrets.SSH_KEY_PEM }}
        known_hosts: unnecessary
    - name: Install SSH key (does nothing)
      uses: ./.
      with:
        key: "dummy" # ignored
        known_hosts: unnecessary
        if_key_exists: ignore
    - name: git clone through SSH
      run: git clone git@github.com:shimataro/ssh-key-action.git tmp

  key_if_exists_ignore-key_doesnt_exist:
    name: if_key_exists=ignore / key doesn't exist
    runs-on: ${{ inputs.os }}
    container: ${{ inputs.docker_image }}
    steps:
    - name: Install packages
      run: ${{ inputs.package_installation_command }}
      if: ${{ inputs.package_installation_command != '' }}
    - name: Checkout source codes
      uses: actions/checkout@v3
    - name: Install SSH key
      uses: ./.
      with:
        key: ${{ secrets.SSH_KEY_PEM }}
        known_hosts: unnecessary
        if_key_exists: ignore
    - name: git clone through SSH
      run: git clone git@github.com:shimataro/ssh-key-action.git tmp

  key_if_exists_fail-key_exists:
    name: if_key_exists=fail / key exists
    runs-on: ${{ inputs.os }}
    container: ${{ inputs.docker_image }}
    steps:
    - name: Install packages
      run: ${{ inputs.package_installation_command }}
      if: ${{ inputs.package_installation_command != '' }}
    - name: Checkout source codes
      uses: actions/checkout@v3
    - name: Install SSH key
      uses: ./.
      with:
        key: ${{ secrets.SSH_KEY_PEM }}
        known_hosts: unnecessary
    - name: Install SSH key (fails)
      uses: ./.
      with:
        key: "dummy" # fails
        known_hosts: unnecessary
        if_key_exists: fail
      continue-on-error: true
    - name: git clone through SSH
      run: git clone git@github.com:shimataro/ssh-key-action.git tmp

  key_if_exists_fail-key_doesnt_exist:
    name: if_key_exists=fail / key doesn't exist
    runs-on: ${{ inputs.os }}
    container: ${{ inputs.docker_image }}
    steps:
    - name: Install packages
      run: ${{ inputs.package_installation_command }}
      if: ${{ inputs.package_installation_command != '' }}
    - name: Checkout source codes
      uses: actions/checkout@v3
    - name: Install SSH key
      uses: ./.
      with:
        key: ${{ secrets.SSH_KEY_PEM }}
        known_hosts: unnecessary
        if_key_exists: fail
    - name: git clone through SSH
      run: git clone git@github.com:shimataro/ssh-key-action.git tmp
