Skip to content

Commit

Permalink
Create md-table-action binary for use in a GitHub Actions environment
Browse files Browse the repository at this point in the history
  • Loading branch information
BrenekH committed Sep 18, 2024
1 parent 8237826 commit 8798e3d
Show file tree
Hide file tree
Showing 5 changed files with 204 additions and 9 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/release-on-tag.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
name: ${{ matrix.target }}
runs-on: ${{ matrix.os }}
env:
binary_name: markdown-table-generator${{ matrix.file_extension }}
binary_name: md-table-action${{ matrix.file_extension }}

steps:
- uses: actions/checkout@v4
Expand All @@ -40,7 +40,7 @@ jobs:
run: sudo apt-get install -y musl-tools

- name: Build
run: cargo build --release --target ${{ matrix.target }}
run: cargo build --release --target ${{ matrix.target }} --bin md-table-action --features action-runner-binary

- name: Create Archive Folder
run: mkdir ${{ runner.os }}
Expand Down
133 changes: 131 additions & 2 deletions Cargo.lock

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

16 changes: 15 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,21 @@ version = "0.1.0"
edition = "2021"

[dependencies]
clap = { version = "4.5.17", features = ["derive"] }
itertools = "0.13.0"
serde = { version = "1.0.210", features = ["derive"] }

# Dependencies for the CLI binary
clap = { version = "4.5.17", features = ["derive"] }
serde_json = "1.0.128"

# Dependencies just for GitHub Actions binary
gha_main = { version = "0.0.5", optional = true }
uuid = { version = "1.10.0", features = ["std", "v4"], optional = true }

[features]
actions-runner-binary = ["gha_main", "uuid"]

[[bin]]
name = "md-table-action"
path = "src/bin/md-table-action.rs"
required-features = ["actions-runner-binary"]
14 changes: 10 additions & 4 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@ description: A custom action written in Rust which generates markdown tables for
author: Brenek Harrison <[email protected]>

inputs:
file-path:
description: "The path to the JSON file to use"
required: true
token:
description: 'The GitHub token to use for downloading the action, defaults to workflow token'
description: 'The GitHub token to use for downloading the action binary, defaults to workflow token'
required: true
default: ${{ github.token }}
outputs:
output-file:
description: "The path of the generated markdown table file"
value: ${{ steps.run.outputs.outputfile }}
error:
description: 'The description of any error that occurred'
value: ${{ steps.run.outputs.error }}
Expand All @@ -18,9 +24,9 @@ runs:
- name: Set reusable variables
shell: bash
run: |
echo "action_repo=markdown-table-generator" >> $GITHUB_ENV
echo "action_repo=md-table-action" >> $GITHUB_ENV
echo "action_org=Locatarr" >> $GITHUB_ENV
echo "binary_name=markdown-table-generator" >> $GITHUB_ENV
echo "binary_name=md-table-action" >> $GITHUB_ENV
- name: Add binary extension
shell: bash
Expand Down Expand Up @@ -50,4 +56,4 @@ runs:
- name: Run Action
shell: bash
id: run
run: ./${{ runner.os }}/${{ env.binary_name }}
run: ./${{ runner.os }}/${{ env.binary_name }} "${{ inputs.file-path }}"
46 changes: 46 additions & 0 deletions src/bin/md-table-action.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use std::{env, path::PathBuf};

use gha_main::anyhow;
use gha_main::{gha_main, gha_output, GitHubActionResult};
use uuid::Uuid;

use markdown_table_generator::generate_md_table;
use markdown_table_generator::models::Applications;

#[gha_main]
fn main() -> GitHubActionResult {
let runner_temp_dir: PathBuf = env::var("RUNNER_TEMP")?.into();
let unique_id = Uuid::new_v4().to_string();

// Get a PathBuf with the input file path
let args: Vec<String> = env::args().collect();
let input_file = args
.get(1)
.ok_or(anyhow::anyhow!("Input file not provided"))?;
let input_filepath: PathBuf = input_file.into();

// Open the file and get a stream
let input_stream = std::fs::File::open(&input_filepath)?;

// Deserialize input file into correct struct and generate markdown table
let apps_json: Applications = serde_json::from_reader(input_stream)?;
let md_table = generate_md_table(&apps_json);

// Create output filepath in form "markdown-table-<original filename>.md"
// in the runner temporary directory (gets reset after each job, but persistent across steps).
// If there's an issue reading the filepath name, use a uuid v4 instead.
let in_file_stem = input_filepath
.file_stem()
.map(|a| a.to_str().unwrap_or(&unique_id))
.unwrap_or(&unique_id);
let out_file_path = runner_temp_dir.join("markdown-table-".to_owned() + in_file_stem + ".md");

// Write markdown table to output file
std::fs::write(&out_file_path, md_table)?;

// Export output file path as a GitHub Actions output for other steps to consume
let output_file = out_file_path.to_str().unwrap();
gha_output!(output_file);

Ok(())
}

0 comments on commit 8798e3d

Please sign in to comment.