Merge pull request #155 from actions/classic-grantbirki-driveby

General Repository Improvements
This commit is contained in:
James M. Greene
2023-04-18 00:43:13 -05:00
committed by GitHub
25 changed files with 1568 additions and 1428 deletions

View File

@@ -13,5 +13,5 @@
"rules": {
"semi": ["error", "never"]
},
"ignorePatterns": ["/dist/", "/pre/"]
"ignorePatterns": ["/dist/"]
}

1
.gitattributes vendored Normal file
View File

@@ -0,0 +1 @@
dist/** -diff linguist-generated=true

View File

@@ -21,12 +21,13 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Checkout
uses: actions/checkout@v3
- name: Set Node.js 16.x
- name: Setup Node.JS
uses: actions/setup-node@v3
with:
node-version: 16.x
node-version-file: '.node-version'
cache: npm
- name: Install dependencies
@@ -36,9 +37,17 @@ jobs:
run: npm run prepare
- name: Compare the expected and actual dist/ directories
id: diff
run: |
if [ "$(git diff --ignore-space-at-eol dist/ | wc -l)" -gt "0" ]; then
echo "Detected uncommitted changes after build in dist folder. See status below:"
git diff
exit 1
fi
# If index.js was different than expected, upload the expected version as an artifact
- uses: actions/upload-artifact@v3
if: ${{ failure() && steps.diff.conclusion == 'failure' }}
with:
name: dist
path: dist/

View File

@@ -1,4 +1,4 @@
name: Checking formatting
name: Check formatting
on:
push:
@@ -24,8 +24,8 @@ jobs:
- name: Setup Node.JS
uses: actions/setup-node@v3
with:
node-version: 16.x
cache: 'npm'
node-version-file: '.node-version'
cache: npm
- name: Install dependencies
run: npm ci

View File

@@ -1,4 +1,4 @@
name: Check linter
name: Check linting
on:
push:
@@ -24,8 +24,8 @@ jobs:
- name: Setup Node.JS
uses: actions/setup-node@v3
with:
node-version: 16.x
cache: 'npm'
node-version-file: '.node-version'
cache: npm
- name: Install dependencies
run: npm ci

View File

@@ -9,7 +9,7 @@
# the `language` matrix defined below to confirm you have the correct set of
# supported CodeQL languages.
#
name: 'CodeQL'
name: CodeQL
on:
push:
@@ -37,7 +37,7 @@ jobs:
# Learn more about CodeQL language support at https://git.io/codeql-language-support
steps:
- name: Checkout repository
- name: Checkout
uses: actions/checkout@v3
# Initializes the CodeQL tools for scanning.

View File

@@ -11,7 +11,6 @@ jobs:
draft-release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: release-drafter/release-drafter@v5
- uses: release-drafter/release-drafter@569eb7ee3a85817ab916c8f8ff03a5bd96c9c83e # v5.23.0
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

View File

@@ -29,8 +29,8 @@ jobs:
- name: Setup Node.JS
uses: actions/setup-node@v3
with:
node-version: 16.x
cache: 'npm'
node-version-file: '.node-version'
cache: npm
- name: Install dependencies
run: npm ci

View File

@@ -1,4 +1,4 @@
name: Run Tests
name: Run tests
on:
push:
@@ -19,8 +19,8 @@ jobs:
- name: Setup Node.JS
uses: actions/setup-node@v3
with:
node-version: 16.x
cache: 'npm'
node-version-file: '.node-version'
cache: npm
- name: Install dependencies
run: npm ci

2
.gitignore vendored
View File

@@ -64,4 +64,4 @@ typings/
.env
# next.js build output
.next
.next

1
.node-version Normal file
View File

@@ -0,0 +1 @@
18.9.0

View File

@@ -1 +0,0 @@
* @actions/pages

29
CONTRIBUTING.md Normal file
View File

@@ -0,0 +1,29 @@
# Contributing 💻
All contributions are welcome and greatly appreciated!
## Steps to Contribute 💡
> Check the `.node-version` file in the root of this repo so see what version of Node.js is required for local development - note, this can be different from the version of Node.js which runs the Action on GitHub runners. It is suggested to download [nodenv](https://github.com/nodenv/nodenv) which uses this file and manages your Node.js versions for you
1. Fork this repository
2. Make your changes
3. [Test](#testing-) your changes locally
4. Before opening a pull request, please run `npm run all` to verify formatting, linting, tests, generated files, etc.
5. Commit and push your changes to your fork
6. Open a pull request back to this repository
7. Wait for an approval or changes requested from the maintainers of this repository
After merging the pull request, the maintainers of this repository will create a new release with those changes included. After that, everyone can utilize the newly integrated changes in their own Actions workflows and enjoy your awesome improvements!
## Testing 🧪
### Running the test suite (required)
Simply run the following command to execute the entire test suite:
```bash
npm test
```
> Note: This requires that you have already run `npm install`.

View File

@@ -1,10 +1,8 @@
# deploy-pages
# deploy-pages 🚀
This action is used to deploy [Actions artifacts][artifacts] to GitHub Pages.
[![Release](https://img.shields.io/github/v/release/actions/deploy-pages?label=Release&logo=github)](https://github.com/actions/deploy-pages/releases/latest) [![Linting](https://img.shields.io/github/actions/workflow/status/actions/deploy-pages/check-linter.yml?label=Linting&logo=github)](https://github.com/actions/deploy-pages/actions/workflows/check-linter.yml) [![Formatting](https://img.shields.io/github/actions/workflow/status/actions/deploy-pages/check-formatting.yml?label=Formatting&logo=github)](https://github.com/actions/deploy-pages/actions/workflows/check-formatting.yml) [![Tests](https://img.shields.io/github/actions/workflow/status/actions/deploy-pages/test.yml?label=Tests&logo=github)](https://github.com/actions/deploy-pages/actions/workflows/test.yml) ![Coverage](./coverage_badge.svg) [![Distributables](https://img.shields.io/github/actions/workflow/status/actions/deploy-pages/check-dist.yml?label=Distributables&logo=github)](https://github.com/actions/deploy-pages/actions/workflows/check-dist.yml) [![CodeQL](https://img.shields.io/github/actions/workflow/status/actions/deploy-pages/codeql-analysis.yml?label=CodeQL&logo=github)](https://github.com/actions/deploy-pages/actions/workflows/codeql-analysis.yml)
## Scope
⚠️ Official support for building Pages with Actions is in public beta at the moment.
This action is used to deploy [Actions artifacts][artifacts] to [GitHub Pages](https://pages.github.com/).
## Usage
@@ -43,10 +41,39 @@ jobs:
steps:
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v1
uses: actions/deploy-pages@v2 # or the latest "vX.X.X" version tag for this action
```
## Security considerations
### Inputs 📥
| Input | Required? | Default | Description |
| ----- | --------- | ------- | ----------- |
| `token` | `true` | `${{ github.token }}` | The GitHub token used to create an authenticated client - Provided for you by default! |
| `emit_telemetry` | `false` | `"false"` | Should this action only emit build telemetry instead of deploying the build artifact? |
| `conclusion` | `false` | - | The status of the previous build |
| `timeout` | `false` | `"600000"` | Time in milliseconds after which to timeout and cancel the deployment (default: 10 minutes) |
| `error_count` | `false` | `"10"` | Maximum number of status report errors before cancelling a deployment (default: 10) |
| `reporting_interval` | `false` | `"5000"` | Time in milliseconds between two deployment status report (default: 5 seconds) |
| `artifact_name` | `false` | `"github-pages"` | The name of the artifact to deploy |
| `preview` | `false` | `"false"` | Is this attempting to deploy a pull request as a GitHub Pages preview site? (NOTE: This feature is only in alpha currently and is not available to the public!) |
### Outputs 📤
| Output | Description |
| ------ | ----------- |
| `page_url` | The URL of the deployed Pages site |
### Environment Variables 🌎
| Variable | Description |
| -------- | ----------- |
| `GITHUB_PAGES` | This environment variable is created and set to the string value `"true"` so that framework build tools may choose to differentiate their output based on the intended target hosting platform. |
## Scope
⚠️ Official support for building Pages with Actions is in public beta at the moment.
## Security Considerations
There are a few important considerations to be aware of:
@@ -60,9 +87,9 @@ There are a few important considerations to be aware of:
4. If your Pages site is using a source branch, the deployment must originate from this source branch unless [your environment is protected][environment-protection] in which case the environment protection rules take precedence over the source branch rule
5. If your Pages site is using GitHub Actions as the source, while not required we highly recommend you also [protect your environment][environment-protection] (we do it by default for you)
5. If your Pages site is using GitHub Actions as the source, while not required we highly recommend you also [protect your environment][environment-protection] (we will configure it by default for you).
# Release instructions
## Release Instructions
In order to release a new version of this Action:

1
coverage_badge.svg Normal file
View File

@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="116" height="20" role="img" aria-label="Coverage: 72.41%"><title>Coverage: 72.41%</title><linearGradient id="s" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><clipPath id="r"><rect width="116" height="20" rx="3" fill="#fff"/></clipPath><g clip-path="url(#r)"><rect width="63" height="20" fill="#555"/><rect x="63" width="53" height="20" fill="#e05d44"/><rect width="116" height="20" fill="url(#s)"/></g><g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="110"><text aria-hidden="true" x="325" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="530">Coverage</text><text x="325" y="140" transform="scale(.1)" fill="#fff" textLength="530">Coverage</text><text aria-hidden="true" x="885" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="430">72.41%</text><text x="885" y="140" transform="scale(.1)" fill="#fff" textLength="430">72.41%</text></g></svg>

After

Width:  |  Height:  |  Size: 1.1 KiB

22
dist/index.js generated vendored
View File

@@ -4949,7 +4949,7 @@ FetchError.prototype.name = 'FetchError';
let convert;
try {
convert = (__nccwpck_require__(2319).convert);
convert = (__nccwpck_require__(2877).convert);
} catch (e) {}
const INTERNALS = Symbol('Body internals');
@@ -9781,7 +9781,7 @@ function wrappy (fn, cb) {
/***/ }),
/***/ 9432:
/***/ 572:
/***/ ((module, __unused_webpack_exports, __nccwpck_require__) => {
const core = __nccwpck_require__(2186)
@@ -9791,7 +9791,7 @@ const { RequestError } = __nccwpck_require__(537)
const HttpStatusMessages = __nccwpck_require__(3703)
// All variables we need from the runtime are loaded here
const getContext = __nccwpck_require__(1319)
const getContext = __nccwpck_require__(8454)
async function processRuntimeResponse(res, requestOptions) {
// Parse the response body as JSON
@@ -9958,7 +9958,7 @@ module.exports = {
/***/ }),
/***/ 1319:
/***/ 8454:
/***/ ((module, __unused_webpack_exports, __nccwpck_require__) => {
const core = __nccwpck_require__(2186)
@@ -9995,19 +9995,19 @@ module.exports = function getContext() {
/***/ }),
/***/ 2877:
/***/ 2634:
/***/ ((module, __unused_webpack_exports, __nccwpck_require__) => {
const core = __nccwpck_require__(2186)
// All variables we need from the runtime are loaded here
const getContext = __nccwpck_require__(1319)
const getContext = __nccwpck_require__(8454)
const {
getSignedArtifactUrl,
createPagesDeployment,
getPagesDeploymentStatus,
cancelPagesDeployment
} = __nccwpck_require__(9432)
} = __nccwpck_require__(572)
const temporaryErrorStatus = {
unknown_status: 'Unable to get deployment status.',
@@ -10098,6 +10098,7 @@ class Deployment {
}
throw new Error(errorMessage)
} else {
// istanbul ignore next
throw error
}
}
@@ -10202,6 +10203,7 @@ class Deployment {
async cancel() {
// Don't attempt to cancel if no deployment was created
if (!this.deploymentInfo || this.deploymentInfo.pending !== true) {
core.debug('No deployment to cancel')
return
}
@@ -10229,7 +10231,7 @@ module.exports = { Deployment }
/***/ }),
/***/ 2319:
/***/ 2877:
/***/ ((module) => {
module.exports = eval("require")("encoding");
@@ -10412,8 +10414,8 @@ var __webpack_exports__ = {};
const core = __nccwpck_require__(2186)
const { Deployment } = __nccwpck_require__(2877)
const getContext = __nccwpck_require__(1319)
const { Deployment } = __nccwpck_require__(2634)
const getContext = __nccwpck_require__(8454)
const deployment = new Deployment()

2
dist/index.js.map generated vendored

File diff suppressed because one or more lines are too long

2552
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,6 +1,6 @@
{
"name": "deploy-pages",
"version": "0.0.1",
"version": "0.0.0",
"description": "Deploy an actions artifact to GitHub Pages",
"main": "./dist/index.js",
"dependencies": {
@@ -17,10 +17,12 @@
"eslint-plugin-github": "^4.7.0",
"jest": "^29.5.0",
"nock": "^13.3.0",
"prettier": "^2.8.7"
"prettier": "^2.8.7",
"make-coverage-badge": "^1.2.0"
},
"scripts": {
"all": "npm run format && npm run lint && npm run prepare && npm run test",
"all": "npm run format && npm run lint && npm run prepare && npm run test && npm run coverage-badge",
"coverage-badge": "make-coverage-badge --output-path ./coverage_badge.svg",
"format": "prettier --write .",
"format:check": "prettier --check .",
"lint": "DEBUG=eslint:cli-engine eslint --fix .",
@@ -41,5 +43,22 @@
"bugs": {
"url": "https://github.com/actions/deploy-pages/issues"
},
"homepage": "https://github.com/actions/deploy-pages#readme"
"homepage": "https://github.com/actions/deploy-pages#readme",
"jest": {
"coverageReporters": [
"json-summary",
"text",
"lcov"
],
"collectCoverage": true,
"collectCoverageFrom": [
"./src/**"
],
"coverageThreshold": {
"global": {
"lines": 70,
"statements": 70
}
}
}
}

View File

@@ -0,0 +1,38 @@
const process = require('process')
const cp = require('child_process')
const path = require('path')
describe('with all environment variables set', () => {
beforeEach(() => {
process.env.ACTIONS_RUNTIME_URL = 'http://my-url'
process.env.GITHUB_RUN_ID = '123'
process.env.ACTIONS_RUNTIME_TOKEN = 'a-token'
process.env.GITHUB_REPOSITORY = 'actions/is-awesome'
process.env.GITHUB_TOKEN = 'gha-token'
process.env.GITHUB_SHA = '123abc'
process.env.GITHUB_ACTOR = 'monalisa'
process.env.GITHUB_ACTION = '__monalisa/octocat'
process.env.GITHUB_ACTION_PATH = 'something'
})
it('executes cleanly', done => {
const ip = path.join(__dirname, '../index.js')
cp.exec(`node ${ip}`, { env: process.env }, (err, stdout) => {
expect(stdout).toMatch(/::debug::all variables are set/)
done()
})
})
})
describe('with variables missing', () => {
it('execution fails if there are missing variables', done => {
delete process.env.ACTIONS_RUNTIME_URL
const ip = path.join(__dirname, '../index.js')
cp.exec(`node ${ip}`, { env: process.env }, (err, stdout) => {
expect(stdout).toBe('')
expect(err).toBeTruthy()
expect(err.code).toBe(1)
done()
})
})
})

View File

@@ -1,51 +1,14 @@
const core = require('@actions/core')
const process = require('process')
const cp = require('child_process')
const path = require('path')
const nock = require('nock')
const { Deployment } = require('./deployment')
const { Deployment } = require('../../internal/deployment')
const fakeJwt =
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiJiNjllMWIxOC1jOGFiLTRhZGQtOGYxOC03MzVlMzVjZGJhZjAiLCJzdWIiOiJyZXBvOnBhcGVyLXNwYS9taW55aTplbnZpcm9ubWVudDpQcm9kdWN0aW9uIiwiYXVkIjoiaHR0cHM6Ly9naXRodWIuY29tL3BhcGVyLXNwYSIsInJlZiI6InJlZnMvaGVhZHMvbWFpbiIsInNoYSI6ImEyODU1MWJmODdiZDk3NTFiMzdiMmM0YjM3M2MxZjU3NjFmYWM2MjYiLCJyZXBvc2l0b3J5IjoicGFwZXItc3BhL21pbnlpIiwicmVwb3NpdG9yeV9vd25lciI6InBhcGVyLXNwYSIsInJ1bl9pZCI6IjE1NDY0NTkzNjQiLCJydW5fbnVtYmVyIjoiMzQiLCJydW5fYXR0ZW1wdCI6IjIiLCJhY3RvciI6IllpTXlzdHkiLCJ3b3JrZmxvdyI6IkNJIiwiaGVhZF9yZWYiOiIiLCJiYXNlX3JlZiI6IiIsImV2ZW50X25hbWUiOiJwdXNoIiwicmVmX3R5cGUiOiJicmFuY2giLCJlbnZpcm9ubWVudCI6IlByb2R1Y3Rpb24iLCJqb2Jfd29ya2Zsb3dfcmVmIjoicGFwZXItc3BhL21pbnlpLy5naXRodWIvd29ya2Zsb3dzL2JsYW5rLnltbEByZWZzL2hlYWRzL21haW4iLCJpc3MiOiJodHRwczovL3Rva2VuLmFjdGlvbnMuZ2l0aHVidXNlcmNvbnRlbnQuY29tIiwibmJmIjoxNjM4ODI4MDI4LCJleHAiOjE2Mzg4Mjg5MjgsImlhdCI6MTYzODgyODYyOH0.1wyupfxu1HGoTyIqatYg0hIxy2-0bMO-yVlmLSMuu2w'
describe('with all environment variables set', () => {
beforeEach(() => {
process.env.ACTIONS_RUNTIME_URL = 'http://my-url'
process.env.GITHUB_RUN_ID = '123'
process.env.ACTIONS_RUNTIME_TOKEN = 'a-token'
process.env.GITHUB_REPOSITORY = 'actions/is-awesome'
process.env.GITHUB_TOKEN = 'gha-token'
process.env.GITHUB_SHA = '123abc'
process.env.GITHUB_ACTOR = 'monalisa'
process.env.GITHUB_ACTION = '__monalisa/octocat'
process.env.GITHUB_ACTION_PATH = 'something'
})
it('executes cleanly', done => {
const ip = path.join(__dirname, './index.js')
cp.exec(`node ${ip}`, { env: process.env }, (err, stdout) => {
expect(stdout).toMatch(/::debug::all variables are set/)
done()
})
})
})
describe('with variables missing', () => {
it('execution fails if there are missing variables', done => {
delete process.env.ACTIONS_RUNTIME_URL
const ip = path.join(__dirname, './index.js')
cp.exec(`node ${ip}`, { env: process.env }, (err, stdout) => {
expect(stdout).toBe('')
expect(err).toBeTruthy()
expect(err.code).toBe(1)
done()
})
})
})
describe('Deployment', () => {
beforeEach(() => {
jest.clearAllMocks()
process.env.ACTIONS_RUNTIME_URL = 'http://my-url/'
process.env.GITHUB_RUN_ID = '123'
process.env.ACTIONS_RUNTIME_TOKEN = 'a-token'
@@ -185,6 +148,81 @@ describe('Deployment', () => {
artifactExchangeScope.done()
})
it('reports errors with a failed 500 in a deployment', async () => {
process.env.GITHUB_SHA = 'build-version'
const artifactExchangeScope = nock(`http://my-url`)
.get('/_apis/pipelines/workflows/123/artifacts?api-version=6.0-preview')
.reply(200, { value: [{ url: 'https://invalid-artifact.com', name: 'github-pages' }] })
const createDeploymentScope = nock('https://api.github.com')
.post(`/repos/${process.env.GITHUB_REPOSITORY}/pages/deployments`, {
artifact_url: 'https://invalid-artifact.com&%24expand=SignedContent',
pages_build_version: process.env.GITHUB_SHA
})
.reply(500, { message: 'oh no' })
// Create the deployment
const deployment = new Deployment()
await expect(deployment.create()).rejects.toEqual(
new Error(
`Failed to create deployment (status: 500) with build version ${process.env.GITHUB_SHA}. Server error, is githubstatus.com reporting a Pages outage? Please re-run the deployment at a later time.`
)
)
artifactExchangeScope.done()
createDeploymentScope.done()
})
it('reports errors with an unexpected 403 during deployment', async () => {
process.env.GITHUB_SHA = 'build-version'
const artifactExchangeScope = nock(`http://my-url`)
.get('/_apis/pipelines/workflows/123/artifacts?api-version=6.0-preview')
.reply(200, { value: [{ url: 'https://invalid-artifact.com', name: 'github-pages' }] })
const createDeploymentScope = nock('https://api.github.com')
.post(`/repos/${process.env.GITHUB_REPOSITORY}/pages/deployments`, {
artifact_url: 'https://invalid-artifact.com&%24expand=SignedContent',
pages_build_version: process.env.GITHUB_SHA
})
.reply(403, { message: 'You are forbidden' })
// Create the deployment
const deployment = new Deployment()
await expect(deployment.create()).rejects.toEqual(
new Error(
`Failed to create deployment (status: 403) with build version ${process.env.GITHUB_SHA}. Ensure GITHUB_TOKEN has permission "pages: write".`
)
)
artifactExchangeScope.done()
createDeploymentScope.done()
})
it('reports errors with an unexpected 404 during deployment', async () => {
process.env.GITHUB_SHA = 'build-version'
const artifactExchangeScope = nock(`http://my-url`)
.get('/_apis/pipelines/workflows/123/artifacts?api-version=6.0-preview')
.reply(200, { value: [{ url: 'https://invalid-artifact.com', name: 'github-pages' }] })
const createDeploymentScope = nock('https://api.github.com')
.post(`/repos/${process.env.GITHUB_REPOSITORY}/pages/deployments`, {
artifact_url: 'https://invalid-artifact.com&%24expand=SignedContent',
pages_build_version: process.env.GITHUB_SHA
})
.reply(404, { message: 'Not found' })
// Create the deployment
const deployment = new Deployment()
await expect(deployment.create()).rejects.toEqual(
new Error(
`Failed to create deployment (status: 404) with build version ${process.env.GITHUB_SHA}. Ensure GitHub Pages has been enabled: https://github.com/actions/is-awesome/settings/pages`
)
)
artifactExchangeScope.done()
createDeploymentScope.done()
})
it('reports errors with failed deployments', async () => {
process.env.GITHUB_SHA = 'invalid-build-version'
const artifactExchangeScope = nock(`http://my-url`)
@@ -263,6 +301,56 @@ describe('Deployment', () => {
createDeploymentScope.done()
deploymentStatusScope.done()
})
it('fails check when no deployment is found', async () => {
process.env.GITHUB_SHA = 'valid-build-version'
const deployment = new Deployment()
await deployment.check()
expect(core.setFailed).toBeCalledWith('Deployment not found.')
})
it('exits early when deployment is not in progress', async () => {
process.env.GITHUB_SHA = 'valid-build-version'
const artifactExchangeScope = nock(`http://my-url`)
.get('/_apis/pipelines/workflows/123/artifacts?api-version=6.0-preview')
.reply(200, {
value: [
{ url: 'https://another-artifact.com', name: 'another-artifact' },
{ url: 'https://fake-artifact.com', name: 'github-pages' }
]
})
const createDeploymentScope = nock('https://api.github.com')
.post(`/repos/${process.env.GITHUB_REPOSITORY}/pages/deployments`, {
artifact_url: 'https://fake-artifact.com&%24expand=SignedContent',
pages_build_version: process.env.GITHUB_SHA,
oidc_token: fakeJwt
})
.reply(200, {
status_url: `https://api.github.com/repos/${process.env.GITHUB_REPOSITORY}/pages/deployments/${process.env.GITHUB_SHA}`,
page_url: 'https://actions.github.io/is-awesome'
})
core.getIDToken = jest.fn().mockResolvedValue(fakeJwt)
core.GetInput = jest.fn(input => {
switch (input) {
case 'timeout':
return 10 * 1000
case 'reporting_interval':
return 0
}
})
const deployment = new Deployment()
await deployment.create(fakeJwt)
deployment.deploymentInfo.pending = false
await deployment.check()
expect(core.setFailed).toBeCalledWith('Unable to get deployment status.')
artifactExchangeScope.done()
createDeploymentScope.done()
})
})
describe('#cancel', () => {
@@ -308,5 +396,62 @@ describe('Deployment', () => {
createDeploymentScope.done()
cancelDeploymentScope.done()
})
it('can exit if a pages deployment was not created and none need to be cancelled', async () => {
process.env.GITHUB_SHA = 'valid-build-version'
// Create the deployment
const deployment = new Deployment()
// Cancel it
await deployment.cancel()
expect(core.debug).toHaveBeenCalledWith('all variables are set')
expect(core.debug).toHaveBeenCalledWith(`No deployment to cancel`)
})
it('catches an error when trying to cancel a deployment', async () => {
process.env.GITHUB_SHA = 'valid-build-version'
const artifactExchangeScope = nock(`http://my-url`)
.get('/_apis/pipelines/workflows/123/artifacts?api-version=6.0-preview')
.reply(200, {
value: [
{ url: 'https://another-artifact.com', name: 'another-artifact' },
{ url: 'https://fake-artifact.com', name: 'github-pages' }
]
})
const createDeploymentScope = nock('https://api.github.com')
.post(`/repos/${process.env.GITHUB_REPOSITORY}/pages/deployments`, {
artifact_url: 'https://fake-artifact.com&%24expand=SignedContent',
pages_build_version: process.env.GITHUB_SHA,
oidc_token: fakeJwt
})
.reply(200, {
status_url: `https://api.github.com/repos/${process.env.GITHUB_REPOSITORY}/pages/deployments/${process.env.GITHUB_SHA}`,
page_url: 'https://actions.github.io/is-awesome'
})
// nock will throw an error every time it tries to cancel the deployment
const cancelDeploymentScope = nock('https://api.github.com')
.post(`/repos/${process.env.GITHUB_REPOSITORY}/pages/deployments/${process.env.GITHUB_SHA}/cancel`)
.reply(500, {})
core.getIDToken = jest.fn().mockResolvedValue(fakeJwt)
// Create the deployment
const deployment = new Deployment()
await deployment.create(fakeJwt)
// Cancel it
await deployment.cancel()
expect(core.error).toHaveBeenCalledWith(`Canceling Pages deployment failed`, expect.anything())
artifactExchangeScope.done()
createDeploymentScope.done()
cancelDeploymentScope.done()
})
})
})

View File

@@ -4,8 +4,8 @@
const core = require('@actions/core')
const { Deployment } = require('./deployment')
const getContext = require('./context')
const { Deployment } = require('./internal/deployment')
const getContext = require('./internal/context')
const deployment = new Deployment()

View File

@@ -98,6 +98,7 @@ class Deployment {
}
throw new Error(errorMessage)
} else {
// istanbul ignore next
throw error
}
}
@@ -202,6 +203,7 @@ class Deployment {
async cancel() {
// Don't attempt to cancel if no deployment was created
if (!this.deploymentInfo || this.deploymentInfo.pending !== true) {
core.debug('No deployment to cancel')
return
}