Publish Function Compute FC applications

Updated at:

Alibaba Cloud DevOps Flow deploys and publishes applications to Alibaba Cloud Function Compute (FC). You can deploy functions from ZIP code packages stored in Object Storage Service (OSS). FC is an event-driven and fully managed serverless compute service. You do not need to manage servers or other infrastructure. You only need to write and upload your code. FC prepares the compute resources and runs your code with elasticity and reliability.

Prerequisites

If you use the OSS source code type, you must complete the following preparations.

  • Ensure that you have created and configured an Object Storage Service (OSS) instance. For more information, see Create a bucket.

  • Ensure that you have created a function instance. Configure an event-triggered or HTTP-triggered function and set the code upload method to OSS. Function Compute provides multiple methods to upload code. You can choose a method based on your requirements. For more information, see Manage functions.

Step 1: Create a pipeline

  1. Log on to the Flow console. In the upper-right corner, click New Pipeline.

  2. In the dialog box, select a pipeline template. This tutorial uses the empty template. Click Create.

Step 2: Configure the pipeline to deploy to Function Compute

Add a task node. Click the Empty Task node. On the Edit page, configure the parameters.

Parameter

Description

Task Name

Enter a custom name for the task.

Build Cluster

Select the default or a private build cluster region as needed.

Build Environment

Select a build environment as needed.

Note

During task execution, the pipeline build environment is allocated for your use. You have full permissions to the build environment and can customize the execution script.

Download Pipeline Source

Select a method as needed. This is a task-level setting that applies to the entire task node. You cannot enable or disable it separately for individual steps.

In the Task Steps section, click Add Step on the right. From the pop-up menu, select Publish. Click Deploy to Alibaba Cloud Function Compute to add the task to the task steps.

Parameter

Description

Step Name

Enter a custom name for the step.

Select Service Connection

Click Add Service Connection on the right. Grant Flow the permissions to access the FC service. Click Create to finish creating the service connection.

Region

Select the region where the target FC instance is located.

Service Name

Select the custom service name to which the target FC instance belongs.

Function Name

Select the name of the function in the target FC instance.

Select a source code type. The parameters vary based on the source code type. Configure the parameters accordingly.

OSS ZIP file

When you use the OSS ZIP file source code type, the ZIP code package in OSS must first be built and packaged by the pipeline, and then uploaded to OSS. For a Node.js project, perform the following steps:

  1. Add an Install Node.js Environment step, and select a Node.js version that matches the runtime of the target FC function.

  2. Add a Run Command step, and run npm install to install dependencies.

  3. Add a Run Command step to package the complete directory, including node_modules, into a ZIP file. For example, run zip -r code_${BUILD_NUMBER}.zip .. The ZIP package must contain the node_modules directory. Otherwise, the function fails at runtime because the dependency modules cannot be found.

  4. Add an Upload to OSS step to upload the ZIP package to the OSS bucket.

  5. Add a Deploy to Alibaba Cloud Function Compute step, set Source Code Type to OSS ZIP file, and deploy the package from OSS.

The command context of each pipeline step is independent, and shell variables are not shared across steps. Configure dependency installation, packaging, uploading, and deployment as separate steps in the order described above.

The preceding steps apply to the OSS ZIP file source code type. For the artifact package method, see Build and deploy a Node.js application.

  1. Set the OSS Bucket parameter to the name of the destination bucket that you created.

  2. Set the OSS Object Name parameter to the custom file path in the destination bucket.

    Note
    • In an OSS instance, the OSS Object Name is the relative path of the file, such as 'test/fc.zip'. You can use environment variables in the path. For example, enter code_${BUILD_NUMBER}.zip to distinguish the code package produced by each pipeline run. BUILD_NUMBER is the pipeline run number, which starts at 1 and increments with each run. The OSS Object Name value must match the object name that you configured in the step that uploads the ZIP package to OSS. Otherwise, the code package cannot be retrieved during deployment.

    • Ensure that the .zip code package is uploaded to the OSS bucket.

Build and deploy a Node.js application

If your source code is a Node.js project, build the project and deploy the resulting artifact package to Function Compute (FC) as follows. Unlike the OSS ZIP file method, this method does not upload a pre-built ZIP package to OSS. Instead, the pipeline builds your Node.js project and deploys the build artifact directly.

  1. In the Task Steps section, click Add Step and add an Install Node.js Environment step. Select a Node.js version that matches the runtime of the target FC function.

  2. Add a Run Command step, and run npm install to install dependencies. If your project has a build script, also run npm run build to build the project.

  3. Add a Deploy to Alibaba Cloud Function Compute step. Select a service connection, and configure the region and the target function name. For descriptions of these parameters, see the parameter table in Step 2: Configure the pipeline to deploy to Function Compute.

  4. Set Source Code Type to Artifact package, and set Code Path based on the structure of your build output. If the entry point of your build output is in the root directory, enter ./. The deployed file must match the entry file configured for the function.

Note

The file you deploy must be consistent with the entry file configured for the target function. If they do not match, the function invocation fails.

Local code

  1. Add a Pipeline Source task. For Code Source, select a code repository.

  2. Set the Code Path parameter based on your environment. Enter a period (.) to indicate the current directory.

Step 3: Run and view the target pipeline job

  1. In the Flow console, go to the My Pipelines list. Click the image Run button to the right of the target pipeline. Click the pipeline name to view the status of the task nodes.

  2. After the pipeline job is complete, on the task node for the Function Compute (FC) deployment, click Log in the lower-right corner to view the execution logs.