问题答案 12026年7月4日 08:28
GitHub Actions: How to get contents of VERSION file into environment variable?
GitHub Actions is an automation tool provided by GitHub that enables developers to automatically execute software development workflows directly within their GitHub repositories. If you need to load environment variables from the file into GitHub Actions, you can achieve this by writing workflow steps. Here is an example of how to do this:First, you need to have a file in your repository, for example:Then, you can use the following steps in a YAML file located in the directory:In this workflow:indicates that the workflow triggers on every push to the repository.defines the tasks; here, there is a single task named .specifies that the task runs on the latest version of Ubuntu.contains several steps:The step checks out the code into the runner environment.The step uses a shell command to read the file's content and appends it in the format to the environment variable file, making the variable accessible in all subsequent steps.The step demonstrates the usage of the loaded environment variable by printing its value, confirming it has been successfully set and can be used within the workflow.The above steps demonstrate how to read content from a file and set it as an environment variable in GitHub Actions. After this setup, you can use this variable in subsequent steps, such as for building, deployment, or other scenarios requiring version information.In GitHub Actions, you can use various actions to read file content and set environment variables. If you have a file containing version information and wish to set up a workflow that loads environment variables from it, you can use the action combined with commands and the file to set the environment variables.Here is another example workflow demonstrating how to load environment variables from the file:In the above workflow:The event triggers the workflow on every push to the repository.The job defines the task.The step uses the action to check out the repository into the runner environment.The step defines a step named , where a shell command reads the file's content and appends it in the format to the environment variable file, creating or updating the environment variable.The step demonstrates how to use the environment variable in subsequent steps.Note that the environment variable becomes available in all subsequent steps of the current workflow after it is set. If your file contains complex data, such as multiple lines or specific formats requiring parsing, you may need to use more complex shell commands or write a script to parse the data and set the environment variables.