Skip to content

Commit 72c5970

Browse files
authored
actions/source: Add action (#3447)
Signed-off-by: Ryan Northey <ryan@synca.io>
1 parent 29698f8 commit 72c5970

File tree

5 files changed

+248
-0
lines changed

5 files changed

+248
-0
lines changed

actions/source/action.yml

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
name: Create Source Tarball
2+
description: Creates a source tarball from a directory with version substitution and configurable exclusions
3+
author: phlax
4+
5+
inputs:
6+
debug:
7+
type: boolean
8+
default: false
9+
exclude-patterns:
10+
type: string
11+
default: |
12+
.git*
13+
output-name:
14+
type: string
15+
required: true
16+
source-path:
17+
type: string
18+
required: true
19+
template-command:
20+
type: string
21+
default: |
22+
export VERSION_FILE=\"\(.version_file)\"
23+
export OUTPUT_NAME=\"\(.output_name)\"
24+
export WORKING_DIRECTORY=\"\(.working_directory)\"
25+
export EXCLUDES=\"\($excludes)\"
26+
export TRANSFORM_ARG=\"\($transform_arg)\"
27+
export SOURCE_PATH=\"\(.source_path)\"
28+
OUTPUT=$(${SOURCE_ACTION_PATH}/source.sh)
29+
transform:
30+
type: string
31+
default: ""
32+
version-file:
33+
type: string
34+
default: VERSION.txt
35+
working-directory:
36+
type: string
37+
default: .
38+
39+
outputs:
40+
tarball:
41+
value: ${{ fromJSON(steps.create-tarball.outputs.output).tarball }}
42+
version:
43+
value: ${{ fromJSON(steps.create-tarball.outputs.output).version }}
44+
45+
46+
runs:
47+
using: composite
48+
steps:
49+
- uses: envoyproxy/toolshed/actions/bson@c73c1b842058331a14008432c0e162b97cccb2f5
50+
id: create-tarball
51+
env:
52+
SOURCE_ACTION_PATH: ${{ github.action_path }}
53+
DEBUG: ${{ inputs.debug == 'true' && '1' || '' }}
54+
with:
55+
input: |
56+
version_file: ${{ inputs.version-file }}
57+
output_name: ${{ inputs.output-name }}
58+
transform: ${{ inputs.transform }}
59+
exclude_patterns: ${{ inputs.exclude-patterns }}
60+
source_path: ${{ inputs.source-path }}
61+
working_directory: ${{ inputs.working-directory }}
62+
filter: |
63+
.version_file as $version_file
64+
| .output_name as $output_name
65+
| (.transform // "") as $transform
66+
| (.exclude_patterns // "") as $exclude_patterns
67+
| .source_path as $source_path
68+
| .working_directory as $working_directory
69+
| ($exclude_patterns | split("\n") | map(select(length > 0)) | map("--exclude=\(.)") | join(" ")) as $excludes
70+
| (if $transform != "" then "--transform=\($transform)" else "" end) as $transform_arg
71+
| "${{ inputs.template-command }}"
72+
| bash::output
73+
options: -r
74+
result-filter: |
75+
.
76+
| split("\n")
77+
| map(select(length > 0))
78+
| map(split("=") | {key: .[0], value: .[1]})
79+
| from_entries

actions/source/source.sh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env bash
2+
3+
set -e -o pipefail
4+
5+
VERSION=$(tr -d '\n' < "$VERSION_FILE")
6+
TARBALL_NAME="${OUTPUT_NAME//\{version\}/$VERSION}"
7+
8+
cd "$WORKING_DIRECTORY"
9+
10+
TAR_CMD=(tar)
11+
read -ra EXCLUDE_ARGS <<< "$EXCLUDES"
12+
TAR_CMD+=("${EXCLUDE_ARGS[@]}")
13+
if [[ -n "$TRANSFORM_ARG" ]]; then
14+
TRANSFORM_EXPANDED="${TRANSFORM_ARG//\{version\}/$VERSION}"
15+
TAR_CMD+=("$TRANSFORM_EXPANDED")
16+
fi
17+
TAR_CMD+=(-czf "$TARBALL_NAME" "$SOURCE_PATH")
18+
19+
if [[ -n "$DEBUG" ]]; then
20+
echo "VERSION: ${VERSION}" >&2
21+
echo "TARBALL_NAME: ${TARBALL_NAME}" >&2
22+
echo "EXCLUDES: ${EXCLUDES}" >&2
23+
echo "TRANSFORM: ${TRANSFORM_ARG}" >&2
24+
echo "RUN: ${TAR_CMD[*]}" >&2
25+
fi
26+
"${TAR_CMD[@]}"
27+
28+
echo "tarball=${TARBALL_NAME}"
29+
echo "version=${VERSION}"
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
uses: ./actions/source
2+
id: source
3+
with:
4+
debug: true
5+
exclude-patterns: |
6+
.git*
7+
output-name: test-v{version}.tar.gz
8+
source-path: test-source/
9+
version-file: VERSION.txt
10+
11+
before:
12+
- shell: bash
13+
run: |
14+
# Create a test directory structure
15+
mkdir -p test-source
16+
echo "1.0.0" > VERSION.txt
17+
echo "test content" > test-source/file.txt
18+
mkdir -p test-source/subdir
19+
echo "subdir content" > test-source/subdir/file2.txt
20+
echo "should be excluded" > test-source/.gitignore
21+
22+
after:
23+
- shell: bash
24+
run: |
25+
. "${ACTION_TEST_PATH}/test_fun.sh"
26+
27+
TARBALL="%{{ steps.source.outputs.tarball }}"
28+
VERSION="%{{ steps.source.outputs.version }}"
29+
30+
test_tarball_exists "$TARBALL"
31+
test_tarball_content "$TARBALL" "test-source/"
32+
test_version_output "$VERSION" "1.0.0"
33+
34+
# Verify .gitignore was excluded
35+
if tar -tzf "$TARBALL" | grep -q ".gitignore"; then
36+
echo "fail:Excluded file (.gitignore) found in tarball" >> "$TEST_OUTPUT"
37+
else
38+
echo "success:Excluded file (.gitignore) not in tarball" >> "$TEST_OUTPUT"
39+
fi

actions/source/tests/test_fun.sh

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/usr/bin/env bash
2+
3+
set -e -o pipefail
4+
5+
6+
test_tarball_exists () {
7+
local tarball="${1}"
8+
if [[ -z "$tarball" ]]; then
9+
echo "fail:Tarball output is empty" >> "$TEST_OUTPUT"
10+
return
11+
fi
12+
if [[ ! -f "$tarball" ]]; then
13+
echo "fail:Tarball not created: $tarball" >> "$TEST_OUTPUT"
14+
return
15+
fi
16+
echo "success:Tarball created successfully: $tarball" >> "$TEST_OUTPUT"
17+
}
18+
19+
test_tarball_content () {
20+
local tarball="${1}"
21+
local expected_pattern="${2}"
22+
23+
if [[ -z "$tarball" ]]; then
24+
echo "fail:Tarball parameter is empty" >> "$TEST_OUTPUT"
25+
return
26+
fi
27+
28+
# List contents of tarball
29+
if ! tar -tzf "$tarball" | head -5 > /tmp/tarball_contents.txt; then
30+
echo "fail:Failed to list tarball contents" >> "$TEST_OUTPUT"
31+
return
32+
fi
33+
34+
# Check if expected pattern exists in contents
35+
if grep -q "$expected_pattern" /tmp/tarball_contents.txt; then
36+
echo "success:Tarball contains expected pattern: $expected_pattern" >> "$TEST_OUTPUT"
37+
else
38+
echo "fail:Tarball does not contain expected pattern: $expected_pattern" >> "$TEST_OUTPUT"
39+
cat /tmp/tarball_contents.txt
40+
return
41+
fi
42+
}
43+
44+
test_version_output () {
45+
local version="${1}"
46+
local expected="${2}"
47+
48+
if [[ -z "$version" ]]; then
49+
echo "fail:Version output is empty" >> "$TEST_OUTPUT"
50+
return
51+
fi
52+
53+
if [[ "$version" != "$expected" ]]; then
54+
echo "fail:Wrong version. Expected '$expected', got '$version'" >> "$TEST_OUTPUT"
55+
return
56+
fi
57+
58+
echo "success:Version output correct: $version" >> "$TEST_OUTPUT"
59+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
uses: ./actions/source
2+
id: source
3+
with:
4+
debug: true
5+
output-name: myapp-v{version}.tar.gz
6+
source-path: test-dir/
7+
transform: s,^test-dir,myapp-{version},
8+
version-file: VERSION.txt
9+
10+
before:
11+
- shell: bash
12+
run: |
13+
# Create a test directory structure
14+
mkdir -p test-dir
15+
echo "2.5.0" > VERSION.txt
16+
echo "test content" > test-dir/file.txt
17+
18+
after:
19+
- shell: bash
20+
run: |
21+
. "${ACTION_TEST_PATH}/test_fun.sh"
22+
23+
TARBALL="%{{ steps.source.outputs.tarball }}"
24+
VERSION="%{{ steps.source.outputs.version }}"
25+
26+
test_tarball_exists "$TARBALL"
27+
test_version_output "$VERSION" "2.5.0"
28+
29+
# Check that transform was applied - directory should be renamed
30+
if tar -tzf "$TARBALL" | grep -q "^myapp-2.5.0/"; then
31+
echo "success:Transform applied correctly (myapp-2.5.0/ found)" >> "$TEST_OUTPUT"
32+
else
33+
echo "fail:Transform not applied correctly" >> "$TEST_OUTPUT"
34+
tar -tzf "$TARBALL" | head -5
35+
fi
36+
37+
# Verify original path is not in tarball
38+
if tar -tzf "$TARBALL" | grep -q "^test-dir/"; then
39+
echo "fail:Original path (test-dir/) still in tarball" >> "$TEST_OUTPUT"
40+
else
41+
echo "success:Original path not in tarball" >> "$TEST_OUTPUT"
42+
fi

0 commit comments

Comments
 (0)