Skip to content

Commit 2579c60

Browse files
committed
feat(local-workflow-actions)!: get source repo of reusable workflow from job context
Signed-off-by: Emilien Escalle <[email protected]>
1 parent 451f2f5 commit 2579c60

3 files changed

Lines changed: 44 additions & 85 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Opinionated GitHub Actions and reusable workflows for foundational continuous-in
3131
- [Get matrix outputs](actions/get-matrix-outputs/README.md) - aggregates outputs across matrix jobs for downstream steps.
3232
- [Local actions](actions/local-actions/README.md) - exposes sibling local actions for a composite action and cleans them up automatically.
3333
- [Set matrix output](actions/set-matrix-output/README.md) - writes structured outputs that can be consumed by other matrix jobs.
34-
- [Local workflow actions](actions/local-workflow-actions/README.md) - loads reusable workflow actions from the current repository.
34+
- [Local workflow actions](actions/local-workflow-actions/README.md) - exposes local actions from the reusable workflow repository.
3535

3636
### Repository insights & utilities
3737

actions/local-workflow-actions/README.md

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,10 @@
2424

2525
## Overview
2626

27-
This action checks out the reusable workflow repository that triggered the current run and copies its local actions directory into the current workspace.
28-
It runs both during the main step and in the post step so that actions with cleanup hooks are also available.
27+
This action checks out the reusable workflow repository that defines the current job and moves it to `../self-workflow` relative to `github.workspace`.
2928
Use it when consuming reusable workflows that reference local actions from the same repository—they are not automatically available in the caller repository and must be synced manually.
30-
Add the `self-workflow` directory to your `.gitignore` and `.dockerignore` files to avoid committing it by mistake.
31-
32-
**This action requires the permission: `id-token: write`**.
33-
34-
Local actions will be available at `./<local-path>/<actions-path>` inside the current workspace.
35-
Example: if `local-path` is `./self-workflow` and `actions-path` is `.github/actions`, then local actions will be available at `./self-workflow/.github/actions`.
29+
Local actions will be available at `../self-workflow/<actions-path>` relative to `github.workspace`.
30+
Example: if `actions-path` is `.github/actions`, then local actions will be available at `../self-workflow/.github/actions`.
3631

3732
<!-- overview:end -->
3833

@@ -44,23 +39,11 @@ Example: if `local-path` is `./self-workflow` and `actions-path` is `.github/act
4439
- uses: hoverkraft-tech/ci-github-common/actions/local-workflow-actions@71b85947453f32b5d147ff3ab37351439a92d840 # 0.34.2
4540
with:
4641
# Relative path(s) (inside the workflow repository) containing the local actions to expose in the current workspace.
47-
# The same relative path will be used inside the current workspace (for example `.github/actions`).
42+
# The same relative path will be available under `../self-workflow` relative to `github.workspace`
43+
# (for example `../self-workflow/.github/actions`).
4844
#
4945
# Default: `.github/actions`
5046
actions-path: .github/actions
51-
52-
# Path inside the current workspace where to copy the local actions from the reusable workflow repository.
53-
#
54-
# Default: `./self-workflow`
55-
local-path: ./self-workflow
56-
57-
# The reusable workflow repository that triggered the current run, in the format `owner/repo`.
58-
# If not provided, this is automatically filled by the OIDC action.
59-
repository: ""
60-
61-
# The Git ref (branch, tag, or SHA) of the reusable workflow repository that triggered the current run.
62-
# If not provided, this is automatically filled by the OIDC action.
63-
ref: ""
6447
```
6548
6649
<!-- usage:end -->
@@ -72,12 +55,7 @@ Example: if `local-path` is `./self-workflow` and `actions-path` is `.github/act
7255
| **Input** | **Description** | **Required** | **Default** |
7356
| ------------------ | ------------------------------------------------------------------------------------------------------------------ | ------------ | ----------------- |
7457
| **`actions-path`** | Relative path(s) (inside the workflow repository) containing the local actions to expose in the current workspace. | **false** | `.github/actions` |
75-
| | The same relative path will be used inside the current workspace (for example `.github/actions`). | | |
76-
| **`local-path`** | Path inside the current workspace where to copy the local actions from the reusable workflow repository. | **false** | `./self-workflow` |
77-
| **`repository`** | The reusable workflow repository that triggered the current run, in the format `owner/repo`. | **false** | - |
78-
| | If not provided, this is automatically filled by the OIDC action. | | |
79-
| **`ref`** | The Git ref (branch, tag, or SHA) of the reusable workflow repository that triggered the current run. | **false** | - |
80-
| | If not provided, this is automatically filled by the OIDC action. | | |
58+
| | The same relative path will be available under `../self-workflow` relative to `github.workspace`. | | |
8159

8260
<!-- inputs:end -->
8361

@@ -88,10 +66,11 @@ Example: if `local-path` is `./self-workflow` and `actions-path` is `.github/act
8866

8967
## Outputs
9068

91-
| **Output** | **Description** |
92-
| ---------------- | ------------------------------------------------------------------------------------------- |
93-
| **`repository`** | The reusable workflow repository that was checked out, in the format `owner/repo`. |
94-
| **`ref`** | The Git ref (branch, tag, or SHA) of the reusable workflow repository that was checked out. |
69+
| **Output** | **Description** |
70+
| ---------------- | ---------------------------------------------------------------------------------- |
71+
| **`repository`** | The reusable workflow repository that was checked out, in the format `owner/repo`. |
72+
| **`ref`** | The Git ref or SHA of the reusable workflow repository that was checked out. |
73+
| **`path`** | The resolved path where the reusable workflow actions are available. |
9574

9675
<!-- outputs:end -->
9776

