How To Set Up Auto Deploy for Cloud Functions

When developing an application, you need to regularly make changes to the code and git push them to the repository.

To keep the code in Cloud Functions always up-to-date, you can set up auto deploy. Follow these steps to do this:

  1. Download the RC file.
  2. Create a function with your application in the Control panel of the Cloud platform.
  3. Create an auto deploy function in the Control panel.
  4. Configure a webhook in a repository on GitHub.

Downloading an RC File

To configure external access to the project, you need to get environment variables from the RC file.

To do this, from the Control panel of the Cloud platform:

  1. Create a user and add it to the project according to the User Access Control instructions.
  2. Go to the Access section of the project and open the External access tab. Select the user and download the RC file.

Creating a Function with an Application

Auto deploy code should update the function with your application code. To do this, you need to create a function in the Control panel and upload the archive with the application into it.

To do this, from the Control panel:

  1. Go to the Functions section and click Create function.
  2. Select the required runtime and enter the function name, for example my_app.
  3. Go to the Code tab. Select Archive in the Upload field.
  4. Click Upload in the Source code field and deploy your application archive.

Now the my_app function will be updated with auto deploy.

Creating an Auto Deploy Function

Follow these steps to create a function that will store the auto deploy code:

  1. Download the source code from GitHub as an archive (CodeDownload ZIP).
  2. Go to the Functions section in the Control panel and click Create function.
  3. Select the Python 3.7 runtime and enter the function name, for example autodeploy.
  4. Go to the Code tab. Select Archive in the Upload field.
  5. Click Upload in the Source code field and upload the ZIP file.
  6. Specify the Path to the file: deploy_function/deploy.
  7. Enter deploy in the Function to execute field.
  8. Click Add in the Environment variables field.
  9. Enter the details of your repository from which the code will be updated, as well as the data from the RC file:
    • ACTION_NAME — name of your function with the (my_app) application;
    • DEPLOY_TOKEN — is specified if the repository is private. If the repository is public, then you don’t need to add the variable;
    • REPO_URLgithub.com/{username}/{repository};
    • BRANCH — the name of the branch with the code ready for deployment;
    • OS_PROJECT_DOMAIN_NAME;
    • OS_PROJECT_ID;
    • OS_USER_DOMAIN_NAME;
    • OS_USERNAME;
    • OS_PASSWORD;
    • OS_AUTH_URL.
  10. Click Save and deploy.
  11. Go to the Triggers tab, enable call by HTTP request and copy the URL that appears — you will need it in the next step.

Now, when you call the autodeploy function, your my_app code will automatically update.

Configuring webhook in a Repository on GitHub

At this stage, you need to merge the repository on GitHub and the autodeploy function. To do this, configure a webhook in the GitHub repository, which will call the auto deploy function via an HTTP request when you push it to the repository.

  1. Go to your GitHub repository.
  2. Open SettingsWebhooks.
  3. Click Add webhook.
  4. Enter the autodeploy function URL in the Payload URL field.
  5. Select application/json in the Content type field.
  6. Select Just the push event in the Which events would you like to trigger this webhook? field.
  7. Click Update webhook.

Configuration Result

Auto deploy is configured. After you push the changes to the repository, the webhook will call the autodeploy function, which will update the function with the my_app application.

To make sure that the auto deploy service is running and the my_app function is updated, call the my_app function — it should return data in accordance with the changes in the code.