From e59d29ab3b0ae2eebe0c32744c8b4a791c833287 Mon Sep 17 00:00:00 2001 From: Vladimir Starkov Date: Tue, 12 Jul 2022 10:21:56 +0200 Subject: [PATCH 1/3] Expose an actual SHA for pull requests as CI_PR_SHA(_SHORT). Default CI_SHA/GITHUB_SHA is the sha of _expect-to-be_ merge/squash/etc commit of the pull requests, and it can be very confusing. If you mark you artifacts with it, its very hard to trace back what was the actual commit. CI_PR_SHA is the way I solved it for my use cases and I can see im not the first one with this problem. Fixes #30 --- .github/workflows/demo.yml | 7 ++++++- README.md | 2 ++ index.js | 7 +++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/.github/workflows/demo.yml b/.github/workflows/demo.yml index 37007e2..2d32f03 100644 --- a/.github/workflows/demo.yml +++ b/.github/workflows/demo.yml @@ -28,6 +28,8 @@ jobs: echo "CI_BASE_REF=$CI_BASE_REF" echo "CI_SHA_SHORT=$CI_SHA_SHORT" echo "CI_SHA=$CI_SHA" + echo "CI_PR_SHA_SHORT=$CI_PR_SHA_SHORT" + echo "CI_PR_SHA=$CI_PR_SHA" echo "CI_PR_NUMBER=$CI_PR_NUMBER" echo "CI_PR_ID=$CI_PR_ID" echo "CI_PR_TITLE=$CI_PR_TITLE" @@ -77,6 +79,8 @@ jobs: echo "CI_BASE_REF=$Env:CI_BASE_REF" echo "CI_SHA_SHORT=$Env:CI_SHA_SHORT" echo "CI_SHA=$Env:CI_SHA" + echo "CI_PR_SHA_SHORT=$Env:CI_PR_SHA_SHORT" + echo "CI_PR_SHA=$Env:CI_PR_SHA" echo "CI_PR_NUMBER=$Env:CI_PR_NUMBER" echo "CI_PR_ID=$Env:CI_PR_ID" echo "CI_PR_TITLE=$Env:CI_PR_TITLE" @@ -122,10 +126,11 @@ jobs: echo "CI_REF=$CI_REF" echo "CI_HEAD_REF_SLUG=$CI_HEAD_REF_SLUG" echo "CI_HEAD_REF=$CI_HEAD_REF" - echo "CI_BASE_REF_SLUG=$CI_BASE_REF_SLUG" echo "CI_BASE_REF=$CI_BASE_REF" echo "CI_SHA_SHORT=$CI_SHA_SHORT" echo "CI_SHA=$CI_SHA" + echo "CI_PR_SHA_SHORT=$CI_PR_SHA_SHORT" + echo "CI_PR_SHA=$CI_PR_SHA" echo "CI_PR_NUMBER=$CI_PR_NUMBER" echo "CI_PR_ID=$CI_PR_ID" echo "CI_PR_TITLE=$CI_PR_TITLE" diff --git a/README.md b/README.md index 384c263..a7e70e7 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,8 @@ A [GitHub Action](https://github.com/features/actions) to expose useful environm | `CI_BASE_REF` | Only set for forked repositories / pull request. The branch of the base repository / the base branch name. Copy of `GITHUB_BASE_REF` - for reasons of completeness. | `main` | | `CI_SHA_SHORT` | The shortened commit SHA (8 characters) that triggered the workflow. | `ffac537e` | | `CI_SHA` | The commit SHA that triggered the workflow. Copy of `GITHUB_SHA` - for reasons of completeness. | `ffac537e6cbbf934b08745a378932722df287a53` | +| `CI_PR_SHA_SHORT` | The shortened latest commit SHA in the pull request's base branch. Short version of `CI_PR_SHA`. Only set for pull requests. | `010b249` | +| `CI_PR_SHA` | The latest commit SHA in the pull request's base branch. Long version of `CI_PR_SHA_SHORT`. Only set for pull requests. | `010b2491902d50e8623934f5bc43763ff5991642` | | `CI_PR_NUMBER` | The number of the pull request. Only set for pull requests. | `42` | | `CI_PR_ID` | Copy of `CI_PR_NUMBER` for completeness. | `42` | | `CI_PR_TITLE` | The title of the pull request. Only set for pull requests. | `Add feature xyz.` | diff --git a/index.js b/index.js index 36bbea4..4830fd4 100644 --- a/index.js +++ b/index.js @@ -186,6 +186,13 @@ try { const pullRequest = github.context.payload && github.context.payload.pull_request; if (pullRequest) { + const prSha = github.event.pull_request.head.sha; + core.exportVariable('CI_PR_SHA_SHORT', getShaShort(prSha)); + core.info(`Set CI_PR_SHA_SHORT=${process.env.CI_PR_SHA_SHORT}`); + + core.exportVariable('CI_PR_SHA', prSha); + core.info(`Set CI_PR_SHA=${process.env.CI_PR_SHA}`); + const prNumber = pullRequest.number; core.exportVariable('CI_PR_NUMBER', prNumber); core.info(`Set CI_PR_NUMBER=${process.env.CI_PR_NUMBER}`); From 28582532534f64a90a9c6cdae15df52a5b4a56b3 Mon Sep 17 00:00:00 2001 From: Franz Diebold Date: Sat, 16 Jul 2022 09:38:57 +0000 Subject: [PATCH 2/3] Improve Makefile: Add help texts. --- Makefile | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index fc59bbf..43fe1a6 100644 --- a/Makefile +++ b/Makefile @@ -1,15 +1,19 @@ +.PHONY: help +help: ## Show this help. + @egrep '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-22s\033[0m %s\n", $$1, $$2}' + .PHONY: install -install: +install: ## Install dependencies. npm install .PHONY: lint -lint: +lint: ## Lint code. npm run lint .PHONY: test -test: +test: ## Run tests. npm run test .PHONY: build -build: +build: ## Build code for distribution. npm run build From d1440854fc43b64831deb5441ba6be8e16215172 Mon Sep 17 00:00:00 2001 From: Franz Diebold Date: Sat, 16 Jul 2022 09:39:41 +0000 Subject: [PATCH 3/3] Prepare for release. --- .github/workflows/demo.yml | 1 + README.md | 2 +- dist/index.js | 7 +++++++ package-lock.json | 4 ++-- package.json | 2 +- 5 files changed, 12 insertions(+), 4 deletions(-) diff --git a/.github/workflows/demo.yml b/.github/workflows/demo.yml index 2d32f03..fb4d409 100644 --- a/.github/workflows/demo.yml +++ b/.github/workflows/demo.yml @@ -126,6 +126,7 @@ jobs: echo "CI_REF=$CI_REF" echo "CI_HEAD_REF_SLUG=$CI_HEAD_REF_SLUG" echo "CI_HEAD_REF=$CI_HEAD_REF" + echo "CI_BASE_REF_SLUG=$CI_BASE_REF_SLUG" echo "CI_BASE_REF=$CI_BASE_REF" echo "CI_SHA_SHORT=$CI_SHA_SHORT" echo "CI_SHA=$CI_SHA" diff --git a/README.md b/README.md index a7e70e7..6c5ba5e 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ [![GitHub Action: View on Marketplace](https://img.shields.io/badge/GitHub%20Action-View_on_Marketplace-28a745?logo=github)](https://github.com/marketplace/actions/github-environment-variables-action) [![Demo: available](https://img.shields.io/badge/Demo-available-orange)](.github/workflows/demo.yml) -[![Version: v2.4.0](https://img.shields.io/badge/Version-v2.4.0-brightgreen)](https://github.com/FranzDiebold/github-env-vars-action/releases/tag/v2.4.0) +[![Version: v2.5.0](https://img.shields.io/badge/Version-v2.5.0-brightgreen)](https://github.com/FranzDiebold/github-env-vars-action/releases/tag/v2.5.0) [![Lint and Test](https://github.com/FranzDiebold/github-env-vars-action/workflows/Lint%20and%20Test/badge.svg)](https://github.com/FranzDiebold/github-env-vars-action/actions?query=workflow%3A%22Lint+and+Test%22) [![license: MIT](https://img.shields.io/badge/license-MIT-brightgreen.svg)](./LICENSE) diff --git a/dist/index.js b/dist/index.js index 44030f4..996f6c0 100644 --- a/dist/index.js +++ b/dist/index.js @@ -192,6 +192,13 @@ try { const pullRequest = github.context.payload && github.context.payload.pull_request; if (pullRequest) { + const prSha = github.event.pull_request.head.sha; + core.exportVariable('CI_PR_SHA_SHORT', getShaShort(prSha)); + core.info(`Set CI_PR_SHA_SHORT=${process.env.CI_PR_SHA_SHORT}`); + + core.exportVariable('CI_PR_SHA', prSha); + core.info(`Set CI_PR_SHA=${process.env.CI_PR_SHA}`); + const prNumber = pullRequest.number; core.exportVariable('CI_PR_NUMBER', prNumber); core.info(`Set CI_PR_NUMBER=${process.env.CI_PR_NUMBER}`); diff --git a/package-lock.json b/package-lock.json index 493f2e5..b7c9b34 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "github-env-vars-action", - "version": "2.4.0", + "version": "2.5.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "github-env-vars-action", - "version": "2.4.0", + "version": "2.5.0", "license": "MIT", "dependencies": { "@actions/core": "^1.9.0", diff --git a/package.json b/package.json index 3f73b0e..54bb08d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "github-env-vars-action", - "version": "2.4.0", + "version": "2.5.0", "description": "A GitHub Action to expose useful environment variables.", "main": "index.js", "scripts": {