mirror of
https://github.com/rlespinasse/github-slug-action.git
synced 2026-05-17 18:35:07 +00:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
955b5ba456 | ||
|
|
ba00826602 | ||
|
|
797666298f | ||
|
|
56d132125c |
139
.github/workflows/check-variables.yml
vendored
Normal file
139
.github/workflows/check-variables.yml
vendored
Normal file
@@ -0,0 +1,139 @@
|
||||
---
|
||||
name: Compare GitHub Variables
|
||||
|
||||
on:
|
||||
workflow_dispatch: # Manual trigger
|
||||
schedule:
|
||||
- cron: "0 0 * * 1" # Weekly on Monday at midnight
|
||||
pull_request:
|
||||
|
||||
permissions: read-all
|
||||
|
||||
jobs:
|
||||
compare-variables:
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
issues: write
|
||||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Load environment variables for testing
|
||||
uses: rlespinasse/github-slug-action@v5
|
||||
with:
|
||||
prefix: TEST_
|
||||
|
||||
- name: Compare GitHub variables
|
||||
id: compare
|
||||
run: |
|
||||
# Initialize arrays to store results
|
||||
MISMATCHED_VARS=()
|
||||
TEST_VALUES=()
|
||||
ACTUAL_VALUES=()
|
||||
|
||||
# Get all environment variables
|
||||
ALL_ENV=$(env)
|
||||
|
||||
# Extract TEST_GITHUB variables
|
||||
TEST_VARS=$(echo "$ALL_ENV" | grep "^TEST_GITHUB_" | cut -d= -f1)
|
||||
|
||||
# Check for mismatches
|
||||
for TEST_VAR in $TEST_VARS; do
|
||||
# Get the corresponding GITHUB variable name
|
||||
GITHUB_VAR=${TEST_VAR/TEST_GITHUB_/GITHUB_}
|
||||
|
||||
# Get the values
|
||||
TEST_VALUE="${!TEST_VAR}"
|
||||
|
||||
# Check if the GITHUB variable exists
|
||||
if [[ -n "${!GITHUB_VAR+x}" ]]; then
|
||||
GITHUB_VALUE="${!GITHUB_VAR}"
|
||||
|
||||
# Compare values
|
||||
if [[ "$TEST_VALUE" != "$GITHUB_VALUE" ]]; then
|
||||
MISMATCHED_VARS+=("$TEST_VAR vs $GITHUB_VAR")
|
||||
TEST_VALUES+=("$TEST_VALUE")
|
||||
ACTUAL_VALUES+=("$GITHUB_VALUE")
|
||||
echo "Mismatch found: $TEST_VAR=$TEST_VALUE, $GITHUB_VAR=$GITHUB_VALUE"
|
||||
else
|
||||
echo "Match: $TEST_VAR=$TEST_VALUE, $GITHUB_VAR=$GITHUB_VALUE"
|
||||
fi
|
||||
else
|
||||
echo "Skipping $TEST_VAR as $GITHUB_VAR does not exist"
|
||||
fi
|
||||
done
|
||||
|
||||
# Set output for next steps
|
||||
if [[ ${#MISMATCHED_VARS[@]} -gt 0 ]]; then
|
||||
echo "has_mismatches=true" >>"$GITHUB_OUTPUT"
|
||||
|
||||
# Create a JSON array of mismatched variables for the issue
|
||||
MISMATCHED_JSON=$(printf '"%s",' "${MISMATCHED_VARS[@]}" | sed 's/,$//')
|
||||
MISMATCHED_JSON="[$MISMATCHED_JSON]"
|
||||
echo "mismatched_vars=$MISMATCHED_JSON" >>"$GITHUB_OUTPUT"
|
||||
|
||||
# Create a JSON array of test values
|
||||
TEST_JSON=$(printf '"%s",' "${TEST_VALUES[@]}" | sed 's/,$//')
|
||||
TEST_JSON="[$TEST_JSON]"
|
||||
echo "test_values=$TEST_JSON" >>"$GITHUB_OUTPUT"
|
||||
|
||||
# Create a JSON array of actual values
|
||||
ACTUAL_JSON=$(printf '"%s",' "${ACTUAL_VALUES[@]}" | sed 's/,$//')
|
||||
ACTUAL_JSON="[$ACTUAL_JSON]"
|
||||
echo "actual_values=$ACTUAL_JSON" >>"$GITHUB_OUTPUT"
|
||||
|
||||
# Create summary table
|
||||
{
|
||||
echo "## GitHub Variable Mismatches";
|
||||
echo "| Variable Pair | TEST Value | GITHUB Value |";
|
||||
echo "| ------------- | ---------- | ------------ |";
|
||||
} >>"$GITHUB_STEP_SUMMARY"
|
||||
|
||||
for i in "${!MISMATCHED_VARS[@]}"; do
|
||||
echo "| ${MISMATCHED_VARS[$i]} | ${TEST_VALUES[$i]} | ${ACTUAL_VALUES[$i]} |" >>"$GITHUB_STEP_SUMMARY"
|
||||
done
|
||||
else
|
||||
echo "has_mismatches=false" >>"$GITHUB_OUTPUT"
|
||||
echo "## All GitHub Variables Match" >>"$GITHUB_STEP_SUMMARY"
|
||||
echo "All action GITHUB_ variables match their official GITHUB_ counterparts." >>"$GITHUB_STEP_SUMMARY"
|
||||
fi
|
||||
shell: bash
|
||||
|
||||
- name: Create issue for mismatches
|
||||
if: steps.compare.outputs.has_mismatches == 'true' && github.event_name != 'pull_request'
|
||||
uses: actions/github-script@v7
|
||||
with:
|
||||
github-token: ${{ secrets.GITHUB_TOKEN }}
|
||||
script: |
|
||||
const mismatchedVars = JSON.parse('${{ steps.compare.outputs.mismatched_vars }}');
|
||||
const testValues = JSON.parse('${{ steps.compare.outputs.test_values }}');
|
||||
const actualValues = JSON.parse('${{ steps.compare.outputs.actual_values }}');
|
||||
|
||||
// Create table for issue body
|
||||
let tableBody = '| Variable Pair | TEST Value | GITHUB Value |\n';
|
||||
tableBody += '| ------------- | ---------- | ------------ |\n';
|
||||
|
||||
for (let i = 0; i < mismatchedVars.length; i++) {
|
||||
tableBody += `| ${mismatchedVars[i]} | ${testValues[i]} | ${actualValues[i]} |\n`;
|
||||
}
|
||||
|
||||
// Create the issue
|
||||
const issueTitle = `GitHub Variable Mismatches Detected - ${new Date().toISOString().split('T')[0]}`;
|
||||
const issueBody = `## GitHub Variable Mismatches Detected\n\nThe following mismatches were found between the action **GITHUB_** variables and their official **GITHUB_** counterparts:\n\n${tableBody}\n\nPlease review these discrepancies and update the test variables as needed.`;
|
||||
|
||||
await github.rest.issues.create({
|
||||
owner: context.repo.owner,
|
||||
repo: context.repo.repo,
|
||||
title: issueTitle,
|
||||
body: issueBody,
|
||||
labels: ['bug']
|
||||
});
|
||||
|
||||
console.log('Issue created for GitHub variable mismatches');
|
||||
|
||||
- name: Fail workflow if mismatches detected
|
||||
if: steps.compare.outputs.has_mismatches == 'true' && github.event_name == 'pull_request'
|
||||
run: |
|
||||
echo "::error::GitHub variable mismatches detected! See the summary for details."
|
||||
exit 1
|
||||
44
README.md
44
README.md
@@ -16,7 +16,7 @@ This GitHub Action will expose the slug/short values of [some GitHub environment
|
||||
|
||||
- `SLUG_URL` on a variable to have a `slug` variable compliant to be used in a URL
|
||||
- Like `SLUG` but `.`, and `_` are also replaced by `-`
|
||||
- `SHORT` on a variable will limit the string size to ~8 characters
|
||||
- `SHORT` on a variable will limit the string size to [~8 characters](#with-another-length-for-short-values)
|
||||
- Useful for _sha_ value
|
||||
- `<KEY>_PART` on a variable will give a part of a variable defined by a key
|
||||
- Like `GITHUB_REPOSITORY_OWNER_PART` for the owner part of `GITHUB_REPOSITORY`
|
||||
@@ -68,11 +68,19 @@ steps:
|
||||
- name: Inject enhanced GitHub environment variables
|
||||
uses: rlespinasse/github-slug-action@v5
|
||||
with:
|
||||
short-length: 7 # By default it's up to Git to decide, use 8 to have the v3.x behavior
|
||||
short-length: 7 # By default it's up to Git to decide, use 8 to have the v3.x behaviour
|
||||
```
|
||||
|
||||
The length of a short sha depends of the size of **your repository** and can differ over time :
|
||||
|
||||
- set `7` to keep the `small repository` behaviour,
|
||||
- set `8` to reproduce `v3` behaviour,
|
||||
- set `4` as the minimum length possible.
|
||||
|
||||
> [!WARNING]
|
||||
> If you leave it empty, you need to checkout the source first in order to let Git decide the size by itself.
|
||||
> If you leave it empty, you need to checkout the source first in order to let Git decide the size by itself by using [`git rev-parse`][git-revparse] behaviour.
|
||||
>
|
||||
> The default is the effective value of the [core.abbrev][git-core-abbrev] configuration variable.
|
||||
|
||||
## Available Environment variables
|
||||
|
||||
@@ -146,12 +154,12 @@ Same as slug variables but URL-compliant
|
||||
The **GITHUB_REF_NAME SLUG/SLUG_URL** variables doesn't work the same way as before
|
||||
|
||||
> [!TIP]
|
||||
> If you use `v5` or related versions, you need to use `GITHUB_REF_POINT` instead of `GITHUB_REF_NAME` to get the behavior of the `v4` action.
|
||||
> If you use `v5` or related versions, you need to use `GITHUB_REF_POINT` instead of `GITHUB_REF_NAME` to get the behaviour of the `v4` action.
|
||||
|
||||
Before `v5`, the behavior was the same as the GitHub one except on `pull_request*` workflows ([Ready the full story][issue-104]).
|
||||
Before `v5`, the behaviour was the same as the GitHub one except on `pull_request*` workflows ([Ready the full story][issue-104]).
|
||||
|
||||
- `${{ env.GITHUB_REF_NAME }}` will serve the behavior of this action,
|
||||
- `$GITHUB_REF_NAME` will serve the behavior of GitHub Action.
|
||||
- `${{ env.GITHUB_REF_NAME }}` will serve the behaviour of this action,
|
||||
- `$GITHUB_REF_NAME` will serve the behaviour of GitHub Action.
|
||||
|
||||
On `pull_request*` workflows, the content will be `<PR-number>/merge` instead of the branch name.
|
||||
So you need to use `GITHUB_REF_POINT` instead
|
||||
@@ -166,23 +174,23 @@ steps:
|
||||
|
||||
```
|
||||
|
||||
Then `${{ env.GITHUB_REF_POINT }}`, and `$GITHUB_REF_POINT` will serve the behavior of this action.
|
||||
And `${{ env.GITHUB_REF_NAME }}`, and `$GITHUB_REF_NAME` will serve the behavior of GitHub Action.
|
||||
Then `${{ env.GITHUB_REF_POINT }}`, and `$GITHUB_REF_POINT` will serve the behaviour of this action.
|
||||
And `${{ env.GITHUB_REF_NAME }}`, and `$GITHUB_REF_NAME` will serve the behaviour of GitHub Action.
|
||||
|
||||
### v3 to v4
|
||||
|
||||
Since `v4`, it's Git who manage the short variables by using [`git rev-parse`][git-revparse] behaviour.
|
||||
The length of a short sha depends of the size of our repository and can differ over time.
|
||||
The length of a short sha depends of the size of **your repository** and can differ over time.
|
||||
|
||||
To manage that moving length, you can use `short-length` input
|
||||
|
||||
- set `7` to reproduce `small repository` behavior
|
||||
- set `8` to reproduce `v3` behavior
|
||||
- set `7` to reproduce `small repository` behaviour
|
||||
- set `8` to reproduce `v3` behaviour
|
||||
|
||||
> [!WARNING]
|
||||
> The minimum length is 4, the default is the effective value of the [core.abbrev][git-core-abbrev] configuration variable.
|
||||
|
||||
So to reproduce previous behavior, use
|
||||
So to reproduce previous behaviour, use
|
||||
|
||||
```yaml
|
||||
steps:
|
||||
@@ -199,14 +207,14 @@ steps:
|
||||
> [!WARNING]
|
||||
> When you set a custom environment variable, you [cannot use any of the default environment variable names][naming-conventions]. For a complete list of these, see [Default environment variables][default-environment-variables]. **If you attempt to override the value of one of these default environment variables, the assignment is ignored.**
|
||||
|
||||
If a variable start to be used as default environment variable, the environment variable may have a different behavior than the expected one.
|
||||
If a variable start to be used as default environment variable, the environment variable may have a different behaviour than the expected one.
|
||||
|
||||
If this append, the `${{ env.GITHUB_AWESOME_VARIABLE }}` and `$GITHUB_AWESOME_VARIABLE` expression will not works in the same way.
|
||||
|
||||
- `${{ env.GITHUB_AWESOME_VARIABLE }}` will serve the behavior of this action,
|
||||
- `$GITHUB_AWESOME_VARIABLE` will serve the behavior of GitHub Action.
|
||||
- `${{ env.GITHUB_AWESOME_VARIABLE }}` will serve the behaviour of this action,
|
||||
- `$GITHUB_AWESOME_VARIABLE` will serve the behaviour of GitHub Action.
|
||||
|
||||
Otherwise the two expression will serve the behavior of this action.
|
||||
Otherwise the two expression will serve the behaviour of this action.
|
||||
This will not occurs if you use the `prefix` input to avoid the issue.
|
||||
|
||||
> [!IMPORTANT]
|
||||
@@ -240,6 +248,7 @@ In French :fr:
|
||||
|
||||
- [Mettre en place une CI/CD Angular avec GitHub Actions & Netlify][article-1]
|
||||
- [GitHub Actions : enfin des pipelines accessibles aux développeurs][talk-1]
|
||||
[GitHub-slug-action : 5 ans d'open source pour cette GitHub Action essentielle au CI/CD][article-6]
|
||||
|
||||
In Chinese :cn:
|
||||
|
||||
@@ -269,4 +278,5 @@ In Chinese :cn:
|
||||
[article-3]: https://barstool.engineering/serverless-deploy-previews-on-github-actions/
|
||||
[article-4]: https://javascript.plainenglish.io/lets-build-a-continuous-delivery-and-branching-process-c27dae09f0b6
|
||||
[article-5]: https://eryajf.github.io/HowToStartOpenSource/views/03-github-tips/10-Use-github-slug-action-to-leak-key-variables-in-the-Github-Action-context.html
|
||||
[article-6]: https://www.sfeir.dev/5-ans-de-github-slug-action-une-aventure-open-source/
|
||||
[talk-1]: https://www.youtube.com/watch?v=F5mBDmOQcvE
|
||||
|
||||
Reference in New Issue
Block a user