|
| 1 | +name: Trigger GitLab pipeline |
| 2 | +on: |
| 3 | + workflow_call: |
| 4 | + inputs: |
| 5 | + triggered-ref: |
| 6 | + description: 'GitLab project ref to trigger' |
| 7 | + required: true |
| 8 | + type: string |
| 9 | + schedule: |
| 10 | + description: 'Indication if it is a automatically scheduled request' |
| 11 | + required: false |
| 12 | + default: false |
| 13 | + type: boolean |
| 14 | + cancel-outdated-pipelines: |
| 15 | + description: 'If set to true, it will cancel previous pipelines that are running for the same github ref' |
| 16 | + required: false |
| 17 | + default: true |
| 18 | + type: boolean |
| 19 | + secrets: |
| 20 | + ci-api-v4-url: |
| 21 | + description: 'GitLab API v4 root URL' |
| 22 | + required: true |
| 23 | + access-token: |
| 24 | + description: 'GitLab API access token' |
| 25 | + required: true |
| 26 | + trigger-token: |
| 27 | + description: 'GitLab API trigger token' |
| 28 | + required: true |
| 29 | + project-id: |
| 30 | + description: 'GitLab project ID' |
| 31 | + required: true |
| 32 | + |
| 33 | +jobs: |
| 34 | + authorize: |
| 35 | + environment: ${{ (github.event_name == 'pull_request_target' && |
| 36 | + github.event.pull_request.head.repo.full_name != github.repository) && |
| 37 | + 'External' || 'Internal' }} |
| 38 | + runs-on: ubuntu-latest |
| 39 | + id: authorize-job |
| 40 | + steps: |
| 41 | + - name: Authorization confirmation |
| 42 | + run: echo "Authorized the job to run" # This step will only execute if |
| 43 | + # the pipeline has necessary approvals to run |
| 44 | + |
| 45 | + # We need workflow resolution to run on non-private runner due to required dependencies |
| 46 | + # missing in private runner. Additionally for clarity it is separated into separate job |
| 47 | + resolve-workflow-ref: |
| 48 | + needs: authorize |
| 49 | + runs-on: ubuntu-latest |
| 50 | + steps: |
| 51 | + - name: Get Workflow Version |
| 52 | + uses: canonical/get-workflow-version-action@a5d53b08d254a157ea441c9819ea5002ffc12edc |
| 53 | + id: workflow-ref |
| 54 | + with: |
| 55 | + repository-name: NordSecurity/trigger-gitlab-pipeline |
| 56 | + file-name: .github/workflows/trigger-gitlab-pipeline.yml |
| 57 | + outputs: |
| 58 | + workflow-ref: ${{ steps.workflow-ref.output.sha }} |
| 59 | + |
| 60 | + trigger-gitlab-pipeline: |
| 61 | + # In the current form, "authorize" job is implicitly required by trigger-gitlab-pipeline job |
| 62 | + # To make this dependency super explicit and a bit more future-proof against modifications of |
| 63 | + # this workflow - it is stated explicitly in the list of dependencies, even though it is |
| 64 | + # redundant |
| 65 | + needs: [resolve-workflow-ref, authorize] |
| 66 | + runs-on: [self-hosted, gitlab] |
| 67 | + steps: |
| 68 | + - uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4.2.1 |
| 69 | + with: |
| 70 | + repository: NordSecurity/trigger-gitlab-pipeline |
| 71 | + ref: ${{ needs.resolve-workflow-ref.outputs.workflow-ref }} |
| 72 | + - uses: actions/setup-node@0a44ba7841725637a19e28fa30b79a866c81b0a6 # v4.0.4 |
| 73 | + with: |
| 74 | + node-version: 20 |
| 75 | + - name: Dependencies install |
| 76 | + run: npm install |
| 77 | + - name: Run triggering script |
| 78 | + run: node index.js # It will not be accessible as of now, but it is enough for testing. |
| 79 | + env: |
| 80 | + TRIGGERED_REF: ${{ inputs.triggered-ref }} |
| 81 | + SCHEDULE: ${{ inputs.schedule }} |
| 82 | + CANCEL_OUTDATED_PIPELINES: ${{ inputs.cancel-outdated-pipelines }} |
| 83 | + CI_API_V4_URL: ${{ secrets.ci-api-v4-url }} |
| 84 | + ACCESS_TOKEN: ${{ secrets.access-token }} |
| 85 | + TRIGGER_TOKEN: ${{ secrets.trigger-token }} |
| 86 | + PROJECT_ID: ${{ secrets.project-id }} |
| 87 | + |
| 88 | + |
| 89 | + |
0 commit comments