Search code examples
gitgithubgithub-actions

Github actions get SSH link of repository


I am using github actions to deploy to a server. Right now I wrote a small bash script to check if directory exists, if so git pull other wise git clone.

Everything is general, but I am not sure how to get the ssh link of the repository from the environment variables:

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

    - name: Create project dir
      uses: appleboy/ssh-action@master
      with:
        HOST: ${{ secrets.HOST }}
        USERNAME: ${{ secrets.USERNAME }}
        KEY: ${{ secrets.KEY }}
        PORT: ${{ secrets.PORT }}
        script: |
          mkdir -p ~/projects
          cd ~/projects
          if [ -d "${{ github.event.repository.name }}" ]; then
            echo "Directory already exists"
            cd ${{ github.event.repository.name }}
            git pull --rebase
          else
            git clone [email protected]:orgname/projectname.git
          fi
          echo 'Deployment to server succesful!'

So as we can see, git clone [email protected]:orgname/projectname.git is still hardcoded. How could I get this part general as well like github.event.repository.name?


Edit for VonC:

Output on GitHub actions:

  pwd
  ls -alrth
  git version
  echo "GURL=$(git remote get-url origin)">>$GITHUB_ENV
  shell: /bin/bash -e {0}
/home/runner/work/digitalocean-test-deploy/digitalocean-test-deploy
total 60K
drwxr-xr-x  3 runner docker 4.0K Dec 27 13:12 ..
drwxr-xr-x  2 runner docker 4.0K Dec 27 13:12 tests
drwxr-xr-x  2 runner docker 4.0K Dec 27 13:12 src
-rw-r--r--  1 runner docker 1.1K Dec 27 13:12 setup.py
drwxr-xr-x  2 runner docker 4.0K Dec 27 13:12 settings
-rw-r--r--  1 runner docker   26 Dec 27 13:12 requirements.txt
drwxr-xr-x  2 runner docker 4.0K Dec 27 13:12 notebooks
drwxr-xr-x  2 runner docker 4.0K Dec 27 13:12 docs
drwxr-xr-x  2 runner docker 4.0K Dec 27 13:12 data
-rw-r--r--  1 runner docker  516 Dec 27 13:12 README.md
-rw-r--r--  1 runner docker 1.1K Dec 27 13:12 LICENSE
-rw-r--r--  1 runner docker 1.8K Dec 27 13:12 .gitignore
drwxr-xr-x  3 runner docker 4.0K Dec 27 13:12 .github
drwxr-xr-x 10 runner docker 4.0K Dec 27 13:12 .
drwxr-xr-x  8 runner docker 4.0K Dec 27 13:12 .git
git version 2.29.2

Solution

  • Considering the checkout action allows to run commands in the checked out working tree, you could use $GITHUB_ENV environment variables, since set-env is deprecated since Oct. 2020:

    on: push
    jobs:
      build:
        runs-on: ubuntu-latest
        steps:
          - uses: actions/checkout@v2
          - run: |
              echo "GURL=$(git remote get-url origin)">>$GITHUB_ENV
          - name: Create project dir
              git clone $GURL
          
    

    As discussed, though, this does not seem to work within a step using ssh-action, hence appleboy/ssh-action issue 99 (Q4 2020).

    Q1 2021: this was resolved with the comment:

    The best way to do this is to build the clone URL yourself, something like this (untested):

    uses: appleboy/ssh-action@master
    env:
      REPO: ${{ github.repository }} # e.g. "appleboy/ssh-action"
      TOKEN: ${{ github.token }}
    with:
      envs: REPO,TOKEN
      script: |
        git clone https://[email protected]/$REPO.git
    

    See also "Easier builds and deployments using Git over HTTPS and OAuth" from Wynn Netherland.