Skip to content

Commit 84d7ac0

Browse files
authored
Merge pull request #7 from pirafrank/dev
v0.1.5
2 parents c83d193 + ba08bb3 commit 84d7ac0

File tree

7 files changed

+491
-631
lines changed

7 files changed

+491
-631
lines changed

.github/workflows/pr.yml

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ on:
1313

1414
jobs:
1515
build:
16-
runs-on: ubuntu-20.04
16+
name: Build and Test
17+
runs-on: ubuntu-22.04
1718

1819
steps:
1920
- name: Clone repo
@@ -50,6 +51,47 @@ jobs:
5051
env:
5152
GITHUB_TOKEN: ${{ secrets.GH_TKN }}
5253

54+
docker_amd64:
55+
name: Build and Test for linux/amd64
56+
runs-on: ubuntu-22.04
57+
steps:
58+
- name: Clone repo
59+
uses: actions/checkout@v4
60+
with:
61+
fetch-depth: 0
62+
63+
# docs: https://github.com/marketplace/actions/build-and-push-docker-images
64+
- name: Set up QEMU
65+
uses: docker/setup-qemu-action@v3
66+
67+
- name: Set up Docker Buildx
68+
uses: docker/setup-buildx-action@v3
69+
70+
# docs: https://docs.docker.com/build/ci/github-actions/test-before-push/
71+
- name: Build Docker image for linux/amd64
72+
uses: docker/build-push-action@v6
73+
with:
74+
push: false
75+
context: .
76+
file: Dockerfile
77+
platforms: linux/amd64
78+
load: true
79+
tags: ${{ github.repository }}:latest
80+
81+
- name: Run Docker tests for linux/amd64
82+
env:
83+
GITHUB_TOKEN: ${{ secrets.GH_TKN }}
84+
run: npm run docker:test
85+
86+
docker_arm64:
87+
name: Build and Test for linux/arm64
88+
runs-on: ubuntu-22.04
89+
steps:
90+
- name: Clone repo
91+
uses: actions/checkout@v4
92+
with:
93+
fetch-depth: 0
94+
5395
# docs: https://github.com/marketplace/actions/build-and-push-docker-images
5496
- name: Set up QEMU
5597
uses: docker/setup-qemu-action@v3
@@ -58,15 +100,17 @@ jobs:
58100
uses: docker/setup-buildx-action@v3
59101

60102
# docs: https://docs.docker.com/build/ci/github-actions/test-before-push/
61-
- name: Build Docker image
62-
uses: docker/build-push-action@v5
103+
- name: Build Docker image for linux/arm64
104+
uses: docker/build-push-action@v6
63105
with:
64106
push: false
65107
context: .
108+
file: Dockerfile
109+
platforms: linux/arm64
66110
load: true
67-
tags: github-commit-sign:latest
111+
tags: ${{ github.repository }}:latest
68112

69-
- name: Run Docker tests
113+
- name: Run Docker tests for linux/arm64
70114
env:
71115
GITHUB_TOKEN: ${{ secrets.GH_TKN }}
72116
run: npm run docker:test

.github/workflows/release.yml

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ on:
66

77
jobs:
88
publish:
9-
runs-on: ubuntu-20.04
9+
runs-on: ubuntu-22.04
1010
permissions:
1111
contents: read
1212
id-token: write
@@ -29,6 +29,31 @@ jobs:
2929
- name: Install dependencies
3030
run: npm ci
3131

32+
- name: Login to DockerHub
33+
uses: docker/login-action@v3
34+
with:
35+
username: ${{ secrets.DOCKERHUB_USERNAME }}
36+
password: ${{ secrets.DOCKERHUB_TOKEN }}
37+
38+
- name: Set up QEMU
39+
uses: docker/setup-qemu-action@v3
40+
with:
41+
platforms: linux/amd64,linux/arm64
42+
43+
- name: Set up Docker Buildx
44+
uses: docker/setup-buildx-action@v3
45+
-
46+
name: Build and push
47+
uses: docker/build-push-action@v6
48+
with:
49+
push: true
50+
tags: ${{ github.repository }}:latest,${{ github.repository }}:${{ github.event.release.tag_name }}
51+
context: .
52+
file: Dockerfile
53+
platforms: linux/amd64,linux/arm64
54+
55+
# publish to npm after docker image is built and pushed.
56+
# this to avoid publishing to npm registry before a possible docker image build failure
3257
- name: Publish to npm
3358
run: npm publish --provenance --access public
3459
env:

README.md

Lines changed: 97 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
# GitHub Commit Sign
22

