diff --git a/.github/workflows/buildScripts/downloadArtifacts.js b/.github/workflows/buildScripts/downloadArtifacts.js new file mode 100644 index 0000000..a1d549a --- /dev/null +++ b/.github/workflows/buildScripts/downloadArtifacts.js @@ -0,0 +1,26 @@ +module.exports = async ({ github, context, core, jobId, name }) => { + const artifacts = await github.rest.actions.listWorkflowRunArtifacts({ + owner: context.repo.owner, + repo: context.repo.repo, + run_id: jobId, + }); + if (artifacts.data.total_count === 0) { + core.setFailed("No artifacts found for jobID: " + jobId); + return false; + } + const artifact = artifacts.data.artifacts.find( + (artifact) => artifact.name === name + ); + if (!artifact) { + core.setFailed(`${name} not found in artifacts`); + return false; + } + const response = await github.rest.actions.downloadArtifact({ + owner: context.repo.owner, + repo: context.repo.repo, + artifact_id: artifact.id, + archive_format: "zip" + }); + require('fs').writeFileSync(`${name}.zip`, Buffer.from(response.data)); + require('child_process').execSync(`unzip -o ${name}.zip -d ./${jobId}`); +}; diff --git a/.github/workflows/buildScripts/getLastBuildId.js b/.github/workflows/buildScripts/getLastBuildId.js new file mode 100644 index 0000000..a82c0ed --- /dev/null +++ b/.github/workflows/buildScripts/getLastBuildId.js @@ -0,0 +1,11 @@ +module.exports = async ({ github, context }) => { + const { data: runs } = await github.rest.actions.listWorkflowRuns({ + owner: context.repo.owner, + repo: context.repo.repo, + workflow_id: "build.yml", + branch: "main", + status: "success", + per_page: 1, + }); + return runs.workflow_runs[0].id; +}; diff --git a/.github/workflows/buildScripts/replaceTokens.js b/.github/workflows/buildScripts/replaceTokens.js new file mode 100644 index 0000000..c6d3f4a --- /dev/null +++ b/.github/workflows/buildScripts/replaceTokens.js @@ -0,0 +1,3 @@ +module.exports = async ({ github, context, core, jobId }) => { + +} \ No newline at end of file diff --git a/.github/workflows/release-morska.yml b/.github/workflows/release-morska.yml new file mode 100644 index 0000000..fad2634 --- /dev/null +++ b/.github/workflows/release-morska.yml @@ -0,0 +1,49 @@ +name: Morska Release +on: + workflow_dispatch: + inputs: + job_id: + description: 'Job ID of the build to release' + required: false + +permissions: + actions: read + contents: read + +jobs: + release: + runs-on: ubuntu-latest + environment: Morska + + steps: + - uses: actions/checkout@v3 + - name: Assign run ID if provided + if: ${{ github.event.inputs.job_id != '' }} + run: echo "job_id=${{ github.event.inputs.job_id }}" >> $GITHUB_ENV + - name: Get last build ID + id: get-build-id + if: ${{ github.event.inputs.job_id == '' }} + uses: actions/github-script@v6 + with: + script: | + const script = require('./.github/workflows/buildScripts/getLastBuildId.js'); + const jobId = await script({github, context}); + core.setOutput("job_id", jobId); + - name: Set job_id + if: ${{ github.event.inputs.job_id == '' }} + run: echo "job_id=${{ steps.get-build-id.outputs.job_id }}" >> $GITHUB_ENV + - name: Download frontend artifacts + if: ${{ github.event.inputs.job_id == '' }} + uses: actions/github-script@v6 + with: + script: | + const script = require('./.github/workflows/buildScripts/downloadArtifacts.js'); + await script({github, context, core, jobId: ${{env.job_id}}, name: 'frontend'}); + - name: Download backend artifacts + if: ${{ github.event.inputs.job_id == '' }} + uses: actions/github-script@v6 + with: + script: | + const script = require('./.github/workflows/buildScripts/downloadArtifacts.js'); + await script({github, context, core, jobId: ${{env.job_id}}, name: 'webapi'}); + diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml deleted file mode 100644 index 502f030..0000000 --- a/.github/workflows/release.yml +++ /dev/null @@ -1,58 +0,0 @@ -name: ReleaseIntoMorska - -on: - workflow_dispatch: # Allows manual triggering - inputs: - run-id: - description: 'ID of the run to deploy' - required: true -jobs: - release: - runs-on: ubuntu-latest - environment: morska - steps: - - uses: actions/checkout@v4 - # Step 2: Download build artifacts from another workflow (e.g., build pipeline) - - name: Download Frontend Artifacts - uses: actions/download-artifact@v4 - with: - run-id: 11989658555 - - - #- name: Copy artifacts into server - # uses: appleboy/scp-action@v0.1.7 - # with: - # host: "crm.bim-it.pl" - # username: "mz" - # key: ${{ secrets.SSH_KEY }} - # port: 22 - # source: "*" - # target: "/home/mz/github" - - - - # Step 3: Replace tokens in files with GitHub secrets - #- name: Replace tokens in files - # run: | - # find ./artifact -type f -exec sed -i "s/{{TOKEN_SECRET}}/${{ secrets.TOKEN_SECRET }}/g" {} \; - - # Step 4: Compress the artifact folder - #- name: Compress files - # run: | - # tar -czvf release_artifact.tar.gz -C ./artifact . - - # Step 5: Set up SSH agent with the private key - #- name: Setup SSH - # uses: webfactory/ssh-agent@v0.5.4 - # with: - # ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }} - - # Step 6: Upload the compressed file to the remote server via SCP - #- name: Upload to remote server - # run: | - # scp release_artifact.tar.gz ${{ secrets.REMOTE_USER }}@${{ secrets.REMOTE_HOST }}:/path/to/destination - - # Step 7: Execute a script on the remote server - #- name: Execute remote script - # run: | - # ssh ${{ secrets.REMOTE_USER }}@${{ secrets.REMOTE_HOST }} "bash /path/to/destination/deploy.sh"