Draft
Conversation
int128
commented
Feb 23, 2024
Member
Author
There was a problem hiding this comment.
--- 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 => {
Member
Author
There was a problem hiding this comment.
--- 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) => {
Member
Author
There was a problem hiding this comment.
--- 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
}
}
}
Member
Author
There was a problem hiding this comment.
--- 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: {
Member
Author
There was a problem hiding this comment.
--- 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 }
}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
New feature
This adds an action of single environment version of https://github.com/quipper/monorepo-deploy-actions/tree/main/environment-matrix.
See the doc for details.
https://github.com/quipper/monorepo-deploy-actions/tree/int128/environment-outputs/environment-outputs