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 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.
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.
Select your prepared public organization and specify the repository name, e.g., sourcecraft-sites-web-app.
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.
In SourceCraft, open the organization where you created the repository fork.
Under Settings, navigate to Service connections.
In the top-right corner, click New service connection.
Under Basic information, specify the default-service-connection name.
Under Scope, allow using it for all repositories.
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.
Click Create service connection and wait for the operation to complete.
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.
Open the .sourcecraft/ci.yaml file.
Make sure the tokens:SERVICE_CONNECTION_WITH_TOKEN:service_connection section shows the name of the service connection you created earlier.
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
In your repository, under CI/CD, navigate to the CI/CD section.
Click New launch.
Select the master branch for the configuration and launch, and the create-resources workflow; then click Launch now.
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:sitesref:release
After this branch appears, the website is published automatically in a few minutes.
Test the application
Open https://<organization_slug>.sourcecraft.site/<repositry_name>/ in your browser.
In the Name field, enter the name, e.g., Alice, and click Hello.
Make sure Hello, Alice! appeared under the button.
Delete the Yandex Cloud resources
In your repository, manually run the delete-resources workflow from the master branch.
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.