Skip to content

Commit

Permalink
Use service input
Browse files Browse the repository at this point in the history
  • Loading branch information
int128 committed Nov 12, 2023
1 parent 6c5a4af commit 471fa22
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 90 deletions.
5 changes: 1 addition & 4 deletions .github/workflows/environment-matrix.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,19 @@ jobs:
- uses: ./environment-matrix
id: environment-matrix
with:
service: example
rules: |
- pull_request:
base: '**'
head: '**'
environments:
- overlay: pr
namespace: pr-${{ github.event.pull_request.number }}
github-deployment: 'true'
github-deployment-environment: pr/pr-${{ github.event.pull_request.number }}/example
- push:
ref: refs/heads/main
environments:
- overlay: development
namespace: development
github-deployment: 'true'
github-deployment-environment: development/development/example
e2e-test-matrix:
needs: e2e-test
Expand Down
32 changes: 12 additions & 20 deletions environment-matrix/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,8 @@ If no rule is matched, this action fails.

This action supports [GitHub Deployment](https://docs.github.com/en/rest/deployments/deployments) to receive the deployment status against the environment.

To create a GitHub Deployment for each environment,

- Set `github-deployment` field to `true`
- Set `github-deployment-environment` field to the environment name
If `overlay`, `namespace` and `service` are given,
this action creates a GitHub Deployment for each environment in the form of `{overlay}/{namespace}/{service}`.

If the old deployment exists, this action deletes it and recreates new one.

Expand All @@ -64,22 +62,19 @@ jobs:
- uses: quipper/monorepo-deploy-actions/environment-matrix@v1
id: environment-matrix
with:
service: example
rules: |
- pull_request:
base: '**'
head: '**'
environments:
- overlay: pr
namespace: pr-${{ github.event.pull_request.number }}
github-deployment: 'true'
github-deployment-environment: pr/pr-${{ github.event.pull_request.number }}/example
- push:
ref: refs/heads/main
environments:
- overlay: development
namespace: development
github-deployment: 'true'
github-deployment-environment: development/development/example
deploy:
needs:
Expand All @@ -96,19 +91,20 @@ jobs:
manifests: # (omit in this example)
overlay: ${{ matrix.environment.overlay }}
namespace: ${{ matrix.environment.namespace }}
service: # (omit in this example)
service: example
application-annotations: |
argocd-commenter.int128.github.io/deployment-url=${{ matrix.github-deployment-url }}
argocd-commenter.int128.github.io/deployment-url=${{ matrix.environment.github-deployment-url }}
```

## Spec

### Inputs

| Name | Default | Description |
| ------- | -------------- | -------------------- |
| `rules` | (required) | YAML string of rules |
| `token` | `github.token` | GitHub token |
| Name | Default | Description |
| --------- | -------------- | ------------------------- |
| `rules` | (required) | YAML string of rules |
| `service` | (optional) | Name of service to deploy |
| `token` | `github.token` | GitHub token |

The following fields are available in the rules YAML.

Expand All @@ -117,15 +113,11 @@ The following fields are available in the rules YAML.
base: # base branch name (wildcard available)
head: # head branch name (wildcard available)
environments:
- # array of map<string, string>
github-deployment: 'true' # set true to create a GitHub Deployment (optional)
github-deployment-environment: # environment name of GitHub Deployment (optional)
- # array of map<string, string>
- push: # on push event
ref: refs/heads/main # ref name (wildcard available)
environments:
- # array of map<string, string>
github-deployment: 'true' # set true to create a GitHub Deployment (optional)
github-deployment-environment: # environment name of GitHub Deployment (optional)
- # array of map<string, string>
```

It supports the wildcard pattern.
Expand Down
3 changes: 3 additions & 0 deletions environment-matrix/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ inputs:
rules:
description: YAML string of rules
required: true
service:
description: Name of service
required: false
token:
description: GitHub token
required: true
Expand Down
25 changes: 17 additions & 8 deletions environment-matrix/src/deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,31 @@ export const createGitHubDeploymentForEnvironments = async (
octokit: Octokit,
context: Context,
environments: Environment[],
service: string,
) => {
for (const environment of environments) {
if (environment['github-deployment'] === 'true' && environment['github-deployment-environment']) {
const deploymentEnvironment = environment['github-deployment-environment']
const deployment = await createDeployment(octokit, context, deploymentEnvironment)
const { overlay, namespace } = environment
if (overlay && namespace) {
const deployment = await createDeployment(octokit, context, overlay, namespace, service)
environment['github-deployment-url'] = deployment.url
}
}
}

const createDeployment = async (octokit: Octokit, context: Context, deploymentEnvironment: string) => {
core.info(`Finding the old deployments for environment ${deploymentEnvironment}`)
const createDeployment = async (
octokit: Octokit,
context: Context,
overlay: string,
namespace: string,
service: string,
) => {
const environment = `${overlay}/${namespace}/${service}`

core.info(`Finding the old deployments for environment ${environment}`)
const oldDeployments = await octokit.rest.repos.listDeployments({
owner: context.repo.owner,
repo: context.repo.repo,
environment: deploymentEnvironment,
environment: environment,
})

core.info(`Deleting ${oldDeployments.data.length} deployment(s)`)
Expand All @@ -48,12 +57,12 @@ const createDeployment = async (octokit: Octokit, context: Context, deploymentEn
}

const deploymentRef = getDeploymentRef(context)
core.info(`Creating a deployment for environment=${deploymentEnvironment}, ref=${deploymentRef}`)
core.info(`Creating a deployment for environment=${environment}, ref=${deploymentRef}`)
const created = await octokit.rest.repos.createDeployment({
owner: context.repo.owner,
repo: context.repo.repo,
ref: deploymentRef,
environment: deploymentEnvironment,
environment: environment,
auto_merge: false,
required_contexts: [],
transient_environment: context.eventName === 'pull_request',
Expand Down
1 change: 1 addition & 0 deletions environment-matrix/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { run } from './run'
const main = async (): Promise<void> => {
const outputs = await run({
rules: core.getInput('rules', { required: true }),
service: core.getInput('service', { required: true }),
token: core.getInput('token', { required: true }),
})
core.setOutput('json', outputs.environments)
Expand Down
8 changes: 3 additions & 5 deletions environment-matrix/src/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ import * as yaml from 'js-yaml'
import Ajv, { JTDSchemaType } from 'ajv/dist/jtd'

export type Environment = Record<string, string> & {
// (input) If set to true, create a GitHub Deployment
'github-deployment'?: string
// (input) The environment name of GitHub Deployment
'github-deployment-environment'?: string
// (output) The URL of GitHub Deployment, set by this action
overlay?: string
namespace?: string
// The URL of GitHub Deployment, set by this action
'github-deployment-url'?: string
}

Expand Down
12 changes: 8 additions & 4 deletions environment-matrix/src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { getOctokit } from './github'

type Inputs = {
rules: string
service: string
token: string
}

Expand All @@ -23,10 +24,13 @@ export const run = async (inputs: Inputs): Promise<Outputs> => {
}
core.info(`environments = ${JSON.stringify(environments, undefined, 2)}`)

core.info(`Creating GitHub Deployments if needed`)
const octokit = getOctokit(inputs.token)
await createGitHubDeploymentForEnvironments(octokit, github.context, environments)
core.info(`environments = ${JSON.stringify(environments, undefined, 2)}`)
if (inputs.service) {
core.info(`Creating GitHub Deployments`)
const octokit = getOctokit(inputs.token)
await createGitHubDeploymentForEnvironments(octokit, github.context, environments, inputs.service)
core.info(`environments = ${JSON.stringify(environments, undefined, 2)}`)
}

return {
environments,
}
Expand Down
49 changes: 0 additions & 49 deletions environment-matrix/tests/rule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,55 +41,6 @@ test('parse a valid YAML', () => {
])
})

test('parse rules with GitHub Deployment', () => {
const yaml = `
- pull_request:
base: '**'
head: '**'
environments:
- overlay: pr
namespace: pr-1
github-deployment: 'true'
github-deployment-environment: pr/pr-1
- push:
ref: refs/heads/main
environments:
- overlay: development
namespace: development
github-deployment: 'true'
github-deployment-environment: development/development
`
expect(parseRulesYAML(yaml)).toStrictEqual<Rules>([
{
pull_request: {
base: '**',
head: '**',
},
environments: [
{
overlay: 'pr',
namespace: 'pr-1',
'github-deployment': 'true',
'github-deployment-environment': 'pr/pr-1',
},
],
},
{
push: {
ref: 'refs/heads/main',
},
environments: [
{
overlay: 'development',
namespace: 'development',
'github-deployment': 'true',
'github-deployment-environment': 'development/development',
},
],
},
])
})

test('parse an empty string', () => {
expect(() => parseRulesYAML('')).toThrow(`invalid rules YAML: must be array`)
})
Expand Down

0 comments on commit 471fa22

Please sign in to comment.