Skip to content

Commit 934b0f7

Browse files
committed
feat(local-workflow-actions)!: get source repo of reusable workflow from job context
Signed-off-by: Emilien Escalle <emilien.escalle@escemi.com>
1 parent 80a7dfb commit 934b0f7

3 files changed

Lines changed: 49 additions & 74 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: 18 additions & 28 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,22 +39,18 @@ 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
5147

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.
48+
# The reusable workflow repository that defines the current job, in the format `owner/repo`.
49+
# If not provided, this defaults to `job.workflow_repository`.
5950
repository: ""
6051

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.
52+
# The Git ref (branch, tag, or SHA) to check out from the reusable workflow repository.
53+
# If not provided, this defaults to `job.workflow_sha`.
6354
ref: ""
6455
```
6556
@@ -72,12 +63,11 @@ Example: if `local-path` is `./self-workflow` and `actions-path` is `.github/act
7263
| **Input** | **Description** | **Required** | **Default** |
7364
| ------------------ | ------------------------------------------------------------------------------------------------------------------ | ------------ | ----------------- |
7465
| **`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. | | |
66+
| | The same relative path will be available under `../self-workflow` relative to `github.workspace`. | | |
67+
| **`repository`** | The reusable workflow repository that defines the current job, in the format `owner/repo`. | **false** | - |
68+
| | If not provided, this defaults to `job.workflow_repository`. | | |
69+
| **`ref`** | The Git ref (branch, tag, or SHA) to check out from the reusable workflow repository. | **false** | - |
70+
| | If not provided, this defaults to `job.workflow_sha`. | | |
8171

8272
<!-- inputs:end -->
8373

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

8979
## Outputs
9080

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. |
81+
| **Output** | **Description** |
82+
| ---------------- | ---------------------------------------------------------------------------------- |
83+
| **`repository`** | The reusable workflow repository that was checked out, in the format `owner/repo`. |
84+
| **`ref`** | The Git ref or SHA of the reusable workflow repository that was checked out. |
9585

9686
<!-- outputs:end -->
9787

Lines changed: 30 additions & 45 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,47 +14,37 @@ 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"
3020
repository:
3121
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.
22+
The reusable workflow repository that defines the current job, in the format `owner/repo`.
23+
If not provided, this defaults to `job.workflow_repository`.
3424
required: false
3525
ref:
3626
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.
27+
The Git ref (branch, tag, or SHA) to check out from the reusable workflow repository.
28+
If not provided, this defaults to `job.workflow_sha`.
3929
required: false
4030

4131
outputs:
4232
repository:
4333
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 }}
34+
value: ${{ inputs.repository || job.workflow_repository }}
4535
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 }}
36+
description: The Git ref or SHA of the reusable workflow repository that was checked out.
37+
value: ${{ inputs.ref || job.workflow_sha }}
4838

4939
runs:
5040
using: "composite"
5141
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-
5742
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
5843
with:
5944
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 }}
45+
path: ./.self-workflow-source
46+
repository: ${{ inputs.repository || job.workflow_repository }}
47+
ref: ${{ inputs.ref || job.workflow_sha }}
6348
sparse-checkout: |
6449
${{ inputs.actions-path }}
6550
@@ -68,20 +53,20 @@ runs:
6853
script: |
6954
const fs = require('fs');
7055
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-
};
56+
const workspacePath = process.env.GITHUB_WORKSPACE;
57+
if (!workspacePath) {
58+
throw new Error('GITHUB_WORKSPACE is not set.');
59+
}
60+
61+
const sourcePath = path.resolve(workspacePath, '.self-workflow-source');
62+
const destinationPath = path.resolve(workspacePath, '../self-workflow');
63+
64+
if (fs.existsSync(destinationPath)) {
65+
fs.rmSync(sourcePath, { recursive: true, force: true });
66+
core.info(`Reusable workflow actions already available at ${destinationPath}.`);
67+
return;
68+
}
8669
87-
['.gitignore', '.dockerignore'].forEach(ensureIgnore);
70+
fs.mkdirSync(path.dirname(destinationPath), { recursive: true });
71+
fs.renameSync(sourcePath, destinationPath);
72+
core.info(`Moved reusable workflow actions to ${destinationPath}.`);

0 commit comments

Comments
 (0)