2.27.2. Deploy with GitHub Actions

GitHub Actions is a collection of workflow automation tools on GitHub. With their help, you can run various actions when certain events occur. One example is the automatic upload of the project code from GitHub to the hosting when pushing changes to the repository.

Attention!

Directory on hosting during deployment not cleared. Files that are not in the repository are not removed from the directory.

Setting up automatic deployment on the example of an action FTP Deploy.

  1. Create hosted by an FTP user with access to the directory where the deployment should be performed.
  2. Disable FTP access restrictions.
  3. Open your repository on GitHub.
  4. Add the data necessary for deployment to the secrets:

    How to add secrets:

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

    Each setting is added as a separate secret.

    Name Value
    REMOTE_HOST FTP host
    FTP_USER FTP Login
    FTP_PASSWORD FTP password
    FTP_PATH Relative way from FTP access directory to the directory on the hosting where the deployment should be performed. Attention! Way necessarily must end with a slash /. Also if your project has directories lib or etc, the FTP user's access directory must be such that they end up not at the root.
  5. Set up the action:
    1. Switch to tab "Actions".
    2. Click "set up a workflow yourself" or first "New workflow", and then "set up a workflow yourself".
    3. Replace the code in the field "Edit new file" on this one:
      name: FTP deploy on push
      on: push
      jobs:
        web-deploy:
          name: Deploy
          runs-on: ubuntu-latest
          steps:
          - name: Get latest code
            uses: actions/checkout@v2
          - name: Sync files
            uses: SamKirkland/FTP-Deploy-Action@4.3.0
            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 tab "Actions" and check the deployment status. To view details, click on the workflow name in the list "All workflows" and then on "build" - if successful, each operation should have a checkmark next to it.

Attention!

Directory on hosting during deployment cleared. The contents of the directory will match the contents of the repository.

Setting up automatic deployment on the example of an action Rsync Deployments Action.

  1. Set up SSH key authentication:
    1. Generate and add account key.
    2. Bind the key to the hosting account where the deployment should be performed.
  2. Open your repository on GitHub.
  3. Add the data necessary for deployment to the secrets:

    How to add secrets:

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

    Each setting is added as a separate secret.

    Name Value
    REMOTE_HOST SSH host
    REMOTE_USER SSH Login
    REMOTE_KEY Data private key in PEM format (starts with -----BEGIN RSA PRIVATE KEY-----). Attention! The key must be private, not public.
    REMOTE_PATH Absolute path to the directory on the hosting where the deployment should be performed.
  4. Set up the action:
    1. Switch to tab "Actions".
    2. Click "set up a workflow yourself" or first "New workflow", and then "set up a workflow yourself".
    3. Replace the code in the field "Edit new file" on this one:
      name: SSH deploy on push
      on: push
      jobs:
        build:
          runs-on: ubuntu-latest
          steps:
            - uses: actions/checkout@v2
            - name: rsync deploy
              uses: burnett01/rsync-deployments@5.2
              with:
                switches: -avzr --delete
                path: .
                remote_host: ${{ secrets.REMOTE_HOST }}
                remote_user: ${{ secrets.REMOTE_USER }}
                remote_key: ${{ secrets.REMOTE_KEY }}
                remote_path: ${{ secrets.REMOTE_PATH }}
    4. Click "Start commit" and then "Commit new file".
  5. Switch to tab "Actions" and check the deployment status. To view details, click on the workflow name in the list "All workflows" and then on "build" - if successful, each operation should have a checkmark next to it.
Content