Giter VIP home page Giter VIP logo

variable-substitution's Introduction

Deprecation notice

This Action is deprecated. The action keeps working even after archiving this repo. So existing workflows are not impacted and new workflows also work fine. Any further enhancement or support is not planned for this action.

GitHub Action for substituting variables in parameterized files .github/workflows/ci.yml

With the Variable Substitution Action for GitHub, you can apply variable substitution to XML, JSON and YAML based configuration and parameter files.

  • Tokens defined in the target configuration files are updated and then replaced with variable values.
  • Variable substitution is applied for only the JSON keys predefined in the object hierarchy. It does not create new keys.
  • Only variables defined explicitly as Environment variables as part of the workflow or system variables that are already available for workflow context can be used in substitution.
  • Variable substitution takes effect only on the applicationSettings, appSettings, connectionStrings and configSections elements of configuration files. Please refer this for more information.

If you are looking for more Github Actions to deploy code or a customized image into an Azure Webapp or a Kubernetes service, consider using Azure Actions.

The definition of this Github Action is in action.yml.

Example

See Use variable substitution with GitHub Actions for an example of how to use variable substitution.

End-to-End Sample Workflow

Sample workflow to apply Variable substitution on XML, JSON, YML files

# .github/workflows/var-substitution.yml
on: [push]
name: variable substitution in json, xml, and yml files

jobs:
  build:
    runs-on: windows-latest
    steps:
    - uses: actions/checkout@v2

    - uses: microsoft/variable-substitution@v1 
      with:
        files: 'Application/*.json, Application/*.yaml, ./Application/SampleWebApplication/We*.config'
      env:
        Var1: "value1"
        Var2.key1: "value2"
        SECRET: ${{ secrets.SOME_SECRET }}

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.

When you submit a pull request, a CLA bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.

variable-substitution's People

Contributors

ajaykalems avatar amrutakawade avatar balaga-gayatri avatar dependabot[bot] avatar jsoref avatar larryclaman avatar microsoft-github-operations[bot] avatar microsoftopensource avatar n-usha avatar ra80533 avatar suaggar avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

variable-substitution's Issues

Using this action changes timestamp format in the yaml file

When this action is used to change a key in the yaml file, it changes other settings which have timestamps even thought the timestamps are not being touched by the action.

config.yaml:
cco_start_date: 2021-02-08 15:04:50.825709
execution_date: 2021-02-11 15:04:50.825709
use_sample: false

In the workflow.yaml, if we use

- uses: microsoft/variable-substitution@v1
name: Toggle Sample Run in the config.yaml
with:
files: '${{ env.CUSTOM_WORKSPACE }}/src/config.yaml'
env:
run_settings.use_sample: true

the value of use_sample will be changed to true, but also the in the output above timestamps will be changed to
cco_start_date: 2021-02-08T15:18:20.925Z
execution_date: 2021-02-11T15:18:20.925Z
use_sample: true

Expected behavior:
All the other values should remain as-is, except for the substituted values.

Can't figure out how to get XML parsing to work.

      - name: Fix Air SDK Target Bug
        uses: microsoft/variable-substitution@v1
        if: matrix.os == 'windows-latest' && steps.cache-air-sdk.outputs.cache-hit != 'true'
        with:
          files: ${{ github.workspace }}\airsdk\frameworks\flex-config.xml
        env:
          flex-config.compiler.external-library-path.path-element: 'libs/player/32.0/playerglobal.swc'

It finds the file, but fails to walk the hierarchy and fill the value. There are no examples I can find on github that show usage with xml.

No file matched

I'm getting no file matched error in this step. Already tried different types of expliciting the file to substitute but still no progress.

The log with one of the tries:

