Skip to content

Commit 5f58fdc

Browse files
authored
feat: customizable permissions in GitHubActionStep (#1017)
This PR adds configuration passthrough for the `permissions` object that `GitHubActionStep` uses under the hood. The default remains `contents: write`. I have also added a snapshot test showing that the change works, and made a small modification to the README. Fixes #731
1 parent d2f2c6c commit 5f58fdc

File tree

6 files changed

+411
-4
lines changed

6 files changed

+411
-4
lines changed

API.md

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,9 @@ If you want to call a GitHub Action in a step, you can utilize the `GitHubAction
441441

442442
The `jobSteps` array is placed into the pipeline job at the relevant `jobs.<job_id>.steps` as [documented here](https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idsteps).
443443

444+
GitHub Actions Job permissions can be modified by passing the `permissions` object to `GitHubActionStep`.
445+
The default set of permissions is simply `contents: write`.
446+
444447
In this example,
445448

446449
```ts
@@ -461,6 +464,10 @@ const pipeline = new GitHubWorkflow(app, 'Pipeline', {
461464
const stage = new MyStage(app, 'Beta', { env: BETA_ENV });
462465
pipeline.addStage(stage, {
463466
pre: [new GitHubActionStep('PreBetaDeployAction', {
467+
permissions: {
468+
idToken: JobPermission.WRITE,
469+
contents: JobPermission.WRITE,
470+
},
464471
jobSteps: [
465472
{
466473
name: 'Checkout',

src/pipeline.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -832,7 +832,7 @@ export class GitHubWorkflow extends PipelineBase {
832832
definition: {
833833
name: step.id,
834834
...this.renderJobSettingParameters(),
835-
permissions: {
835+
permissions: step.permissions ?? {
836836
contents: github.JobPermission.WRITE,
837837
},
838838
runsOn: this.runner.runsOn,

src/steps/github-action-step.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Step } from 'aws-cdk-lib/pipelines';
2-
import { JobStep } from '../workflows-model';
2+
import { JobStep, JobPermissions } from '../workflows-model';
33

44
export interface GitHubActionStepProps {
55
/**
@@ -11,6 +11,12 @@ export interface GitHubActionStepProps {
1111
* Environment variables to set.
1212
*/
1313
readonly env?: Record<string, string>;
14+
15+
/**
16+
* Permissions for the GitHub Action step.
17+
* @default The job receives 'contents: write' permissions. If you set additional permissions and require 'contents: write', it must be provided in your configuration.
18+
*/
19+
readonly permissions?: JobPermissions;
1420
}
1521

1622
/**
@@ -19,10 +25,12 @@ export interface GitHubActionStepProps {
1925
export class GitHubActionStep extends Step {
2026
public readonly env: Record<string, string>;
2127
public readonly jobSteps: JobStep[];
28+
public readonly permissions?: JobPermissions;
2229

2330
constructor(id: string, props: GitHubActionStepProps) {
2431
super(id);
2532
this.jobSteps = props.jobSteps;
2633
this.env = props.env ?? {};
34+
this.permissions = props.permissions;
2735
}
2836
}

0 commit comments

Comments
 (0)