2.19.9. Deploy using GitHub Actions

GitHub Actions is a set of tools for automating workflows on GitHub. They can be used to trigger various actions when certain events occur. One example is automatically uploading project code from GitHub to hosting when changes are pushed to the repository.

Attention!

On deployment, the directory on the hosting is cleaned up. The contents of the directory will match the contents of the repository.

Setting up automatic deployment using the Rsync Deployments Action as an example.

  1. Configure SSH key-based authentication:
    1. Generate and add key to your account.
    2. Bind key to the hosting account where the deploy is to be performed.
  2. Open your repository on GitHub.
  3. Add the data required for the deployment to the secrets:

    How to add secrets:

    1. Switch to the "Settings" tab.
    2. Select "Secrets and variables → Actions" from the side menu.
    3. Click "New repository secret".
    4. Specify the name of the secret in the "Name" field and the value in the "Value" field.
    5. Click "Add secret".

    Each parameter is added as a separate secret.

    Name Value
    REMOTE_HOST SSH host
    REMOTE_USER SSH login
    REMOTE_KEY The private key data in PEM format (starts with -----BEGIN RSA PRIVATE KEY-----). ⚠️ Key must be private, not public.
    REMOTE_KEY_PASS Private key passphrase (if not used, can be not added or added with any value).
    REMOTE_PATH Absolute path to the directory on the hosting, to which the upload should be performed.
  4. Configure action:
    1. Switch to the "Actions" tab.
    2. Click "set up a workflow yourself" or first "New workflow" and then "set up a workflow yourself".
    3. Replace the code in the "Edit new file" field with this:
      name: SSH deploy on push
      on:
        push:
          branches:
          - main
      jobs:
        build:
          runs-on: ubuntu-latest
          steps:
            - uses: actions/checkout@v6
            - name: rsync deploy
              uses: burnett01/rsync-deployments@v8
              with:
                switches: -avzr --delete
                path: .
                remote_host: ${{ secrets.REMOTE_HOST }}
                remote_user: ${{ secrets.REMOTE_USER }}
                remote_key: ${{ secrets.REMOTE_KEY }}
                remote_key_pass: ${{ secrets.REMOTE_KEY_PASS }}
                remote_path: ${{ secrets.REMOTE_PATH }}
    4. Click "Start commit" and then "Commit new file".
  5. Switch to the "Actions" tab and check the status of the deployment. Click on the workflow name in the "All workflows" list and then on "build" to view details — if successful, there should be a checkmark next to each operation.

Attention!

On deployment, the directory on the hosting is not cleaned up. Files that are not in the repository are not removed from the directory.

Server sent FIN packet unexpectedly

If you get the error "Server sent FIN packet unexpectedly" when deploying a project with a large number of files, it is recommended to switch to SSH. For FTP may help, downgrading the action version to 3.1.2-patch. This will require modifying the action: replace the uses: SamKirkland/FTP-Deploy-Action@v4.3.5 line with uses: SamKirkland/FTP-Deploy-Action@v3.1.2-patch, rename the server, username and password parameters to ftp-server, ftp-username and ftp-password. Also, the server-dir parameter will not work in this case and the deploy directory will only need to be set by changing the access directory of FTP user.

Setting up automatic deployment using the FTP Deploy action as an example.

  1. Create an FTP user on the hosting with access to the directory to which the upload should be performed.
  2. Disable FTP access restrictions.
  3. Open your repository on GitHub.
  4. Add the data required for the deployment to the secrets:

    How to add secrets:

    1. Switch to the "Settings" tab.
    2. Select "Secrets and variables → Actions" from the side menu.
    3. Click "New repository secret".
    4. Specify the name of the secret in the "Name" field and the value in the "Value" field.
    5. Click "Add secret".

    Each parameter is added as a separate secret.

    Name Value
    REMOTE_HOST FTP host
    FTP_USER FTP login
    FTP_PASSWORD FTP password
    FTP_PATH Relative path from FTP access directory to the directory on the hosting where the deploy should be performed. ⚠️ The path must end with the slash /. Also, if your project has lib or etc directories, the FTP user access directory should be such that they are not in the root.
  5. Configure action:
    1. Switch to the "Actions" tab.
    2. Click "set up a workflow yourself" or first "New workflow" and then "set up a workflow yourself".
    3. Replace the code in the "Edit new file" field with this:
      name: FTP deploy on push
      on:
        push:
          branches:
          - main
      jobs:
        web-deploy:
          name: Deploy
          runs-on: ubuntu-latest
          steps:
          - name: Get latest code
            uses: actions/checkout@v4
          - name: Sync files
            uses: SamKirkland/FTP-Deploy-Action@v4.3.5
            with:
              server: ${{ secrets.REMOTE_HOST }}
              username: ${{ secrets.FTP_USER }}
              password: ${{ secrets.FTP_PASSWORD }}
              server-dir: ${{ secrets.FTP_PATH }}
    4. Click "Start commit" and then "Commit new file".
  6. Switch to the "Actions" tab and check the status of the deployment. Click on the workflow name in the "All workflows" list and then on "build" to view details — if successful, there should be a checkmark next to each operation.
Tabla de conținut

    (3)

    Comentarii

    wladislaw353
    Можна налаштувати деплой лише при пуші в головну гілку, змінивши конфіг наступним чином

    on:
    push:
    branches:
    - main
    sanyva
    Будь ласка, додайте інструкцію для Gitlab
    webmega
    Підтримую. Цікавить інструкція для GitLab. Дякую.
    artgana
    як налаштувати деплой через sftp в GitHub Actions?
    karlov
    Спосіб «SSH» в цій статті це по суті і є такий деплой.