Skip to content

Commit

Permalink
feat: add configurable repo and revision config (#60)
Browse files Browse the repository at this point in the history
* Move repo and revision config to action defaults instead of being determined in the action. These still default to the same values as before but can be overridden if needed. Also update pull requests to specify targetrevision of pull/<number>/head instead of provided sha which causes issues in argocd. This may need to be rethought in the future for non github repos
* chore(lint): Fix code style issues with Prettier

---------

Co-authored-by: Lint Action <github-action@users.noreply.github.com>
  • Loading branch information
ejhayes and Lint Action committed Mar 1, 2023
1 parent e961c4e commit 3e4a922
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 35 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ jobs:
INPUT_PROJECT: default
INPUT_TOKENS: '{"TAG": "latest"}'
INPUT_VALUESFILE: fixtures/values.yaml
INPUT_REPOURL: ${{ github.server_url }}/${{ github.repository }}
INPUT_REPOWEBURL: ${{ github.server_url }}/${{ github.repository }}
INPUT_REPOREVISION: ${{ github.sha }}
INPUT_PRNUMBER: ${{ github.event.number }}
run: node dist/index.js
- name: Version Dry Run
env:
Expand Down
16 changes: 16 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,29 @@ inputs:
project:
description: 'Argo project to create application in'
required: true
prNumber:
description: 'PR number'
default: ${{ github.event.number }}
repoRevision:
description: Repo revision to deploy
required: false
default: ${{ github.sha }}
repoUrl:
description: Repo URL to deploy
required: false
default: ${{ github.server_url }}/${{ github.repository }}
repoWebUrl:
description: Repo web url
required: false
default: ${{ github.server_url }}/${{ github.repository }}
tokens:
description: 'Key/Value list of tokens to replace in helm chart'
required: false
default: '{}'
valuesFile:
description: 'Values file to pass to ArgoCD'
required: true

runs:
using: 'node16'
main: 'dist/index.js'
67 changes: 32 additions & 35 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ enum INPUTS {
NAMESPACE = 'namespace',
PATH = 'path',
PROJECT = 'project',
PR_NUMBER = 'prNumber',
REPO_REVISION = 'repoRevision',
REPO_URL = 'repoUrl',
REPO_WEBURL = 'repoWebUrl',
TOKENS = 'tokens',
VALUES_FILE = 'valuesFile',
}
Expand Down Expand Up @@ -60,22 +64,6 @@ interface ActionArgs {
* Dry run mode
*/
dryRun: boolean;
/**
* Git revision to deploy
*/
gitRef: string;
/**
* Git repo url to deploy (e.g. git://...)
*/
gitRepoUrl: string;
/**
* Git SHA of the deployment
*/
gitSha: string;
/**
* GitHub web url
*/
gitWebUrl: string;
/**
* Info tags to add to argo application
*/
Expand All @@ -96,10 +84,26 @@ interface ActionArgs {
* Path to helm chart in repo
*/
path: string;
/**
* Pull Request Number
*/
prNumber?: number;
/**
* Argo project to deploy to
*/
project: string;
/**
* Repo revision to deploy
*/
repoRevision: string;
/**
* Repo url to deploy
*/
repoUrl: string;
/**
* Repo web url
*/
repoWebUrl: string;
/**
* Tokens to replace in values file
*/
Expand Down Expand Up @@ -130,8 +134,8 @@ async function upsertApplication(args: ActionArgs) {

const app = await client.createApplication(args.name, {
clusterName: args.clusterName,
gitRef: args.gitSha,
gitRepo: args.gitRepoUrl,
gitRef: args.prNumber ? `pull/${args.prNumber}/head` : args.repoRevision,
gitRepo: args.repoUrl,
helmValues: await promisify(readFile)(args.valuesFile),
namespace: args.namespace,
project: args.project,
Expand All @@ -144,7 +148,7 @@ async function upsertApplication(args: ActionArgs) {
tokens: args.tokens,
});

const webUiUrl = `${args.gitWebUrl}/blob/${args.gitSha}`;
const webUiUrl = `${args.repoWebUrl}/blob/${args.repoRevision}`;

try {
await app.toManifest();
Expand Down Expand Up @@ -217,6 +221,11 @@ function getClient(args: ActionArgs) {
}

function getInputs(): ActionArgs {
const prNumber = core.getInput(INPUTS.PR_NUMBER, {
required: false,
trimWhitespace: true,
});

const inputs = {
accessToken: core.getInput(INPUTS.ACCESS_TOKEN, { required: false }),
action: core.getInput(INPUTS.ACTION, { required: true }) as any,
Expand All @@ -228,25 +237,13 @@ function getInputs(): ActionArgs {
clientSecret: core.getInput(INPUTS.CLIENT_SECRET, { required: false }),
clusterName: core.getInput(INPUTS.CLUSTER_NAME, { required: true }),
dryRun: core.getBooleanInput(INPUTS.DRY_RUN, { required: false }),
/**
* Get the ref as follows:
* 1) User specified
* 2) If pull request, return the head_ref
* 3) Extract from GITHUB_REF
*/
gitRef:
process.env.GITHUB_HEAD_REF ||
process.env.GITHUB_REF.replace(/refs\/[^\/]+\/([^\/]+).*/, '$1'),
gitSha: process.env.GITHUB_SHA,
gitWebUrl: `${process.env.GITHUB_SERVER_URL}/${process.env.GITHUB_REPOSITORY}`,
/**
* TODO: this can be either https or git depending on argocd setup. rather than
* making an assumption here this should be cleaned up to be configurable
*/
gitRepoUrl: `${process.env.GITHUB_SERVER_URL}/${process.env.GITHUB_REPOSITORY}`,
name: core.getInput(INPUTS.NAME, { required: true }),
namespace: core.getInput(INPUTS.NAMESPACE, { required: true }),
project: core.getInput(INPUTS.PROJECT, { required: true }),
prNumber: prNumber === '' ? null : Number(prNumber),
repoRevision: core.getInput(INPUTS.REPO_REVISION, { required: true }),
repoUrl: core.getInput(INPUTS.REPO_URL, { required: true }),
repoWebUrl: core.getInput(INPUTS.REPO_WEBURL, { required: true }),
valuesFile: core.getInput(INPUTS.VALUES_FILE, { required: true }),
info: (load(core.getInput(INPUTS.INFO, { required: false })) ||
{}) as KeyVal,
Expand Down

0 comments on commit 3e4a922

Please sign in to comment.