adjust pages hard code endpoint + fix artifact look up

This commit is contained in:
yimysty
2022-08-04 15:43:58 -07:00
parent 38e42fe573
commit 3e976523d7
10 changed files with 61 additions and 29 deletions

View File

@@ -24,6 +24,10 @@ inputs:
description: 'Time in milliseconds between two deployment status report (default: 5 seconds)' description: 'Time in milliseconds between two deployment status report (default: 5 seconds)'
required: false required: false
default: "5000" default: "5000"
artifact_name:
description: 'Name of the artifact to deploy'
required: false
default: "github-pages"
outputs: outputs:
page_url: page_url:
description: 'URL to deployed GitHub Pages' description: 'URL to deployed GitHub Pages'

22
dist/index.js vendored
View File

@@ -7057,7 +7057,9 @@ function getRequiredVars() {
buildVersion: process.env.GITHUB_SHA, buildVersion: process.env.GITHUB_SHA,
buildActor: process.env.GITHUB_ACTOR, buildActor: process.env.GITHUB_ACTOR,
actionsId: process.env.GITHUB_ACTION, actionsId: process.env.GITHUB_ACTION,
githubToken: core.getInput('token') githubApiUrl: process.env.GITHUB_API_URL ?? `https://api.github.com`,
githubToken: core.getInput('token'),
artifactName: core.getInput('artifact_name'),
} }
} }
@@ -7105,6 +7107,8 @@ class Deployment {
this.workflowRun = context.workflowRun this.workflowRun = context.workflowRun
this.requestedDeployment = false this.requestedDeployment = false
this.deploymentInfo = null this.deploymentInfo = null
this.githubApiUrl = context.githubApiUrl
this.artifactName = context.artifactName
} }
// Ask the runtime for the unsigned artifact URL and deploy to GitHub Pages // Ask the runtime for the unsigned artifact URL and deploy to GitHub Pages
@@ -7113,7 +7117,7 @@ class Deployment {
try { try {
core.info(`Actor: ${this.buildActor}`) core.info(`Actor: ${this.buildActor}`)
core.info(`Action ID: ${this.actionsId}`) core.info(`Action ID: ${this.actionsId}`)
const pagesDeployEndpoint = `https://api.github.com/repos/${this.repositoryNwo}/pages/deployment` const pagesDeployEndpoint = `${this.githubApiUrl}/repos/${this.repositoryNwo}/pages/deployment`
const artifactExgUrl = `${this.runTimeUrl}_apis/pipelines/workflows/${this.workflowRun}/artifacts?api-version=6.0-preview` const artifactExgUrl = `${this.runTimeUrl}_apis/pipelines/workflows/${this.workflowRun}/artifacts?api-version=6.0-preview`
core.info(`Artifact URL: ${artifactExgUrl}`) core.info(`Artifact URL: ${artifactExgUrl}`)
const {data} = await axios.get(artifactExgUrl, { const {data} = await axios.get(artifactExgUrl, {
@@ -7123,10 +7127,12 @@ class Deployment {
} }
}) })
core.info(JSON.stringify(data)) core.info(JSON.stringify(data))
if (data.value.length == 0) { const artifactRawUrl = data?.value?.find(artifact => artifact.name === this.artifactName)?.url
throw new Error('No uploaded artifact was found! Please check if there are any errors at build step.') if (!artifactRawUrl) {
throw new Error('No uploaded artifact was found! Please check if there are any errors at build step, or uploaded artifact name is correct.')
} }
const artifactUrl = `${data.value[0].url}&%24expand=SignedContent`
const artifactUrl = `${artifactRawUrl}&%24expand=SignedContent`
const payload = { const payload = {
artifact_url: artifactUrl, artifact_url: artifactUrl,
pages_build_version: this.buildVersion, pages_build_version: this.buildVersion,
@@ -7185,7 +7191,7 @@ class Deployment {
try { try {
const statusUrl = this.deploymentInfo != null ? const statusUrl = this.deploymentInfo != null ?
this.deploymentInfo["status_url"] : this.deploymentInfo["status_url"] :
`https://api.github.com/repos/${this.repositoryNwo}/pages/deployment/status/${process.env['GITHUB_SHA']}` `${this.githubApiUrl}/repos/${this.repositoryNwo}/pages/deployment/status/${process.env['GITHUB_SHA']}`
core.setOutput('page_url', this.deploymentInfo != null ? this.deploymentInfo["page_url"] : "") core.setOutput('page_url', this.deploymentInfo != null ? this.deploymentInfo["page_url"] : "")
const timeout = Number(core.getInput('timeout')) const timeout = Number(core.getInput('timeout'))
const reportingInterval = Number(core.getInput('reporting_interval')) const reportingInterval = Number(core.getInput('reporting_interval'))
@@ -7290,7 +7296,7 @@ const {Deployment} = __nccwpck_require__(2877)
async function emitTelemetry() { async function emitTelemetry() {
// All variables we need from the runtime are set in the Deployment constructor // All variables we need from the runtime are set in the Deployment constructor
const deployment = new Deployment() const deployment = new Deployment()
const telemetryUrl = `https://api.github.com/repos/${deployment.repositoryNwo}/pages/telemetry` const telemetryUrl = `${deployment.githubApiUrl}/repos/${deployment.repositoryNwo}/pages/telemetry`
core.info(`Sending telemetry for run id ${deployment.workflowRun}`) core.info(`Sending telemetry for run id ${deployment.workflowRun}`)
await axios await axios
.post( .post(
@@ -7500,7 +7506,7 @@ const deployment = new Deployment()
async function cancelHandler(evtOrExitCodeOrError) { async function cancelHandler(evtOrExitCodeOrError) {
try { try {
if (deployment.requestedDeployment) { if (deployment.requestedDeployment) {
const pagesCancelDeployEndpoint = `https://api.github.com/repos/${process.env.GITHUB_REPOSITORY}/pages/deployment/cancel/${process.env.GITHUB_SHA}` const pagesCancelDeployEndpoint = `${deployment.githubApiUrl}/repos/${process.env.GITHUB_REPOSITORY}/pages/deployment/cancel/${process.env.GITHUB_SHA}`
await axios.put( await axios.put(
pagesCancelDeployEndpoint, pagesCancelDeployEndpoint,
{}, {},

2
dist/index.js.map vendored

File diff suppressed because one or more lines are too long

View File

@@ -7057,7 +7057,9 @@ function getRequiredVars() {
buildVersion: process.env.GITHUB_SHA, buildVersion: process.env.GITHUB_SHA,
buildActor: process.env.GITHUB_ACTOR, buildActor: process.env.GITHUB_ACTOR,
actionsId: process.env.GITHUB_ACTION, actionsId: process.env.GITHUB_ACTION,
githubToken: core.getInput('token') githubApiUrl: process.env.GITHUB_API_URL ?? `https://api.github.com`,
githubToken: core.getInput('token'),
artifactName: core.getInput('artifact_name'),
} }
} }
@@ -7105,6 +7107,8 @@ class Deployment {
this.workflowRun = context.workflowRun this.workflowRun = context.workflowRun
this.requestedDeployment = false this.requestedDeployment = false
this.deploymentInfo = null this.deploymentInfo = null
this.githubApiUrl = context.githubApiUrl
this.artifactName = context.artifactName
} }
// Ask the runtime for the unsigned artifact URL and deploy to GitHub Pages // Ask the runtime for the unsigned artifact URL and deploy to GitHub Pages
@@ -7113,7 +7117,7 @@ class Deployment {
try { try {
core.info(`Actor: ${this.buildActor}`) core.info(`Actor: ${this.buildActor}`)
core.info(`Action ID: ${this.actionsId}`) core.info(`Action ID: ${this.actionsId}`)
const pagesDeployEndpoint = `https://api.github.com/repos/${this.repositoryNwo}/pages/deployment` const pagesDeployEndpoint = `${this.githubApiUrl}/repos/${this.repositoryNwo}/pages/deployment`
const artifactExgUrl = `${this.runTimeUrl}_apis/pipelines/workflows/${this.workflowRun}/artifacts?api-version=6.0-preview` const artifactExgUrl = `${this.runTimeUrl}_apis/pipelines/workflows/${this.workflowRun}/artifacts?api-version=6.0-preview`
core.info(`Artifact URL: ${artifactExgUrl}`) core.info(`Artifact URL: ${artifactExgUrl}`)
const {data} = await axios.get(artifactExgUrl, { const {data} = await axios.get(artifactExgUrl, {
@@ -7123,10 +7127,12 @@ class Deployment {
} }
}) })
core.info(JSON.stringify(data)) core.info(JSON.stringify(data))
if (data.value.length == 0) { const artifactRawUrl = data?.value?.find(artifact => artifact.name === this.artifactName)?.url
throw new Error('No uploaded artifact was found! Please check if there are any errors at build step.') if (!artifactRawUrl) {
throw new Error('No uploaded artifact was found! Please check if there are any errors at build step, or uploaded artifact name is correct.')
} }
const artifactUrl = `${data.value[0].url}&%24expand=SignedContent`
const artifactUrl = `${artifactRawUrl}&%24expand=SignedContent`
const payload = { const payload = {
artifact_url: artifactUrl, artifact_url: artifactUrl,
pages_build_version: this.buildVersion, pages_build_version: this.buildVersion,
@@ -7185,7 +7191,7 @@ class Deployment {
try { try {
const statusUrl = this.deploymentInfo != null ? const statusUrl = this.deploymentInfo != null ?
this.deploymentInfo["status_url"] : this.deploymentInfo["status_url"] :
`https://api.github.com/repos/${this.repositoryNwo}/pages/deployment/status/${process.env['GITHUB_SHA']}` `${this.githubApiUrl}/repos/${this.repositoryNwo}/pages/deployment/status/${process.env['GITHUB_SHA']}`
core.setOutput('page_url', this.deploymentInfo != null ? this.deploymentInfo["page_url"] : "") core.setOutput('page_url', this.deploymentInfo != null ? this.deploymentInfo["page_url"] : "")
const timeout = Number(core.getInput('timeout')) const timeout = Number(core.getInput('timeout'))
const reportingInterval = Number(core.getInput('reporting_interval')) const reportingInterval = Number(core.getInput('reporting_interval'))
@@ -7290,7 +7296,7 @@ const {Deployment} = __nccwpck_require__(2877)
async function emitTelemetry() { async function emitTelemetry() {
// All variables we need from the runtime are set in the Deployment constructor // All variables we need from the runtime are set in the Deployment constructor
const deployment = new Deployment() const deployment = new Deployment()
const telemetryUrl = `https://api.github.com/repos/${deployment.repositoryNwo}/pages/telemetry` const telemetryUrl = `${deployment.githubApiUrl}/repos/${deployment.repositoryNwo}/pages/telemetry`
core.info(`Sending telemetry for run id ${deployment.workflowRun}`) core.info(`Sending telemetry for run id ${deployment.workflowRun}`)
await axios await axios
.post( .post(

File diff suppressed because one or more lines are too long

View File

@@ -10,7 +10,9 @@ function getRequiredVars() {
buildVersion: process.env.GITHUB_SHA, buildVersion: process.env.GITHUB_SHA,
buildActor: process.env.GITHUB_ACTOR, buildActor: process.env.GITHUB_ACTOR,
actionsId: process.env.GITHUB_ACTION, actionsId: process.env.GITHUB_ACTION,
githubToken: core.getInput('token') githubApiUrl: process.env.GITHUB_API_URL ?? `https://api.github.com`,
githubToken: core.getInput('token'),
artifactName: core.getInput('artifact_name'),
} }
} }

View File

@@ -25,6 +25,8 @@ class Deployment {
this.workflowRun = context.workflowRun this.workflowRun = context.workflowRun
this.requestedDeployment = false this.requestedDeployment = false
this.deploymentInfo = null this.deploymentInfo = null
this.githubApiUrl = context.githubApiUrl
this.artifactName = context.artifactName
} }
// Ask the runtime for the unsigned artifact URL and deploy to GitHub Pages // Ask the runtime for the unsigned artifact URL and deploy to GitHub Pages
@@ -33,7 +35,7 @@ class Deployment {
try { try {
core.info(`Actor: ${this.buildActor}`) core.info(`Actor: ${this.buildActor}`)
core.info(`Action ID: ${this.actionsId}`) core.info(`Action ID: ${this.actionsId}`)
const pagesDeployEndpoint = `https://api.github.com/repos/${this.repositoryNwo}/pages/deployment` const pagesDeployEndpoint = `${this.githubApiUrl}/repos/${this.repositoryNwo}/pages/deployment`
const artifactExgUrl = `${this.runTimeUrl}_apis/pipelines/workflows/${this.workflowRun}/artifacts?api-version=6.0-preview` const artifactExgUrl = `${this.runTimeUrl}_apis/pipelines/workflows/${this.workflowRun}/artifacts?api-version=6.0-preview`
core.info(`Artifact URL: ${artifactExgUrl}`) core.info(`Artifact URL: ${artifactExgUrl}`)
const {data} = await axios.get(artifactExgUrl, { const {data} = await axios.get(artifactExgUrl, {
@@ -43,10 +45,12 @@ class Deployment {
} }
}) })
core.info(JSON.stringify(data)) core.info(JSON.stringify(data))
if (data.value.length == 0) { const artifactRawUrl = data?.value?.find(artifact => artifact.name === this.artifactName)?.url
throw new Error('No uploaded artifact was found! Please check if there are any errors at build step.') if (!artifactRawUrl) {
throw new Error('No uploaded artifact was found! Please check if there are any errors at build step, or uploaded artifact name is correct.')
} }
const artifactUrl = `${data.value[0].url}&%24expand=SignedContent`
const artifactUrl = `${artifactRawUrl}&%24expand=SignedContent`
const payload = { const payload = {
artifact_url: artifactUrl, artifact_url: artifactUrl,
pages_build_version: this.buildVersion, pages_build_version: this.buildVersion,
@@ -105,7 +109,7 @@ class Deployment {
try { try {
const statusUrl = this.deploymentInfo != null ? const statusUrl = this.deploymentInfo != null ?
this.deploymentInfo["status_url"] : this.deploymentInfo["status_url"] :
`https://api.github.com/repos/${this.repositoryNwo}/pages/deployment/status/${process.env['GITHUB_SHA']}` `${this.githubApiUrl}/repos/${this.repositoryNwo}/pages/deployment/status/${process.env['GITHUB_SHA']}`
core.setOutput('page_url', this.deploymentInfo != null ? this.deploymentInfo["page_url"] : "") core.setOutput('page_url', this.deploymentInfo != null ? this.deploymentInfo["page_url"] : "")
const timeout = Number(core.getInput('timeout')) const timeout = Number(core.getInput('timeout'))
const reportingInterval = Number(core.getInput('reporting_interval')) const reportingInterval = Number(core.getInput('reporting_interval'))

View File

@@ -17,7 +17,7 @@ const deployment = new Deployment()
async function cancelHandler(evtOrExitCodeOrError) { async function cancelHandler(evtOrExitCodeOrError) {
try { try {
if (deployment.requestedDeployment) { if (deployment.requestedDeployment) {
const pagesCancelDeployEndpoint = `https://api.github.com/repos/${process.env.GITHUB_REPOSITORY}/pages/deployment/cancel/${process.env.GITHUB_SHA}` const pagesCancelDeployEndpoint = `${deployment.githubApiUrl}/repos/${process.env.GITHUB_REPOSITORY}/pages/deployment/cancel/${process.env.GITHUB_SHA}`
await axios.put( await axios.put(
pagesCancelDeployEndpoint, pagesCancelDeployEndpoint,
{}, {},

View File

@@ -20,6 +20,7 @@ describe('with all environment variables set', () => {
process.env.GITHUB_ACTOR = 'monalisa' process.env.GITHUB_ACTOR = 'monalisa'
process.env.GITHUB_ACTION = '__monalisa/octocat' process.env.GITHUB_ACTION = '__monalisa/octocat'
process.env.GITHUB_ACTION_PATH = 'something' process.env.GITHUB_ACTION_PATH = 'something'
process.env.ARTIFACT_NAME = 'github-pages'
}) })
it('Executes cleanly', done => { it('Executes cleanly', done => {
@@ -55,6 +56,14 @@ describe('create', () => {
process.env.GITHUB_ACTOR = 'monalisa' process.env.GITHUB_ACTOR = 'monalisa'
process.env.GITHUB_ACTION = '__monalisa/octocat' process.env.GITHUB_ACTION = '__monalisa/octocat'
process.env.GITHUB_ACTION_PATH = 'something' process.env.GITHUB_ACTION_PATH = 'something'
jest.spyOn(core, 'getInput').mockImplementation(param => {
switch (param) {
case 'artifact_name':
return 'github-pages'
case 'token':
return process.env.GITHUB_TOKEN
}
})
jest.spyOn(core, 'setOutput').mockImplementation(param => { jest.spyOn(core, 'setOutput').mockImplementation(param => {
return param return param
@@ -75,7 +84,7 @@ describe('create', () => {
const fakeJwt = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiJiNjllMWIxOC1jOGFiLTRhZGQtOGYxOC03MzVlMzVjZGJhZjAiLCJzdWIiOiJyZXBvOnBhcGVyLXNwYS9taW55aTplbnZpcm9ubWVudDpQcm9kdWN0aW9uIiwiYXVkIjoiaHR0cHM6Ly9naXRodWIuY29tL3BhcGVyLXNwYSIsInJlZiI6InJlZnMvaGVh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9yeV9vd25lciI6InBhcGVyLXNwYSIsInJ1bl9pZCI6IjE1NDY0NTkzNjQiLCJydW5fbnVtYmVyIjoiMzQiLCJydW5fYXR0ZW1wdCI6IjIiLCJhY3RvciI6IllpTXlzdHkiLCJ3b3JrZmxvdyI6IkNJIiwiaGVhZF9yZWYiOiIiLCJiYXNlX3JlZiI6IiIsImV2ZW50X25hbWUiOiJwdXNoIiwicmVmX3R5cGUiOiJicmFuY2giLCJlbnZpcm9ubWVudCI6IlByb2R1Y3Rpb24iLCJqb2Jfd29ya2Zsb3dfcmVmIjoicGFwZXItc3BhL21pbnlpLy5naXRodWIvd29ya2Zsb3dzL2JsYW5rLnltbEByZWZzL2hlYWRzL21haW4iLCJpc3MiOiJodHRwczovL3Rva2VuLmFjdGlvbnMuZ2l0aHVidXNlcmNvbnRlbnQuY29tIiwibmJmIjoxNjM4ODI4MDI4LCJleHAiOjE2Mzg4Mjg5MjgsImlhdCI6MTYzODgyODYyOH0.1wyupfxu1HGoTyIqatYg0hIxy2-0bMO-yVlmLSMuu2w'
const scope = nock(`http://my-url`) const scope = nock(`http://my-url`)
.get('/_apis/pipelines/workflows/123/artifacts?api-version=6.0-preview') .get('/_apis/pipelines/workflows/123/artifacts?api-version=6.0-preview')
.reply(200, { value: [{ url: 'https://fake-artifact.com' }] }) .reply(200, { value: [ {url: 'https://another-artifact.com', name: 'another-artifact'}, { url: 'https://fake-artifact.com', name: 'github-pages' }] })
core.getIDToken = jest.fn().mockResolvedValue(fakeJwt) core.getIDToken = jest.fn().mockResolvedValue(fakeJwt)
axios.post = jest.fn().mockResolvedValue('test') axios.post = jest.fn().mockResolvedValue('test')
@@ -94,7 +103,7 @@ describe('create', () => {
{ {
headers: { headers: {
Accept: 'application/vnd.github.v3+json', Accept: 'application/vnd.github.v3+json',
Authorization: 'Bearer ', Authorization: `Bearer gha-token`,
'Content-type': 'application/json' 'Content-type': 'application/json'
} }
} }
@@ -112,7 +121,7 @@ describe('create', () => {
process.env.GITHUB_SHA = 'invalid-build-version' process.env.GITHUB_SHA = 'invalid-build-version'
const scope = nock(`http://my-url`) const scope = nock(`http://my-url`)
.get('/_apis/pipelines/workflows/123/artifacts?api-version=6.0-preview') .get('/_apis/pipelines/workflows/123/artifacts?api-version=6.0-preview')
.reply(200, { value: [{ url: 'https://invalid-artifact.com' }] }) .reply(200, { value: [{ url: 'https://invalid-artifact.com', name: 'github-pages' }] })
axios.post = jest.fn().mockRejectedValue({ axios.post = jest.fn().mockRejectedValue({
status: 400 status: 400
@@ -161,6 +170,7 @@ describe('check', () => {
process.env.GITHUB_ACTOR = 'monalisa' process.env.GITHUB_ACTOR = 'monalisa'
process.env.GITHUB_ACTION = '__monalisa/octocat' process.env.GITHUB_ACTION = '__monalisa/octocat'
process.env.GITHUB_ACTION_PATH = 'something' process.env.GITHUB_ACTION_PATH = 'something'
process.env.ARTIFACT_NAME = 'github-pages'
jest.spyOn(core, 'setOutput').mockImplementation(param => { jest.spyOn(core, 'setOutput').mockImplementation(param => {
return param return param
@@ -209,7 +219,7 @@ describe('check', () => {
`https://api.github.com/repos/${repositoryNwo}/pages/deployment/status/${buildVersion}`, `https://api.github.com/repos/${repositoryNwo}/pages/deployment/status/${buildVersion}`,
{ {
headers: { headers: {
Authorization: 'token ' Authorization: 'token gha-token'
} }
} }
) )

View File

@@ -21,7 +21,7 @@ const {Deployment} = require('./deployment')
async function emitTelemetry() { async function emitTelemetry() {
// All variables we need from the runtime are set in the Deployment constructor // All variables we need from the runtime are set in the Deployment constructor
const deployment = new Deployment() const deployment = new Deployment()
const telemetryUrl = `https://api.github.com/repos/${deployment.repositoryNwo}/pages/telemetry` const telemetryUrl = `${deployment.githubApiUrl}/repos/${deployment.repositoryNwo}/pages/telemetry`
core.info(`Sending telemetry for run id ${deployment.workflowRun}`) core.info(`Sending telemetry for run id ${deployment.workflowRun}`)
await axios await axios
.post( .post(