DEV Community

JL
JL

Posted on

GitHub Workflow - Action

Github Action
Custom Github Action is for performing task which is re-used. The implementation can be in JS. Repository of the example:
https://github.com/otter13/override-with-secrets-action

Example structure of Github Action folder

Image description

.github/actions/override-with-secrets-action/action.yaml
This is the interface to GitHub, specifying inputs.

name: 'override-with-secrets-action'
description: 'loverride values in input json with secrets'
inputs:
  jsonString:
    required: true
runs:
  using: 'node16'
  main: 'dist/index.js'
Enter fullscreen mode Exit fullscreen mode

Caller side in workflow

Code snippet for using the action in workflow

      - name: Checkout Source
        with:
          persist-credentials: false
        uses: actions/checkout@v3

      - name: aws-ssm-to-env
        uses: ./.github/actions/override-with-secrets-action
        with:
          jsonString: $

      - name: log envs
        env:
          output_secrets_override: ${{ env.output_secrets_override }}
        run: |
          echo $output_secrets_override
Enter fullscreen mode Exit fullscreen mode

.github/actions/override-with-secrets-action/src/index.js

const core = require('@actions/core');

const configureInputs = () => {
  const inputjsonString = core.getInput('jsonString');

  return {
    jsonString: inputjsonString,
  }
}
const override = async ({
  jsonString
})=> {
  var overridenJsonString = ''
  const obj = JSON.parse(jsonString)
  for (var key in obj) {
    if (obj.hasOwnProperty(key)) {
        // TODO: override here
        console.log(key + " -> " + obj[key]);
    }
  }
  return overridenJsonString
}


const saveToEnv = (overridenJsonString) => {
    core.exportVariable('output_secrets_override', overridenJsonString);
}

const run = async () => {
  try {
    const { jsonString } = configureInputs();
    const overridenJsonString = await override({jsonString});
    saveToEnv(overridenJsonString);
  } catch (error) {
    core.setFailed(error.message);
  }
}

run();
Enter fullscreen mode Exit fullscreen mode

Package.json
Specifying what packages and the build command

{
    "name": "override-with-secrets-action",
    "version": "1.0.0",
    "description": "",
    "main": "src/index.js",
    "scripts": {
        "build": "ncc build src/index.js -o dist"
    },
    "keywords": [],
    "author": "",
    "license": "ISC",
    "dependencies": {
        "@actions/core": "^1.6.0",
        "@actions/github": "^5.0.0",
        "build": "^0.1.4"
    },
    "devDependencies": {
        "@vercel/ncc": "^0.33.4"
    }
}

Enter fullscreen mode Exit fullscreen mode

Top comments (0)