diff --git a/package.json b/package.json index 00a1b60..122fd5a 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,7 @@ "@actions/core": "^1.9.1", "@actions/github": "^4.0.0", "@vercel/ncc": "^0.27.0", - "axios": "^0.27.2", + "axios": "^1.8.2", "dotenv": "^8.2.0" } } diff --git a/src/action.js b/src/action.js index c8efcbe..18a393e 100644 --- a/src/action.js +++ b/src/action.js @@ -4,73 +4,73 @@ const core = require('@actions/core'); const github = require('@actions/github'); async function run() { - const GITHUB_TOKEN = core.getInput('GITHUB_TOKEN'); - const CSW_URL = core.getInput('CSW_URL'); - const IMAGE_NAME = core.getInput('IMAGE_NAME'); - const CSW_APP_ID = core.getInput('CSW_APP_ID'); - const REGISTRY_URL = core.getInput('REGISTRY_URL'); - const REGISTRY_USER = core.getInput('REGISTRY_USER'); - const REGISTRY_PASSWORD = core.getInput('REGISTRY_PASSWORD'); - const CREATE_SUCCESS_ISSUE = core.getInput('CREATE_SUCCESS_ISSUE'); - const CREATE_FAILURE_ISSUE = core.getInput('CREATE_FAILURE_ISSUE'); + // Get inputs from GitHub action + const inputs = [ + 'GITHUB_TOKEN', + 'CSW_URL', + 'IMAGE_NAME', + 'CSW_APP_ID', + 'REGISTRY_URL', + 'REGISTRY_USER', + 'REGISTRY_PASSWORD', + 'CREATE_SUCCESS_ISSUE', + 'CREATE_FAILURE_ISSUE' + ]; + const values = inputs.map(input => core.getInput(input)); - if (typeof GITHUB_TOKEN !== 'string') { - throw new Error('Invalid GITHUB_TOKEN: did you forget to set it in your action config?'); - } - - if (typeof CSW_URL !== 'string') { - throw new Error('Invalid CSW_URL: did you forget to set it in your action config?'); - } - - if (typeof CSW_APP_ID !== 'string') { - throw new Error('Invalid CSW_APP_ID: did you forget to set it in your action config?'); + // Validate inputs + const validateInput = (name, value) => { + if (typeof value !== 'string') { + throw new Error(`Invalid ${name}: did you forget to set it in your action config?`); + } } - if (typeof IMAGE_NAME !== 'string') { - throw new Error('Invalid IMAGE_NAME: did you forget to set it in your action config?'); - } + inputs.forEach((input, index) => validateInput(input, values[index])); + // Deploy app using inputs provided by GitHub action const context = github.context; - const octokit = github.getOctokit(GITHUB_TOKEN) + const octokit = github.getOctokit(GITHUB_TOKEN); + // Send request to deploy app const response = await axios.post(`${CSW_URL}/api/v1/apps/deploy`, { "image": `${IMAGE_NAME}`, "app_id": CSW_APP_ID, "registry": REGISTRY_URL, "registry_user": REGISTRY_USER, "registry_pass": REGISTRY_PASSWORD - }) + }); - if (response.status !== 200) { - if (CREATE_FAILURE_ISSUE === 'true') { + // Check response status and create issue if needed + if (response.status === 200) { + if (CREATE_SUCCESS_ISSUE === 'true') { await octokit.issues.create({ ...context.repo, - title: 'Deployment failed', - body: `Deployment failed for ${IMAGE_NAME}`, + title: 'Deployment successful', + body: `Deployment successful for ${IMAGE_NAME}`, labels: ['deployment', { - name: 'deployment-failed', - color: '#B60205' + name: 'deployment-success', + color: '#0E8A16' } ] }); } - core.setFailed(`Deployment failed: please check the logs for more information`); + core.setOutput('deployment', "App deployed successfully"); } else { - if (CREATE_SUCCESS_ISSUE === 'true') { + if (CREATE_FAILURE_ISSUE === 'true') { await octokit.issues.create({ ...context.repo, - title: 'Deployment successful', - body: `Deployment successful for ${IMAGE_NAME}`, + title: 'Deployment failed', + body: `Deployment failed for ${IMAGE_NAME}`, labels: ['deployment', { - name: 'deployment-success', - color: '#0E8A16' + name: 'deployment-failed', + color: '#B60205' } ] }); } - core.setOutput('deployment', "App deployed successfully"); + core.setFailed(`Deployment failed: please check the logs for more information`); } } diff --git a/yarn.lock b/yarn.lock index 9c0144d..9ad8b95 100644 --- a/yarn.lock +++ b/yarn.lock @@ -171,13 +171,14 @@ asynckit@^0.4.0: resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79" integrity sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q== -axios@^0.27.2: - version "0.27.2" - resolved "https://registry.yarnpkg.com/axios/-/axios-0.27.2.tgz#207658cc8621606e586c85db4b41a750e756d972" - integrity sha512-t+yRIyySRTp/wua5xEr+z1q60QmLq8ABsS5O9Me1AsE5dfKqgnCFzwiCZZ/cGNd1lq4/7akDWMxdhVlucjmnOQ== +axios@^1.8.2: + version "1.8.2" + resolved "https://registry.yarnpkg.com/axios/-/axios-1.8.2.tgz#fabe06e241dfe83071d4edfbcaa7b1c3a40f7979" + integrity sha512-ls4GYBm5aig9vWx8AWDSGLpnpDQRtWAfrjU+EuytuODrFBkqesN2RkOQCBzrA1RQNHw1SmRMSDDDSwzNAYQ6Rg== dependencies: - follow-redirects "^1.14.9" + follow-redirects "^1.15.6" form-data "^4.0.0" + proxy-from-env "^1.1.0" before-after-hook@^2.2.0: version "2.2.2" @@ -206,10 +207,10 @@ dotenv@^8.2.0: resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-8.2.0.tgz#97e619259ada750eea3e4ea3e26bceea5424b16a" integrity sha512-8sJ78ElpbDJBHNeBzUbUVLsqKdccaa/BXF1uPTw3GrvQTBgrQrtObr2mUrE38vzYd8cEv+m/JBfDLioYcfXoaw== -follow-redirects@^1.14.9: - version "1.15.0" - resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.0.tgz#06441868281c86d0dda4ad8bdaead2d02dca89d4" - integrity sha512-aExlJShTV4qOUOL7yF1U5tvLCB0xQuudbf6toyYA0E/acBNw71mvjFTnLaRp50aQaYocMR0a/RMMBIHeZnGyjQ== +follow-redirects@^1.15.6: + version "1.15.9" + resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.9.tgz#a604fa10e443bf98ca94228d9eebcc2e8a2c8ee1" + integrity sha512-gew4GsXizNgdoRyqmyfMHyAmXsZDk6mHkSxZFCzW9gwlbtOW44CDtYavM+y+72qD/Vq2l550kMF52DT8fOLJqQ== form-data@^4.0.0: version "4.0.0" @@ -251,6 +252,11 @@ once@^1.4.0: dependencies: wrappy "1" +proxy-from-env@^1.1.0: + version "1.1.0" + resolved "https://registry.yarnpkg.com/proxy-from-env/-/proxy-from-env-1.1.0.tgz#e102f16ca355424865755d2c9e8ea4f24d58c3e2" + integrity sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg== + tr46@~0.0.3: version "0.0.3" resolved "https://registry.yarnpkg.com/tr46/-/tr46-0.0.3.tgz#8184fd347dac9cdc185992f3a6622e14b9d9ab6a"