diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 729d6b9..06a6eaa 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -1,23 +1,30 @@ +name: Deploy to Server + on: push: branches: - main - workflow_dispatch: jobs: - run_pull: - name: run pull + deploy: runs-on: ubuntu-latest - + steps: - - name: install ssh keys - # check this thread to understand why its needed: - # https://stackoverflow.com/a/70447517 + - name: Checkout code + uses: actions/checkout@v3 + + - name: Install sshpass + run: sudo apt-get install -y sshpass + + - name: SSH into server and pull application run: | - install -m 600 -D /dev/null ~/.ssh/id_rsa - echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa - ssh-keyscan -H ${{ secrets.SSH_HOST }} > ~/.ssh/known_hosts - - name: connect and pull - run: ssh ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }} "cd ${{ secrets.WORK_DIR }} && git checkout ${{ secrets.MAIN_BRANCH }} && git pull && exit" - - name: cleanup - run: rm -rf ~/.ssh + sshpass -p "${{ secrets.SSH_PRIVATE_KEY }}" ssh -o StrictHostKeyChecking=no ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }} << EOF + cd ${{ secrets.WORK_DIR }} + + # Set GitHub credentials using GITHUB_TOKEN + git config --global credential.helper store + echo "https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}" > ~/.git-credentials + + # Pull the latest code from the repository + git pull origin ${{ secrets.MAIN_BRANCH }} + EOF