/ src / main.ts
main.ts
 1  import {context} from '@actions/github';
 2  import * as core from '@actions/core';
 3  import * as exec from '@actions/exec';
 4  import * as github from '@actions/github';
 5  import {Inputs} from './interfaces';
 6  import {showInputs, getInputs} from './get-inputs';
 7  import {setTokens} from './set-tokens';
 8  import {setRepo, setCommitAuthor, getCommitMessage, commit, push, pushTag} from './git-utils';
 9  import {getWorkDirName, addNoJekyll, addCNAME, skipOnFork} from './utils';
10  
11  export async function run(): Promise<void> {
12    try {
13      core.info('[INFO] Usage https://github.com/peaceiris/actions-gh-pages#readme');
14  
15      const inps: Inputs = getInputs();
16      core.startGroup('Dump inputs');
17      showInputs(inps);
18      core.endGroup();
19  
20      if (core.isDebug()) {
21        core.startGroup('Debug: dump context');
22        console.log(context);
23        core.endGroup();
24      }
25  
26      const eventName = context.eventName;
27      if (eventName === 'pull_request' || eventName === 'push') {
28        // eslint-disable-next-line @typescript-eslint/no-explicit-any
29        const isForkRepository = (context.payload as any).repository.fork;
30        const isSkipOnFork = await skipOnFork(
31          isForkRepository,
32          inps.GithubToken,
33          inps.DeployKey,
34          inps.PersonalToken
35        );
36        if (isSkipOnFork) {
37          core.warning('This action runs on a fork and not found auth token, Skip deployment');
38          core.setOutput('skip', 'true');
39          return;
40        }
41      }
42  
43      core.startGroup('Setup auth token');
44      const remoteURL = await setTokens(inps);
45      core.debug(`remoteURL: ${remoteURL}`);
46      core.endGroup();
47  
48      core.startGroup('Prepare publishing assets');
49      const date = new Date();
50      const unixTime = date.getTime();
51      const workDir = await getWorkDirName(`${unixTime}`);
52      await setRepo(inps, remoteURL, workDir);
53      await addNoJekyll(workDir, inps.DisableNoJekyll);
54      await addCNAME(workDir, inps.CNAME);
55      core.endGroup();
56  
57      core.startGroup('Setup Git config');
58      try {
59        await exec.exec('git', ['remote', 'rm', 'origin']);
60      } catch (e) {
61        core.info(`[INFO] ${e.message}`);
62      }
63      await exec.exec('git', ['remote', 'add', 'origin', remoteURL]);
64      await exec.exec('git', ['add', '--all']);
65      await setCommitAuthor(inps.UserName, inps.UserEmail);
66      core.endGroup();
67  
68      core.startGroup('Create a commit');
69      const hash = `${process.env.GITHUB_SHA}`;
70      const baseRepo = `${github.context.repo.owner}/${github.context.repo.repo}`;
71      const commitMessage = getCommitMessage(
72        inps.CommitMessage,
73        inps.FullCommitMessage,
74        inps.ExternalRepository,
75        baseRepo,
76        hash
77      );
78      await commit(inps.AllowEmptyCommit, commitMessage);
79      core.endGroup();
80  
81      core.startGroup('Push the commit or tag');
82      await push(inps.PublishBranch, inps.ForceOrphan);
83      await pushTag(inps.TagName, inps.TagMessage);
84      core.endGroup();
85  
86      core.info('[INFO] Action successfully completed');
87  
88      return;
89    } catch (e) {
90      throw new Error(e.message);
91    }
92  }