Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: input default values #8

Merged
merged 1 commit into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
44 changes: 30 additions & 14 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,33 @@ jobs:
version: ${{ steps.cz.outputs.version }}
steps:
- name: Check out
uses: actions/checkout@v4
uses: actions/checkout@v4.1.5
with:
fetch-depth: 0
token: "${{ secrets.ACCESS_TOKEN }}"
ref: "main"

- name: Set up Python
uses: actions/[email protected]
with:
python-version: 3.11

- name: Config Git User
run: |
git config --local user.email "$GIT_USER_EMAIL"
git config --local user.name "$GIT_USER_NAME"
git config --local pull.ff only

- id: cz
name: Create bump and changelog
- name: Set up Python
uses: actions/[email protected]
with:
python-version: 3.11

- name: Create bump and changelog
id: cz
run: |
python -m pip install -U commitizen
cz bump --yes
export REV=`cz version --project`
echo "version=\"v$REV\"" >> $GITHUB_OUTPUT

- name: Push changes
uses: ad-m/github-push-action@v0.6.0
uses: ad-m/github-push-action@v0.8.0
with:
github_token: ${{ secrets.ACCESS_TOKEN }}
repository: "Drafteame/sync-secrets-manager"
Expand All @@ -61,13 +61,29 @@ jobs:

build:
runs-on: ubuntu-latest
needs:
- bump_version
steps:
- name: 🛎 Checkout
- name: Checkout
uses: actions/checkout@v4
- name: Set up buildx

- name: Setup buildx
uses: docker/setup-buildx-action@v3
- name: Login to ghcr.io
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin

- name: Log in to GitHub container registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ github.token }}
logout: false

- name: Set repo name
run: |
repo=$(echo "ghcr.io/${{ github.repository }}" | tr '[:upper:]' '[:lower:]')
echo "REPO=$repo" >> $GITHUB_ENV

- name: Build and push
run: |
docker buildx build --platform linux/amd64,linux/arm64 -t $(echo "ghcr.io/${{ github.repository }}:latest" | tr '[:upper:]' '[:lower:]') --push .
docker buildx build --platform linux/amd64,linux/arm64 -t ${{ env.REPO }}:latest --push .
docker buildx build --platform linux/amd64,linux/arm64 -t ${{ env.REPO }}:${{ needs.bump_version.outputs.version }} --push .
File renamed without changes.
41 changes: 0 additions & 41 deletions CHANGELOG.md

This file was deleted.

4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:20
FROM node:20-alpine

LABEL "com.github.actions.icon"="blue"
LABEL "com.github.actions.color"="database"
Expand All @@ -9,6 +9,6 @@ LABEL "org.opencontainers.image.source"="https://github.com/Drafteame/sync-secre
COPY . /app
WORKDIR /app

RUN npm install
RUN npm install --omit=dev

ENTRYPOINT ["node", "/app/index.js"]
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ jobs:
create_secret: false # If true it will check if the secret exists or not to create it before execute sync (default false)
dry_run: true # Default false
show_values: false # If true secret values will be displayed on action logs (default false)
exclude: '^_' # Regular expression that excludes the matching keys to be synced (default '^_')
exclude: "^_" # Regular expression that excludes the matching keys to be synced (default '^_')
```
3 changes: 0 additions & 3 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,12 @@ inputs:
create_secret:
description: "Flag to create the given secret if not exist before execute sync"
required: false
default: "false"
dry_run:
description: "Dry run mode (preview changes without modifying the secret)"
required: false
default: "false"
show_values:
description: "Dry run mode (preview changes without modifying the secret)"
required: false
default: "false"
exclude:
description: "List of regular expressions that determines if a secret key should be excluded from sync"
required: false
Expand Down
32 changes: 10 additions & 22 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
import core from "@actions/core";
import { getInput, getBooleanInput } from "./src/utils.js";
import Action from "./src/action/Action.js";

const getAction = () => {
return new Action(
core.getInput("aws_access_key_id"),
core.getInput("aws_secret_access_key"),
core.getInput("aws_region"),
core.getInput("secret_name"),
core.getInput("json_file_path"),
core.getInput("exclude"),
core.getBooleanInput("show_values"),
core.getBooleanInput("create_secret"),
getInput("aws_access_key_id"),
getInput("aws_secret_access_key"),
getInput("aws_region"),
getInput("secret_name"),
getInput("json_file_path"),
getInput("exclude"),
getBooleanInput("show_values", false),
getBooleanInput("create_secret", false),
);
};

const run = async () => {
try {
setDefault("dry_run", "false");
setDefault("show_values", "false");
setDefault("create_secret", "false");

const dryRun = core.getBooleanInput("dry_run");
const dryRun = getBooleanInput("dry_run", false);

const changeSet = await getAction().run();

Expand All @@ -37,12 +33,4 @@ const run = async () => {
}
};

const setDefault = (name, value) => {
const envVarName = `INPUT_${name.replace(/ /g, '_').toUpperCase()}`;
const val = process.env[envVarName] || '';
if (val === '') {
process.env[envVarName] = value;
}
}

run();
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/action/Action.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fs from "fs";
import core from "@actions/core";

import SecretsManager from "../secrets-manager/SecretsManager.js";
import ChangeSet from "./ChangeSet.js";
Expand Down Expand Up @@ -119,6 +120,7 @@ export default class Action {
*/
async #createSecret() {
if (!this.#createSecretFlag) {
core.info("secret creation skip...");
return;
}

Expand Down
64 changes: 64 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import core from "@actions/core";

/**
* Checks if a given value is empty.
*
* @param {*} value - The value to check for emptiness.
* @returns {boolean} - Returns true if the value is empty, false otherwise.
*/
export function isEmpty(value) {
if (value == null || value == undefined) {
// Handles null and undefined
return true;
}

if (typeof value === "boolean") {
// Boolean values are never empty
return false;
}

if (typeof value === "number") {
// Number values are never empty
return false;
}

if (typeof value === "string") {
// Check if the string is empty
return value.trim().length === 0;
}

// For any other types, assume it's not empty
return false;
}

/**
* Get action input with default value
* @param {string} input input name to retrieve
* @param {string} value string default value
* @returns {string} The value of input, if is empty it return the default one
*/
export function getInput(input, value = "") {
const inputValue = core.getInput(input);

if (isEmpty(inputValue)) {
return value;
}

return inputValue;
}

/**
* Get action boolean input
* @param {string} input input name to retrieve
* @param {boolean} value boolean default value
* @returns {boolean} The value of the input, if it is empty ir return the default one
*/
export function getBooleanInput(input, value = false) {
const inputValue = core.getInput(input);

if (isEmpty(inputValue)) {
return value;
}

return inputValue.trim().toLowerCase() === "true";
}
Loading