Web application on SourceCraft Sites and Yandex Cloud Functions

In this tutorial, you will deploy a web application from a repository using SourceCraft CI/CD. The app features the following components:

  • Frontend: Static website on SourceCraft Sites with a field for enter the name and a Hello button.
  • Backend: Yandex Cloud Functions function in Python that returns Hello, <name>!.
  • Yandex API Gateway gateway that accepts requests from the frontend and calls the function.

The application code and CI/CD configuration are stored in a SourceCraft repository. Yandex Cloud resources are created through a service connection.

This use case is suitable for prototypes and smaller projects: you do not have to manager servers, and SourceCraft Sites provides free hosting with HTTPS access without uploading a custom TLS certificate.

To create a web app:

  1. Get ready to work.
  2. Create a service account.
  3. Create a repository fork.
  4. Create a service connection.
  5. Prepare the configuration and application code.
  6. Deploy the application.
  7. Test the application.

If you no longer need the resources you have created, delete them and unpublish the website.

Get ready to work

  1. Log in to the Yandex Cloud management console or sign up.

  2. Make sure you have a billing account with the ACTIVE or TRIAL_ACTIVE status. If you do not have a billing account, create one and link a cloud to it.

  3. Select or create a folder to place your function and API gateway in.

  4. Authenticate in SourceCraft on the service home page or sign up.

  5. Select or create a public organization in SourceCraft.

    To create a service connection at the organization level, you need the Organization admin (organization-manager.admin) or Organization owner (organization-manager.organizations.owner) role.

Warning

You can only publish a website from a public repository in a public SourceCraft organization. The repository files will be available to all internet users. Do not save tokens, access keys, and other secrets in them. A service connection allows accessing Yandex Cloud without storing long-lived keys in the code.

The infrastructure support cost includes:

Note

For Cloud Functions and API Gateway, a non-chargeable amount of services is available. Exceeding this limit is charged according to the relevant pricing policy.

Website hosting on SourceCraft Sites is free. You can find the CI/CD terms of use in the SourceCraft pricing policy.

Create a service account

Under a service account, CI/CD workflows will create and delete resources, while the API gateway will call a function.

  1. In the Yandex Cloud management console, select your prepared folder.

  2. Navigate to Identity and Access Management.

  3. Click Create service account.

  4. In the Name field, specify sites-web-app-sa.

  5. Click  Add role and select roles for the folder:

  6. Click Create.

Create a repository fork

A fork enables copying the example into your organization along with the CI/CD and SourceCraft Sites settings.

  1. Open the example repository.
  2. In the top-right corner, click Fork.
  3. Select your prepared public organization and specify the repository name, e.g., sourcecraft-sites-web-app.
  4. Select the master branch for the fork and click Create a fork.

The repository stores:

  • .sourcecraft/ci.yaml: CI/CD configuration with the create-resources and delete-resources workflows.
  • .sourcecraft/sites.yaml: Website publication configuration from the sites directory in the release branch.
  • sites/: Frontend HTML and JavaScript. The sites/config.js file contains the address of the API gateway that the workflow provides when deploying.
  • src/index.py: Python function code.
  • yc-scripts/: Scripts for getting an IAM token, creating and deleting Yandex Cloud resources.

Note

Cloning the example to a local computer does not create a repository in your organization. To follow this tutorial, use the fork you created in SourceCraft.

Create a service connection

Create a connection at the organization level. The default-service-connection name must match the one specified in .sourcecraft/ci.yaml.

  1. In SourceCraft, open the organization where you created the repository fork.

  2. Under Settings, navigate to Service connections.

  3. In the top-right corner, click New service connection.

  4. Under Basic information, specify the default-service-connection name.

  5. Under Scope, allow using it for all repositories.

  6. Under Yandex Cloud settings, select your prepared folder and the sites-web-app-sa service account.

    Tip

    To re-request the list of clouds, folders, and service accounts from Yandex Cloud, click Synchronize. This can be of use if alongside creating a service connection you also created a folder or service account.

  7. Click Create service connection and wait for the operation to complete.

For details, see this tutorial on configuring a service connection.

Prepare the configuration and application code

To edit a file in the SourceCraft UI, open it and click Edit. After editing each file, click Commit changes and commit to the master branch.

  1. Open the .sourcecraft/ci.yaml file.

  2. Make sure the tokens:SERVICE_CONNECTION_WITH_TOKEN:service_connection section shows the name of the service connection you created earlier.

  3. In the env section, check the names of the function (YC_FUNCTION_NAME) and API gateway (YC_API_GATEWAY_NAME).

    Warning

    Make sure the selected cloud folder has no function and API gateway with these names. If it does, set other names: the workflow updates the existing function and uses the names from this configuration when deleting resources.

Deploy the application

  1. In your repository, under CI/CD, navigate to the CI/CD section.
  2. Click New launch.
  3. Select the master branch for the configuration and launch, and the create-resources workflow; then click Launch now.
  4. Wait until the workflow completes with the Success status. If the workflow completed with an error, open it and check the logs of the cube that was not executed.

The workflow gets an IAM token through the service connection, sets a function and its version, and then creates an API gateway with the GET /hello/{name} route. After that, it writes the gateway address to the sites/config.js file and publishes the files in the release branch.

The .sourcecraft/sites.yaml file in the main branch is already configured to publish from the release branch:

site:
  root: sites
  ref: release

After this branch appears, the website is published automatically in a few minutes.

Test the application

  1. Open https://<organization_slug>.sourcecraft.site/<repositry_name>/ in your browser.
  2. In the Name field, enter the name, e.g., Alice, and click Hello.
  3. Make sure Hello, Alice! appeared under the button.

Delete the Yandex Cloud resources

  1. In your repository, manually run the delete-resources workflow from the master branch.
  2. Wait for its successful completion. The workflow will delete the API gateway and function under the names from .sourcecraft/ci.yaml.

You can also delete the API gateway and function in the Yandex Cloud console.

If the sites-web-app-sa service account is no longer in use, delete it after deleting the function and API gateway.

Unpublish the website

Deleting the function and API gateway does not unpublish the static website. After deleting the cloud resources, delete the .sourcecraft/sites.yaml file from the master main branch.

Useful links