Skip to content

Commit

Permalink
feat: create github deployment
Browse files Browse the repository at this point in the history
Signed-off-by: Minsu Lee <amond@amond.net>
  • Loading branch information
amondnet committed Oct 13, 2020
1 parent d4e0a9f commit d826954
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/example-static.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ jobs:
alias-domains: |
staging.static.vercel-action.amond.dev
pr-{{PR_NUMBER}}.static.vercel-action.amond.dev
github-deployment: true
# github-deployment-environment: staging
- name: production or not
id: prod_or_not
run: |
Expand Down
5 changes: 4 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ inputs:
required: false
default: 'false'
description: 'if you want to create github deployment, set true, default: false'
github-deployment-environment:
required: false
description: 'The environment parameter allows deployments to be issued to different runtime environments. Teams often have multiple environments for verifying their applications, such as production, staging, and qa. This parameter makes it easier to track which environments have requested deployments. The default environment is production.'
default: 'production'
working-directory:
required: false
description: the working directory
Expand Down Expand Up @@ -55,7 +59,6 @@ inputs:
required: false
description: 'You can assign a domain to this deployment. Please note that this domain must have been configured in the project. You can use pull request number via `{{PR_NUMBER}}` and branch via `{{BRANCH}}`'
default: ''

outputs:
preview-url:
description: 'deployment preview URL'
Expand Down
46 changes: 43 additions & 3 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1149,6 +1149,9 @@ const aliasDomains = core
return url;
});

const githubDeployment = core.getInput('github-deployment') === 'true';
const githubDeploymentEnv = core.getInput('github-deployment-environment');

let octokit;
if (githubToken) {
octokit = new github.GitHub(githubToken);
Expand Down Expand Up @@ -1399,17 +1402,50 @@ async function aliasDomainsToDeployment(deploymentUrl) {
await Promise.all(promises);
}

function getEffectiveRef() {
let { ref } = context;
if (github.context.eventName === 'pull_request') {
const pullRequestPayload = github.context.payload;
ref = pullRequestPayload.pull_request.head.ref;
}
return ref;
}

async function createGithubDeployment() {
if (githubDeployment && octokit) {
core.debug(`Create a github deployment.`);
const deployment = await octokit.repos.createDeployment({
...context.repo,
ref: getEffectiveRef(),
environment: githubDeploymentEnv,
});
return deployment.id;
}
}

async function createGithubDeploymentStatus(deploymentId, state) {
if (githubDeployment && deploymentId && state && octokit) {
await octokit.repos.createDeploymentStatus({
...context.repo,
deployment_id: deploymentId,
state,
});
}
}

async function run() {
core.debug(`action : ${context.action}`);
core.debug(`ref : ${context.ref}`);
core.debug(`eventName : ${context.eventName}`);
core.debug(`actor : ${context.actor}`);
core.debug(`sha : ${context.sha}`);
core.debug(`workflow : ${context.workflow}`);
let { ref } = context;
let ref = getEffectiveRef();
let { sha } = context;
await setEnv();

const githubDeploymentId = await createGithubDeployment();

let commit = execSync('git log -1 --pretty=format:%B')
.toString()
.trim();
Expand All @@ -1434,9 +1470,13 @@ async function run() {
core.debug(`The head commit is: ${commit}`);
}
}

if (githubDeploymentId) {
await createGithubDeploymentStatus(githubDeploymentId, 'in_progress');
}
const deploymentUrl = await vercelDeploy(ref, commit);

if (githubDeploymentId) {
await createGithubDeploymentStatus(githubDeploymentId, 'success');
}
if (deploymentUrl) {
core.info('set preview-url output');
if (aliasDomains && aliasDomains.length) {
Expand Down
46 changes: 43 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ const aliasDomains = core
return url;
});

const githubDeployment = core.getInput('github-deployment') === 'true';
const githubDeploymentEnv = core.getInput('github-deployment-environment');

let octokit;
if (githubToken) {
octokit = new github.GitHub(githubToken);
Expand Down Expand Up @@ -302,17 +305,50 @@ async function aliasDomainsToDeployment(deploymentUrl) {
await Promise.all(promises);
}

function getEffectiveRef() {
let { ref } = context;
if (github.context.eventName === 'pull_request') {
const pullRequestPayload = github.context.payload;
ref = pullRequestPayload.pull_request.head.ref;
}
return ref;
}

async function createGithubDeployment() {
if (githubDeployment && octokit) {
core.debug(`Create a github deployment.`);
const deployment = await octokit.repos.createDeployment({
...context.repo,
ref: getEffectiveRef(),
environment: githubDeploymentEnv,
});
return deployment.id;
}
}

async function createGithubDeploymentStatus(deploymentId, state) {
if (githubDeployment && deploymentId && state && octokit) {
await octokit.repos.createDeploymentStatus({
...context.repo,
deployment_id: deploymentId,
state,
});
}
}

async function run() {
core.debug(`action : ${context.action}`);
core.debug(`ref : ${context.ref}`);
core.debug(`eventName : ${context.eventName}`);
core.debug(`actor : ${context.actor}`);
core.debug(`sha : ${context.sha}`);
core.debug(`workflow : ${context.workflow}`);
let { ref } = context;
let ref = getEffectiveRef();
let { sha } = context;
await setEnv();

const githubDeploymentId = await createGithubDeployment();

let commit = execSync('git log -1 --pretty=format:%B')
.toString()
.trim();
Expand All @@ -337,9 +373,13 @@ async function run() {
core.debug(`The head commit is: ${commit}`);
}
}

if (githubDeploymentId) {
await createGithubDeploymentStatus(githubDeploymentId, 'in_progress');
}
const deploymentUrl = await vercelDeploy(ref, commit);

if (githubDeploymentId) {
await createGithubDeploymentStatus(githubDeploymentId, 'success');
}
if (deploymentUrl) {
core.info('set preview-url output');
if (aliasDomains && aliasDomains.length) {
Expand Down

1 comment on commit d826954

@github-actions
Copy link

Choose a reason for hiding this comment

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

Deploy preview for zeit-now-deployment-action-example-static ready!

✅ Preview
https://zeit-now-deployment-action-example-static-5v3jpw0wh.vercel.app
https://featuredeployment.static.vercel-action.amond.dev

Built with commit d826954.
This pull request is being automatically deployed with vercel-action

Please sign in to comment.