乐闻世界logo
搜索文章和话题

GitHub Actions: How to get contents of VERSION file into environment variable?

1个答案

1

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 VERSION 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 VERSION file in your repository, for example:

shell
1.0.0

Then, you can use the following steps in a YAML file located in the .github/workflows directory:

yaml
name: Load VERSION file as environment variable on: [push] jobs: load-version: runs-on: ubuntu-latest steps: - name: Checkout repository uses: actions/checkout@v2 - name: Load VERSION run: echo "VERSION=$(cat VERSION)" >> $GITHUB_ENV # The following steps are for demonstration purposes and are not required - name: Print VERSION run: echo "The version is $VERSION"

In this workflow:

  1. on: [push] indicates that the workflow triggers on every push to the repository.
  2. jobs: defines the tasks; here, there is a single task named load-version.
  3. runs-on: ubuntu-latest specifies that the task runs on the latest version of Ubuntu.
  4. steps: contains several steps:
    • The Checkout repository step checks out the code into the runner environment.
    • The Load VERSION step uses a shell command to read the VERSION file's content and appends it in the format VERSION=<content> to the $GITHUB_ENV environment variable file, making the VERSION variable accessible in all subsequent steps.
    • The Print VERSION 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 VERSION file containing version information and wish to set up a workflow that loads environment variables from it, you can use the read-file action combined with echo commands and the GITHUB_ENV file to set the environment variables.

Here is another example workflow demonstrating how to load environment variables from the VERSION file:

yaml
name: Load Version as Env Var on: [push] jobs: set-env-var: runs-on: ubuntu-latest steps: - name: Checkout repository uses: actions/checkout@v2 - name: Read VERSION file id: version run: echo "VERSION=$(cat VERSION)" >> $GITHUB_ENV - name: Use VERSION run: echo "The version is $VERSION"

In the above workflow:

  1. The on: [push] event triggers the workflow on every push to the repository.
  2. The set-env-var job defines the task.
  3. The Checkout repository step uses the actions/checkout@v2 action to check out the repository into the runner environment.
  4. The Read VERSION file step defines a step named version, where a shell command reads the VERSION file's content and appends it in the format VERSION=<content> to the $GITHUB_ENV environment variable file, creating or updating the VERSION environment variable.
  5. The Use VERSION step demonstrates how to use the VERSION environment variable in subsequent steps.

Note that the VERSION environment variable becomes available in all subsequent steps of the current workflow after it is set. If your VERSION 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.

2024年6月29日 12:07 回复

你的答案