2020-04-27T03:59:57.0825611Z ##[section]Starting: Request a runner to run this job
2020-04-27T03:59:57.5271608Z Requesting a hosted runner in current repository's account/organization with labels: 'ubuntu-latest', require runner match: True
2020-04-27T03:59:57.6326430Z Labels matched hosted runners has been found, waiting for one of them get assigned for this job.
2020-04-27T03:59:57.6585945Z ##[section]Finishing: Request a runner to run this job
2020-04-27T04:00:02.8937332Z Current runner version: '2.169.1'
2020-04-27T04:00:02.8961069Z ##[group]Operating System
2020-04-27T04:00:02.8961775Z Ubuntu
2020-04-27T04:00:02.8962087Z 18.04.4
2020-04-27T04:00:02.8962452Z LTS
2020-04-27T04:00:02.8962735Z ##[endgroup]
2020-04-27T04:00:02.8963036Z ##[group]Virtual Environment
2020-04-27T04:00:02.8963358Z Environment: ubuntu-18.04
2020-04-27T04:00:02.8963718Z Version: 20200406.2
2020-04-27T04:00:02.8964126Z Included Software: https://github.com/actions/virtual-environments/blob/ubuntu18/20200406.2/images/linux/Ubuntu1804-README.md
2020-04-27T04:00:02.8964508Z ##[endgroup]
2020-04-27T04:00:02.8965638Z Prepare workflow directory
2020-04-27T04:00:02.9168424Z Prepare all required actions
2020-04-27T04:00:02.9180417Z Download action repository 'microsoft/variable-substitution@v1'
2020-04-27T04:00:04.9113242Z ##[group]Run microsoft/variable-substitution@v1
2020-04-27T04:00:04.9113684Z with:
2020-04-27T04:00:04.9114025Z   files: /src/configs/*.json
2020-04-27T04:00:04.9114423Z   twitter.access_token: ***
2020-04-27T04:00:04.9114770Z   twitter.access_token_secret: ***
2020-04-27T04:00:04.9115066Z   twitter.api_key: ***
2020-04-27T04:00:04.9115382Z   twitter.api_key_secret: ***
2020-04-27T04:00:04.9115705Z   mongo.connection_string: ***
2020-04-27T04:00:04.9115977Z   mongo.database: ***
2020-04-27T04:00:04.9116269Z   bot.follow_triggers: ***
2020-04-27T04:00:04.9116519Z ##[endgroup]
2020-04-27T04:00:05.3380266Z ##[error]No file matched with specific pattern: /src/configs/*.json
2020-04-27T04:00:05.3383715Z ##[error]Error: Failed to apply variable substitution
2020-04-27T04:00:05.3409889Z Cleaning up orphan processes

My project structure:

image

the CI.yaml:

# This is a basic workflow to help you get started with Actions

name: Chr0m1ng Twitter Bot CI

# Controls when the action will run. Triggers the workflow on push or pull request
# events but only for the master branch
on:
  push:
    branches: [ master ]

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
    # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
    - name: Variable Substitution
      uses: microsoft/variable-substitution@v1
      with:
        # comma separated list of XML/JSON/YAML files in which tokens are to be substituted. Files names must be specified relative to the folder-path.
        files: '${{ env.GITHUB_WORKSPACE }}/src/configs/*.json'
        twitter.access_token: ${{ secrets.TWITTER_ACCESS_TOKEN }}
        twitter.access_token_secret: ${{ secrets.TWITTER_ACCESS_TOKEN_SECRET }}
        twitter.api_key: ${{ secrets.TWITTER_API_KEY }}
        twitter.api_key_secret: ${{ secrets.TWITTER_API_KEY_SECRET }}
        mongo.connection_string: ${{ secrets.MONGO_CONNECTION_STRING }}
        mongo.database: ${{ secrets.MONGO_DATABASE }}
        bot.follow_triggers: ${{ secrets.BOT_FOLLOW_TRIGGERS }}

I'm aiming the file located at src/configs/settings.example.json, can someone help?

Planning to upgrade logo on GitHub Marketplace to Microsoft logo by end of May 2021

Substituting an int & multiple yaml files in one document

Hi,

I am using this action to substitute values into a Kubernetes config file.

Substituting an integer;
I am trying to substitute a variable into the replicas value field on a k8s deployment config file.
This is how I call the substitution in the Github action file.

- name: Variable Substitution uses: microsoft/variable-substitution@v1 with: files: 'deploy-k8s.yaml' env: spec.replicas: 2

However when I substitute the integer the value substituted is given quotation marks.
The value substituted in the yaml file is
replicas: '2'

The yaml file just expects an int here and so will not deploy.
Is there any way to remove the quotes around the int when it is substituted?

Multiple Yaml Files
In the same document, I have two yaml files.
One which creates a deployment and the other creates a service.

When I try to substitute values in this file I get the below error.

Run microsoft/variable-substitution@v1 Error: Error: Could not parse file: deploy-k8s.yaml JSON parse error: SyntaxError: Unexpected token a in JSON at position 0 XML parse error: Error: Incomplete documentYAML parse error: YAMLException: expected a single document in the stream, but found more

When I comment out the service config, the substitution works fine.
Is there anyway to use this github action with YAML config files in the one document?

Thanks for your help!

Value altered when substitude value in yaml

How to reproduce

Here is part of my workflow script

      - name: Variable substitution
        uses: microsoft/variable-substitution@v1
        with:
          files: ./build/config.yaml
        env:
          sms.senderNumber: ${{ secrets.SMS_SENDER_NUMBER }}

And secrets.SMS_SENDER_NUMBER is 1069368924410002259

But when the workflow is done, I check the actual config file, I got this

image

The value is not the same as secrets.SMS_SENDER_NUMBER

What I have done to solve this

Check if GitHub secret correct

I use the following workflow debug GitHub action via ssh:

name: Show Me the S3cr3tz
on: [push]

jobs:
  debug:
    name: Debug
    runs-on: ubuntu-latest

    steps:
    - name: Check out code
      uses: actions/checkout@v2

    - name: Set up secret file
      env:
        SMS_SENDER_NUMBER: ${{ secrets.SMS_SENDER_NUMBER }}
      run: |
        echo $SMS_SENDER_NUMBER >> secrets.txt

    - name: Run tmate
      uses: mxschmitt/action-tmate@v2

The secret value is correct

Direct write value to the workflow YAML file

      - name: Variable substitution
        uses: microsoft/variable-substitution@v1
        with:
          files: ./build/config.yaml
        env:
          sms.senderNumber: 1069368924410002259

The result is, that the value is still altered,

Use double quotes

      - name: Variable substitution
        uses: microsoft/variable-substitution@v1
        with:
          files: ./build/config.yaml
        env:
          sms.senderNumber: "1069368924410002259"

The result is, that the value is still altered,

Use sed command

      - name: Replace sender number
        run: sed -i 's/SMS_SENDER_NUMBER/${{ secrets.SMS_SENDER_NUMBER }}/' ./build/config.yaml

The result shows that the replacement is done correctly.

How can we substitute attribute values of other XML Element like <machineKey>

Hello, I have the following Web.config XML file

<?xml...>
<configuration>
...
  <system.web>
    <machineKey validationKey="foo" decryptionKey="bar" />
  </system.web>
...
</configuration>

How do we change the validationKey and descriptionKey values?

I tried system.web.machineKey.validationKey: "baz" but it doesn't work. Also tried up to validationKey: "baz"

See sample repo here: https://github.com/gojanpaolo/wm-pipeline-sb

Thank you

Access Objects inside of an Array

(Thanks for the Action!)

It looks like the Access of Objects inside of an Array is not currently supported:

- uses: microsoft/variable-substitution@v1 
  with:
    files: './google-services.json'
  env:
    client[0].api_key[0].current_key: ${{ secrets.FIREBASE_API_KEY }}

Any near-term plans to support?

Can't get XML parsing to work (repost)

Apologies I couldn't reopen and I wasn't sure if you got my notifications from the closed issue.
#9

<?xml version="1.0" encoding="utf-8"?>
<application xmlns="http://ns.adobe.com/air/application/32.0">
  <id>R3</id>
  <versionNumber>9.9.9</versionNumber>
</application>
      - name: Variable Substitution
        uses: microsoft/variable-substitution@v1
        with:
          files: 'application.xml'
        env:
          application.versionNumber: ${{ steps.versionNumber.outputs.tag }}
Variable Substitution
    application.versionNumber: 
Run microsoft/variable-substitution@v1
  with:
    files: application.xml
  env:
    application.versionNumber: 1.2.2
Applying variable substitution on XML file: application.xml
Skipped updating file: application.xml
##[error]Error: Failed to apply variable substitution

Only web.config XML files are supported :(

Only certain tags of xml are currently supported.

File https://github.com/microsoft/variable-substitution/blob/master/lib/operations/xmlVariableSubstitution.js
lists these specific tags:
const tags = ["applicationSettings", "appSettings", "connectionStrings", "configSections"];

If I want to replace variables inside any other xml file that is not a web.config, I'm not able to, and the error message is not helpful at all.
Please at least improve the documentation or error message, so that other users to not spend their time having to look at the action source code to figure out what is going on...

Replace value in list

Not a bug per se, but is it possible to replace the value in a list element?

# value in json file
"tags": [""]

#resulting value
"tags": "env:dev-cc"

# would like
"tags": ["env:dev-cc"]

Target and replace XML connectionStrings node

I am attempting to change a cnfgConnection.config > "connectionString" value using the following action and settings.

How do I target the connectionStrings > add > connectionString value?

- uses: microsoft/variable-substitution@v1 
   with:
       files: './Website/cnfgConnection.config'
   env:
          connectionStrings.add: "Data Source=prod.server.com;Initial Catalog=test;User ID=prod_tester;Password=xxxxxx"

Example XML

<?xml version="1.0"?>
<connectionStrings>
  <add name="DefaultConnectionString" connectionString="Data Source=test.server.com;Initial Catalog=test;User ID=tester;Password=xxxxxx" providerName="System.Data.SqlClient"/>
</connectionStrings>

YAML file mistakenly taken as XML format

I created an YAML file with some html context as value, and the file is mastakenly taken as XML file by the workflow.

It looks that some thing wrong in xmlDomUtility.js

test.yml

foo:
  bar:
    test: "<div>some html context in yaml file</div>"

.github/workflows/test.yml

name: 'Test Workflow'

on:
  push:
    branches:
      - master

jobs:
  deploy:
    runs-on: ubuntu-20.04
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: replace secrets in yaml config
        uses: microsoft/variable-substitution@v1
        with:
          files: 'test.yml'
        env:
          foo.bar.test: ${{ secrets.ATESTSTRING }}

Output:
image

It would be greatly appreciated if it can be fixed. Thanks!

XML | Web.config | ##[error]Error: Failed to apply variable substitution

Hello. I can't seem to get Web.config XML variable substitution to work.

My .yml file has the following:

- uses: microsoft/variable-substitution@v1 
  with:
    files: '${{ github.workspace }}/WebApplication1/WebApplication3/Web.config'
  env:
    configuration.appSettings.foo: "b"

I also tried appSettings.foo: "b"

And I have the following in Web.config XML file:

<?xml...>
<configuration>
...
  <appSettings>
    <add key="foo" value="a"></add>
  </appSettings>
...
</configuration>

See the sample repo here: https://github.com/gojanpaolo/wm-pipeline-sb

Thank you.

I tested with a JSON file but dit nto work

Unfortunatelly the log doe snot say much, it only says the following:

Applying variable substitution on JSON file: myfile.json
Skipped updating file: myfile.json
##[error]Error: Failed to apply variable substitution
It's hard to tell why my file was skipped or even which variable failed

Replacement suggestion

This project is deprecated. Is there any substitution from Microsoft? I assume Microsoft just rename this to something else but I can't find one with a quick search

Changing the split character for variable substitution

Currently, the only character that is used for splitting an environment variable name to be used for variable substitution is the period. However, a period is not a valid character for an environment variable name in many *nix shells, including Bash. It would be very beneficial if we could override this separator or if it would split by either a period or an underscore (which is a valid character in an environment variable in all *nix shells).

How get to array element?

I have this json:

{
  "AllowedHosts": "*",
  "Serilog": {
    "Using": [],
    "MinimunLevel": {
      "Default": "Information",
      "Override": {
        "Microsoft": "Warning",
        "System": "Warning"
      }
    },
    "Enrich": [ "FromLogContext", "WithMachineName", "WithProcessId", "WithThreadId" ],
    "WriteTo": [
      { "Name": "Console" },
      {
        "Name": "Logger",
        "Args": {
          "configureLogger": {
            "WriteTo": [
              {
                "Name": "MSSqlServer",
                "Args": {
                  "connectionString": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=dbName;Integrated Security=True;",
                  "tableName": "Log"
                }
              }
            ],
            "Filter": [
              {
                "Name": "ByIncludingOnly",
                "Args": { "expression": "StartsWith(@Properties['SourceContext'] , 'some data') or @Exception is not null" }
              }
            ]
          }
        }
      }
    ]
  }
}

I want to replace connectionString. How to do it?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.