3+
[![GitHub release](https://img.shields.io/github/release/pirafrank/github-commit-sign.svg?style=flat-square)](https://github.com/pirafrank/github-commit-sign/releases/latest)
4+
[![GitHub marketplace](https://img.shields.io/badge/marketplace-github--commit--sign-blue?logo=github&style=flat-square)](https://github.com/marketplace/actions/github-commit-sign)
5+
[![Docker pulls](https://img.shields.io/docker/pulls/pirafrank/github-commit-sign.svg?style=flat-square)](https://hub.docker.com/r/pirafrank/github-commit-sign)
6+
[![npm](https://img.shields.io/npm/v/@pirafrank/github-commit-sign.svg?style=flat-square)](https://www.npmjs.com/package/@pirafrank/github-commit-sign)
7+
38
A thin wrapper to perform signed commits to a GitHub repository through their GraphQL APIs. Useful to create signed commits in CI/CD environments.
49

5-
Offered as node module and CLI tool.
10+
## Available as
11+
12+
- [GitHub Action](https://github.com/marketplace/actions/github-commit-sign)
13+
- [Docker image](https://hub.docker.com/r/pirafrank/github-commit-sign)
14+
- [CLI tool](#cli-usage)
15+
- [npm module](https://www.npmjs.com/package/@pirafrank/github-commit-sign)
616

717
## Why
818

@@ -21,33 +31,107 @@ I have actually written this to get signed commits in GitHub Actions running [he
2131

2232
## ⚠️ Before you start
2333

24-
- `GITHUB_TOKEN` must be set as environment variable. It must have write access to the repository you want to commit to
34+
### About `GITHUB_TOKEN`
35+
36+
In GitHub Actions the `GITHUB_TOKEN` is [automatically generated](https://docs.github.com/en/actions/security-guides/automatic-token-authentication) per each run and is available as an environment variable. For the commit action to work, the `GITHUB_TOKEN` must be set as environment variable and it must have *write* access to the repository you want to commit to.
37+
38+
The following applies, based on the context you are running the action in:
39+
40+
- **GitHub Actions**: If the repository is the same where your workflow run, you can either:
41+
- Configure it by adding the following to your workflow YAML file (restricted priviledges, recommended):
42+
43+
```yaml
44+
permissions:
45+
contents: write
46+
```
47+
48+
- Set it up for all workflows in your repository (wider priviledges, not recommended): Go to *Repository Settings > Actions > General > Workflow permissions*, and set `Read and write permissions`.
49+
50+
- **GitHub Actions**: if you need to commit to other repositories, you may need to override the default `GITHUB_TOKEN` with a personal access token with the `repo` scope. Go to *Profile > Settings > Developer settings > Personal access tokens > Token (classic)*, and Generate new token (classic) with the full-control over `repo` scope.
51+
- **Tip**: store the generated token in repository secrets!
52+
- **Docker image, npm module, or CLI**: when running outside of GitHub Actions, set an environment variable called `GITHUB_TOKEN` with the token value having full-control over `repo` scope.
53+
54+
### Usage assumptions
55+
2556
- Changed (or new) files must exist locally
2657
- for practial reasons, those files must have the same file name and file path as the ones in the repository you are replacing with your commit (or the same file name and file path you want them to have in the repository)
2758
- Deleted files may not exist locally, and their path may just be provided as argument
2859
- GraphQL APIs are not meant to be used to push a lot of code! If that is your case, please consider using a local clone and `git`.
2960

30-
## Requirements
61+
## GitHub Action usage
62+
63+
You can use this module as a GitHub Action. It is a Docker-based action.
64+
65+
### Print help
66+
67+
```yaml
68+
# Print help
69+
- name: Print help
70+
uses: pirafrank/github-commit-sign@v0
71+
with:
72+
args: "--help"
73+
```
74+
75+
### Commit changes
76+
77+
Requirements when running in a GitHub Actions workflow:
78+
79+
- `GITHUB_TOKEN` must be set as environment variable and it must have write access to the repository you want to commit to. Read the *Before you start* section above for more details.
80+
- `--changed` and `--deleted` may have multiple file paths, as a single string with space-separated values, or by repeating the option per each file path. All file paths must be relative to the repository root.
81+
82+
```yaml
83+
# Commit changes...
84+
- name: Commit changes
85+
id: commit_changes
86+
uses: pirafrank/github-commit-sign@v0
87+
if: ${{ vars.RUN_COMMIT_CHANGES == 'true' }}
88+
env:
89+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
90+
with:
91+
args: "commit --owner=${{ github.repository_owner }} --repo=${{ github.event.repository.name }} --branch=${{ github.ref_name }} --commitMessage='this is a webflow signed commit' --changed new.txt dummy/subdir/changed.txt --deleted dummy/delete_me.txt another_deleted.txt"
92+
# ...then use output details in another step
93+
- name: Print git commit output
94+
if: ${{ vars.RUN_COMMIT_CHANGES == 'true' }}
95+
run: |
96+
echo "Run command: ${{ steps.commit_changes.outputs.command }}"
97+
echo "Commit URL: ${{ steps.commit_changes.outputs.commitUrl }}"
98+
99+
```
100+
101+
> [!TIP]
102+
> You may want to create string format list of added and changed files in a previous step in your workflow.
103+
104+
### Other commands
105+
106+
The action accepts the same commands you can provide to the CLI. Pass them as a single string to the `args` input. Read below for more details.
31107

32-
- Node.js (18+)
108+
## Docker image
109+
110+
You can use this module as a Docker image. It is a multi-arch image, so it should run on any architecture.
111+
112+
The image is available on Docker Hub as `pirafrank/github-commit-sign`.
113+
114+
The image run the CLI instance of the program, thus accepting the same commands you can provide to the CLI. Pass them as you'd do with any other Docker image.
115+
116+
## CLI usage
117+
118+
### Requirements
119+
120+
- Node.js (18.18+)
33121
- A GitHub token with the `repo` scope.
34122
- The token must be set in the environment variable called `GITHUB_TOKEN`.
35123

36-
Note: in GitHub Actions the `GITHUB_TOKEN` is automatically generated per each run and is available as an environment variable. More info [here](https://docs.github.com/en/actions/security-guides/automatic-token-authentication).
37-
38-
## Installation
124+
### Installation
39125

40126
```sh
41-
npm install
127+
npm install -g @pirafrank/github-commit-sign
42128
```
43129

44-
## CLI
45-
46130
### Usage examples
47131

48132
```sh
49133
export GITHUB_TOKEN='your_github_token_here'
50-
node github.js commit \
134+
ggh commit \
51135
--owner yourname \
52136
--repo some_repo_of_yours \
53137
--branch main \
@@ -57,7 +141,7 @@ node github.js commit \
57141

58142
```sh
59143
export GITHUB_TOKEN='your_github_token_here'
60-
node github.js commit \
144+
ggh commit \
61145
--owner yourname \
62146
--repo some_repo_of_yours \
63147
--branch main \
@@ -71,7 +155,7 @@ Multi-file commit is also possible:
71155

72156
```sh
73157
export GITHUB_TOKEN='your_github_token_here'
74-
node github.js commit \
158+
ggh commit \
75159
--owner yourname \
76160
--repo some_repo_of_yours \
77161
--branch main \
@@ -105,54 +189,6 @@ init();
105189

106190
Please refer to `index.js` for the function signatures.
107191

108-
## GitHub Action usage
109-
110-
You can use this module as a GitHub Action. It is a Docker-based action.
111-
112-
### Print help
113-
114-
```yaml
115-
# Print help
116-
- name: Print help
117-
uses: pirafrank/github-commit-sign@v0
118-
with:
119-
args: "--help"
120-
```
121-
122-
### Commit changes
123-
124-
Requirements when running in a GitHub Actions workflow:
125-
126-
- `--changed` and `--deleted` may have multiple file paths, as a single string with space-separated values, or by repeating the option per each file path. All file paths must be relative to the repository root.
127-
- `GITHUB_TOKEN` must be set in the environment variables with write access to the repository. Go to *Repository Settings > Actions > General > Workflow permissions*, and set `Read and write permissions`.
128-
129-
To commit to other repositories, you may need to override the default `GITHUB_TOKEN` with a personal access token with the `repo` scope. Go to *Profile > Settings > Developer settings > Personal access tokens > Token (classic)*, and Generate new token (classic) with the full-control over `repo` scope. Tip: store the generated token in repository secrets.
130-
131-
```yaml
132-
# Commit changes...
133-
- name: Commit changes
134-
id: commit_changes
135-
uses: pirafrank/github-commit-sign@v0
136-
if: ${{ vars.RUN_COMMIT_CHANGES == 'true' }}
137-
env:
138-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
139-
with:
140-
args: "commit --owner=${{ github.repository_owner }} --repo=${{ github.event.repository.name }} --branch=${{ github.ref_name }} --commitMessage='this is a webflow signed commit' --changed new.txt dummy/subdir/changed.txt --deleted dummy/delete_me.txt another_deleted.txt"
141-
# ...then use output details in another step
142-
- name: Print git commit output
143-
if: ${{ vars.RUN_COMMIT_CHANGES == 'true' }}
144-
run: |
145-
echo "Run command: ${{ steps.commit_changes.outputs.command }}"
146-
echo "Commit URL: ${{ steps.commit_changes.outputs.commitUrl }}"
147-
148-
```
149-
150-
Tip: you may create the strings with the list of added and changed files from a previous step in your workflow.
151-
152-
### Other commands
153-
154-
The action accepts the same commands you can provied to the CLI. Pass them as a single string to the `args` input.
155-
156192
## Tests
157193

158194
Create a `.env` file with your `repo`-scoped `GITHUB_TOKEN`, then run:

github.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#!/usr/bin/env node
2+
13
const fs = require("fs");
24
const yargs = require("yargs");
35
const CURRENT_VERSION = require("./package.json").version;
@@ -10,9 +12,8 @@ const {
1012
checkIfBranchExists,
1113
} = require("./index");
1214

13-
1415
const commitCommand = "commit";
15-
const branchCommand = "branch"
16+
const branchCommand = "branch";
1617
const knownCommands = [commitCommand, branchCommand];
1718

1819
const appendLineToFile = (filename, line) => {
@@ -156,9 +157,7 @@ yargs
156157
checkIfBranchExists(owner, repo, branch)
157158
.then((response) => {
158159
const n = response ? "a" : "no";
159-
info(
160-
`Repository ${owner}/${repo} has ${n} branch named '${branch}'`
161-
);
160+
info(`Repository ${owner}/${repo} has ${n} branch named '${branch}'`);
162161
writeResultToGithubOutputFile([
163162
{
164163
label: "command",

0 commit comments

Comments
 (0)