Lines changed: 32 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
name: "Local workflow actions"
22
description: |
3-
This action checks out the reusable workflow repository that triggered the current run and copies its local actions directory into the current workspace.
4-
It runs both during the main step and in the post step so that actions with cleanup hooks are also available.
3+
This action checks out the reusable workflow repository that defines the current job and moves it to `../self-workflow` relative to `github.workspace`.
54
Use it when consuming reusable workflows that reference local actions from the same repository—they are not automatically available in the caller repository and must be synced manually.
6-
Add the `self-workflow` directory to your `.gitignore` and `.dockerignore` files to avoid committing it by mistake.
7-
8-
**This action requires the permission: `id-token: write`**.
9-
10-
Local actions will be available at `./<local-path>/<actions-path>` inside the current workspace.
11-
Example: if `local-path` is `./self-workflow` and `actions-path` is `.github/actions`, then local actions will be available at `./self-workflow/.github/actions`.
5+
Local actions will be available at `../self-workflow/<actions-path>` relative to `github.workspace`.
6+
Example: if `actions-path` is `.github/actions`, then local actions will be available at `../self-workflow/.github/actions`.
127
138
author: hoverkraft
149
branding:
@@ -19,69 +14,54 @@ inputs:
1914
actions-path:
2015
description: |
2116
Relative path(s) (inside the workflow repository) containing the local actions to expose in the current workspace.
22-
The same relative path will be used inside the current workspace (for example `.github/actions`).
17+
The same relative path will be available under `../self-workflow` relative to `github.workspace` (for example `../self-workflow/.github/actions`).
2318
required: false
2419
default: ".github/actions"
25-
local-path:
26-
description: |
27-
Path inside the current workspace where to copy the local actions from the reusable workflow repository.
28-
required: false
29-
default: "./self-workflow"
30-
repository:
31-
description: |
32-
The reusable workflow repository that triggered the current run, in the format `owner/repo`.
33-
If not provided, this is automatically filled by the OIDC action.
34-
required: false
35-
ref:
36-
description: |
37-
The Git ref (branch, tag, or SHA) of the reusable workflow repository that triggered the current run.
38-
If not provided, this is automatically filled by the OIDC action.
39-
required: false
4020

4121
outputs:
4222
repository:
4323
description: The reusable workflow repository that was checked out, in the format `owner/repo`.
44-
value: ${{ inputs.repository || steps.oidc.outputs.job_workflow_repo_name_and_owner }}
24+
value: ${{ job.workflow_repository }}
4525
ref:
46-
description: The Git ref (branch, tag, or SHA) of the reusable workflow repository that was checked out.
47-
value: ${{ inputs.ref || steps.oidc.outputs.job_workflow_repo_ref }}
26+
description: The Git ref or SHA of the reusable workflow repository that was checked out.
27+
value: ${{ job.workflow_sha }}
28+
path:
29+
description: The resolved path where the reusable workflow actions are available.
30+
value: ${{ steps.prepare.outputs.path }}
4831

4932
runs:
5033
using: "composite"
5134
steps:
52-
# FIXME: This is a workaround for having workflow actions. See https://github.com/orgs/community/discussions/38659
53-
- id: oidc
54-
if: inputs.repository == ''
55-
uses: ChristopherHX/oidc@73eee1ff03fdfce10eda179f617131532209edbd # v3
56-
5735
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
5836
with:
5937
persist-credentials: false
60-
path: ${{ inputs.local-path }}
61-
repository: ${{ inputs.repository || steps.oidc.outputs.job_workflow_repo_name_and_owner }}
62-
ref: ${{ inputs.ref || steps.oidc.outputs.job_workflow_repo_ref }}
38+
path: ./.self-workflow-source
39+
repository: ${{ job.workflow_repository }}
40+
ref: ${{ job.workflow_sha }}
6341
sparse-checkout: |
6442
${{ inputs.actions-path }}
6543
66-
- uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
44+
- id: prepare
45+
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
6746
with:
6847
script: |
6948
const fs = require('fs');
7049
const path = require('path');
71-
const entry = 'self-workflow';
72-
const ensureIgnore = (fileName) => {
73-
const filePath = path.join(process.cwd(), fileName);
74-
if (fs.existsSync(filePath)) {
75-
const content = fs.readFileSync(filePath, 'utf8');
76-
const lines = content.split(/\r?\n/);
77-
if (lines.some((line) => line.trim() === entry)) {
78-
return;
79-
}
80-
const suffix = content.endsWith('\n') ? '' : '\n';
81-
fs.writeFileSync(filePath, `${content}${suffix}${entry}\n`);
82-
} else {
83-
fs.writeFileSync(filePath, `${entry}\n`);
84-
}
85-
};
50+
const workspacePath = process.env.GITHUB_WORKSPACE;
51+
if (!workspacePath) {
52+
throw new Error('GITHUB_WORKSPACE is not set.');
53+
}
54+
55+
const sourcePath = path.resolve(workspacePath, '.self-workflow-source');
56+
const destinationPath = path.resolve(workspacePath, '../self-workflow');
57+
core.setOutput('path', destinationPath);
58+
59+
if (fs.existsSync(destinationPath)) {
60+
fs.rmSync(sourcePath, { recursive: true, force: true });
61+
core.info(`Reusable workflow actions already available at ${destinationPath}.`);
62+
return;
63+
}
8664
87-
['.gitignore', '.dockerignore'].forEach(ensureIgnore);
65+
fs.mkdirSync(path.dirname(destinationPath), { recursive: true });
66+
fs.renameSync(sourcePath, destinationPath);
67+
core.info(`Moved reusable workflow actions to ${destinationPath}.`);

0 commit comments

Comments
 (0)