Skip to content

Conversation

int128
Copy link
Member

@int128 int128 commented Feb 23, 2024

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

--- a/environment-matrix/src/deployment.ts
+++ b/environment-outputs/src/deployment.ts
@@ -1,39 +1,12 @@
 import * as core from '@actions/core'
 import * as github from '@actions/github'
-import { Environment } from './rule'
 import { RequestError } from '@octokit/request-error'
 import { Octokit, assertPullRequestPayload } from './github'
 import assert from 'assert'

 type Context = Pick<typeof github.context, 'eventName' | 'repo' | 'ref' | 'payload'>

-export type EnvironmentWithDeployment = Environment & {
-  // URL of the GitHub Deployment
-  // e.g. https://api.github.com/repos/octocat/example/deployments/1
-  'github-deployment-url': string
-}
-
-export const createGitHubDeploymentForEnvironments = async (
-  octokit: Octokit,
-  context: Context,
-  environments: Environment[],
-  service: string,
-): Promise<EnvironmentWithDeployment[]> => {
-  const environmentsWithDeployments = []
-  for (const environment of environments) {
-    const { overlay, namespace } = environment
-    if (overlay && namespace && service) {
-      const deployment = await createDeployment(octokit, context, overlay, namespace, service)
-      environmentsWithDeployments.push({
-        ...environment,
-        'github-deployment-url': deployment.url,
-      })
-    }
-  }
-  return environmentsWithDeployments
-}
-
-const createDeployment = async (
+export const createDeployment = async (
   octokit: Octokit,
   context: Context,
   overlay: string,
@@ -96,7 +69,7 @@ const createDeployment = async (
     state: 'inactive',
   })
   core.info(`Set the deployment status to inactive`)
-  return created.data
+  return created.data.url
 }

 const getDeploymentRef = (context: Context): string => {

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

--- a/environment-matrix/src/main.ts
+++ b/environment-outputs/src/main.ts
@@ -7,7 +7,15 @@ const main = async (): Promise<void> => {
     service: core.getInput('service'),
     token: core.getInput('token'),
   })
-  core.setOutput('json', outputs.environments)
+  core.info('Setting outputs:')
+  for (const [k, v] of Object.entries(outputs.outputs)) {
+    core.info(`${k}=${v}`)
+    core.setOutput(k, v)
+  }
+  if (outputs.githubDeploymentURL) {
+    core.info(`github-deployment-url=${outputs.githubDeploymentURL}`)
+    core.setOutput('github-deployment-url', outputs.githubDeploymentURL)
+  }
 }

 main().catch((e: Error) => {

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

--- a/environment-matrix/src/matcher.ts
+++ b/environment-outputs/src/matcher.ts
@@ -1,14 +1,14 @@
 import * as github from '@actions/github'
 import { minimatch } from 'minimatch'
-import { Environment, Rule, Rules } from './rule'
+import { Rule, Rules } from './rule'
 import { assertPullRequestPayload } from './github'

 type Context = Pick<typeof github.context, 'eventName' | 'ref' | 'payload'>

-export const find = (context: Context, rules: Rules): Environment[] | undefined => {
+export const find = (context: Context, rules: Rules): Record<string, string> | undefined => {
   for (const rule of rules) {
     if (match(context, rule)) {
-      return rule.environments
+      return rule.outputs
     }
   }
 }

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

--- a/environment-matrix/src/rule.ts
+++ b/environment-outputs/src/rule.ts
@@ -1,14 +1,6 @@
 import * as yaml from 'js-yaml'
 import Ajv, { JTDSchemaType } from 'ajv/dist/jtd'

-export type Environment = Record<string, string>
-
-const EnvironmentSchema: JTDSchemaType<Environment> = {
-  values: {
-    type: 'string',
-  },
-}
-
 export type Rule = {
   pull_request?: {
     base: string
@@ -17,13 +9,15 @@ export type Rule = {
   push?: {
     ref: string
   }
-  environments: Environment[]
+  outputs: Record<string, string>
 }

 const RuleSchema: JTDSchemaType<Rule> = {
   properties: {
-    environments: {
-      elements: EnvironmentSchema,
+    outputs: {
+      values: {
+        type: 'string',
+      },
     },
   },
   optionalProperties: {

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

--- a/environment-matrix/src/run.ts
+++ b/environment-outputs/src/run.ts
@@ -1,9 +1,9 @@
 import assert from 'assert'
 import * as core from '@actions/core'
 import * as github from '@actions/github'
-import { Environment, parseRulesYAML } from './rule'
+import { parseRulesYAML } from './rule'
 import { find } from './matcher'
-import { EnvironmentWithDeployment, createGitHubDeploymentForEnvironments } from './deployment'
+import { createDeployment } from './deployment'
 import { getOctokit } from './github'

 type Inputs = {
@@ -13,31 +13,28 @@ type Inputs = {
 }

 type Outputs = {
-  environments: Environment[] | EnvironmentWithDeployment[]
+  outputs: Record<string, string>
+  githubDeploymentURL?: string
 }

 export const run = async (inputs: Inputs): Promise<Outputs> => {
   const rules = parseRulesYAML(inputs.rules)
   core.info(`rules: ${JSON.stringify(rules, undefined, 2)}`)
-  const environments = find(github.context, rules)
-  if (environments === undefined) {
+  const outputs = find(github.context, rules)
+  if (outputs === undefined) {
     throw new Error(`no environment to deploy`)
   }

-  core.info(`environments = ${JSON.stringify(environments, undefined, 2)}`)
   if (!inputs.service) {
-    return { environments }
+    return { outputs }
   }

-  core.info(`Creating GitHub Deployments for environments`)
+  core.info(`Creating a GitHub Deployment for the environment`)
+  const { overlay, namespace } = outputs
+  assert(overlay, `overlay is required in the rule outputs`)
+  assert(namespace, `namespace is required in the rule outputs`)
   assert(inputs.token, `inputs.token is required`)
   const octokit = getOctokit(inputs.token)
-  const environmentsWithDeployments = await createGitHubDeploymentForEnvironments(
-    octokit,
-    github.context,
-    environments,
-    inputs.service,
-  )
-  core.info(`environmentsWithDeployments = ${JSON.stringify(environmentsWithDeployments, undefined, 2)}`)
-  return { environments: environmentsWithDeployments }
+  const githubDeploymentURL = await createDeployment(octokit, github.context, overlay, namespace, inputs.service)
+  return { outputs, githubDeploymentURL }
 }

@github-actions github-actions bot temporarily deployed to pr/pr-1336/example March 16, 2024 09:12 Destroyed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant