-
Notifications
You must be signed in to change notification settings - Fork 8
feat: Add GitHub Actions CI/CD workflow #189
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
zohayb23
wants to merge
7
commits into
revanite-io:main
Choose a base branch
from
zohayb23:feature/add-comprehensive-cicd-workflow
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
fee1a4b
feat: Add comprehensive CI/CD workflow with multi-platform builds
zohayb23 68cab44
fix: Address security issues and apply workflow optimizations
zohayb23 9b70570
refactor: Split CI/CD workflow into separate workflows per review fee…
zohayb23 6fa2a83
fix: Correct Docker Hub image handling and fix indentation
zohayb23 bfb7e8c
chore: Update SDK and fix security vulnerabilities
9378d4b
Merge main into feature branch: resolve go.mod/go.sum conflicts
d0da0b6
Merge branch 'main' into feature/add-comprehensive-cicd-workflow
zohayb23 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| name: Build Binaries | ||
|
|
||
| on: | ||
| workflow_run: | ||
| workflows: ["build"] | ||
| types: [completed] | ||
| branches: [main, develop] | ||
| workflow_dispatch: | ||
|
|
||
| permissions: | ||
| contents: read | ||
| actions: read | ||
| checks: read | ||
|
|
||
| env: | ||
| GO_VERSION: "1.25.1" | ||
|
|
||
| jobs: | ||
| build-binaries: | ||
| name: Build Multi-Platform Binaries | ||
| runs-on: ubuntu-latest | ||
| if: ${{ github.event_name == 'workflow_dispatch' || (github.event.workflow_run.conclusion == 'success' && (github.event.workflow_run.event == 'push' || github.event.workflow_run.event == 'workflow_dispatch')) }} | ||
| strategy: | ||
| matrix: | ||
| include: | ||
| - os: linux | ||
| arch: amd64 | ||
| output: github-repo-linux-amd64 | ||
| - os: linux | ||
| arch: arm64 | ||
| output: github-repo-linux-arm64 | ||
| - os: darwin | ||
| arch: amd64 | ||
| output: github-repo-darwin-amd64 | ||
| - os: darwin | ||
| arch: arm64 | ||
| output: github-repo-darwin-arm64 | ||
| - os: windows | ||
| arch: amd64 | ||
| output: github-repo-windows-amd64.exe | ||
| steps: | ||
| - name: Validate workflow_run security | ||
| if: github.event_name == 'workflow_run' | ||
| uses: actions/github-script@v7 | ||
| with: | ||
| script: | | ||
| const workflowRun = context.payload.workflow_run; | ||
|
|
||
| // Validate workflow_run is from the same repository (not a fork) | ||
| if (!workflowRun || workflowRun.repository.full_name !== context.repo.owner + '/' + context.repo.repo) { | ||
| core.setFailed('Security: workflow_run must be from the same repository'); | ||
| return; | ||
| } | ||
|
|
||
| // Validate head SHA format | ||
| const headSha = workflowRun.head_sha; | ||
| if (!headSha || !/^[a-f0-9]{40}$/i.test(headSha)) { | ||
| core.setFailed('Invalid head SHA format'); | ||
| return; | ||
| } | ||
|
|
||
| // Validate branch is allowed | ||
| const allowedBranches = ['main', 'develop']; | ||
| if (!allowedBranches.includes(workflowRun.head_branch)) { | ||
| core.setFailed(`Security: workflow_run from branch '${workflowRun.head_branch}' is not allowed. Allowed branches: ${allowedBranches.join(', ')}`); | ||
| return; | ||
| } | ||
|
|
||
| // Check if lint workflow passed | ||
| const { data: runs } = await github.rest.actions.listWorkflowRuns({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| workflow_id: 'lint.yaml', | ||
| head_sha: headSha, | ||
| per_page: 1 | ||
| }); | ||
| if (runs.workflow_runs.length > 0 && runs.workflow_runs[0].conclusion !== 'success') { | ||
| core.setFailed('Lint workflow did not pass'); | ||
| } | ||
| - name: Checkout code | ||
| uses: actions/checkout@v5 | ||
| with: | ||
| persist-credentials: false | ||
|
|
||
| - name: Set up Go | ||
| uses: actions/setup-go@v5 | ||
| with: | ||
| go-version: ${{ env.GO_VERSION }} | ||
|
|
||
| - name: Build binary for ${{ matrix.os }}/${{ matrix.arch }} | ||
| env: | ||
| GOOS: ${{ matrix.os }} | ||
| GOARCH: ${{ matrix.arch }} | ||
| CGO_ENABLED: 0 | ||
| VERSION: ${{ github.event_name == 'workflow_dispatch' && github.ref_name || github.event.workflow_run.head_branch || 'unknown' }} | ||
| COMMIT_HASH: ${{ github.event_name == 'workflow_dispatch' && github.sha || github.event.workflow_run.head_sha || github.sha }} | ||
| OUTPUT_NAME: ${{ matrix.output }} | ||
| run: | | ||
| go build -ldflags="-s -w -X 'main.Version=${VERSION}' -X 'main.GitCommitHash=${COMMIT_HASH}' -X 'main.BuiltAt=$(date -u +%Y-%m-%dT%H:%M:%SZ)'" -o "${OUTPUT_NAME}" | ||
|
|
||
| - name: Upload binary artifact | ||
| uses: actions/upload-artifact@v4 | ||
| with: | ||
| name: ${{ matrix.output }} | ||
| path: ${{ matrix.output }} | ||
| retention-days: 30 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| name: Docker Build and Push | ||
|
|
||
| on: | ||
zohayb23 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| workflow_run: | ||
| workflows: ["build"] | ||
| types: [completed] | ||
| branches: [main, develop] | ||
| workflow_dispatch: | ||
|
|
||
| permissions: | ||
| contents: read | ||
| packages: write | ||
| id-token: write | ||
| attestations: write | ||
| actions: read | ||
| checks: read | ||
|
|
||
| env: | ||
| REGISTRY: ghcr.io | ||
| IMAGE_NAME: ${{ github.repository }} | ||
|
|
||
| jobs: | ||
| docker-build-push: | ||
| name: Build and Push Docker Image | ||
| runs-on: ubuntu-latest | ||
| if: ${{ github.event_name == 'workflow_dispatch' || (github.event.workflow_run.conclusion == 'success' && (github.event.workflow_run.head_branch == 'main' || github.event.workflow_run.head_branch == 'develop')) }} | ||
| steps: | ||
| - name: Validate workflow_run security | ||
| if: github.event_name == 'workflow_run' | ||
| uses: actions/github-script@v7 | ||
| with: | ||
| script: | | ||
| const workflowRun = context.payload.workflow_run; | ||
|
|
||
| // Validate workflow_run is from the same repository (not a fork) | ||
| if (!workflowRun || workflowRun.repository.full_name !== context.repo.owner + '/' + context.repo.repo) { | ||
| core.setFailed('Security: workflow_run must be from the same repository'); | ||
| return; | ||
| } | ||
|
|
||
| // Validate head SHA format | ||
| const headSha = workflowRun.head_sha; | ||
| if (!headSha || !/^[a-f0-9]{40}$/i.test(headSha)) { | ||
| core.setFailed('Invalid head SHA format'); | ||
| return; | ||
| } | ||
|
|
||
| // Validate branch is allowed | ||
| const allowedBranches = ['main', 'develop']; | ||
| if (!allowedBranches.includes(workflowRun.head_branch)) { | ||
| core.setFailed(`Security: workflow_run from branch '${workflowRun.head_branch}' is not allowed. Allowed branches: ${allowedBranches.join(', ')}`); | ||
| return; | ||
| } | ||
|
|
||
| // Check if lint workflow passed | ||
| const { data: runs } = await github.rest.actions.listWorkflowRuns({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| workflow_id: 'lint.yaml', | ||
| head_sha: headSha, | ||
| per_page: 1 | ||
| }); | ||
| if (runs.workflow_runs.length > 0 && runs.workflow_runs[0].conclusion !== 'success') { | ||
| core.setFailed('Lint workflow did not pass'); | ||
| } | ||
| - name: Checkout code | ||
| uses: actions/checkout@v5 | ||
| with: | ||
| persist-credentials: false | ||
| ref: ${{ github.event_name == 'workflow_dispatch' && github.sha || github.event.workflow_run.head_sha }} | ||
|
|
||
| - name: Set up Docker Buildx | ||
| uses: docker/setup-buildx-action@v3 | ||
|
|
||
| - name: Log in to GitHub Container Registry | ||
| uses: docker/login-action@v3 | ||
| with: | ||
| registry: ${{ env.REGISTRY }} | ||
| username: ${{ github.actor }} | ||
| password: ${{ secrets.GITHUB_TOKEN }} | ||
|
|
||
| - name: Prepare Docker Hub image name | ||
| id: dockerhub | ||
| run: | | ||
| if [ -n "${{ secrets.DOCKERHUB_TOKEN }}" ] && [ -n "${{ secrets.DOCKERHUB_USERNAME }}" ]; then | ||
| echo "image=${{ secrets.DOCKERHUB_USERNAME }}/pvtr-github-repo" >> $GITHUB_OUTPUT | ||
| echo "has_credentials=true" >> $GITHUB_OUTPUT | ||
| else | ||
| echo "has_credentials=false" >> $GITHUB_OUTPUT | ||
| fi | ||
|
|
||
| - name: Log in to Docker Hub | ||
| uses: docker/login-action@v3 | ||
| if: steps.dockerhub.outputs.has_credentials == 'true' | ||
| with: | ||
| username: ${{ secrets.DOCKERHUB_USERNAME }} | ||
| password: ${{ secrets.DOCKERHUB_TOKEN }} | ||
|
|
||
| - name: Prepare images list for metadata | ||
| id: prepare-images | ||
| run: | | ||
| echo "images<<EOF" >> $GITHUB_OUTPUT | ||
| echo "${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}" >> $GITHUB_OUTPUT | ||
| if [ "${{ steps.dockerhub.outputs.has_credentials }}" = "true" ]; then | ||
| echo "${{ steps.dockerhub.outputs.image }}" >> $GITHUB_OUTPUT | ||
| fi | ||
| echo "EOF" >> $GITHUB_OUTPUT | ||
|
|
||
| - name: Extract metadata | ||
| id: meta | ||
| uses: docker/metadata-action@v5 | ||
| with: | ||
| images: ${{ steps.prepare-images.outputs.images }} | ||
| tags: | | ||
| type=raw,value=latest,enable=${{ github.event_name == 'workflow_dispatch' && github.ref == 'refs/heads/main' || github.event.workflow_run.head_branch == 'main' }} | ||
| type=ref,event=branch | ||
| type=sha,prefix=${{ github.event_name == 'workflow_dispatch' && github.ref_name || github.event.workflow_run.head_branch }}- | ||
|
|
||
| - name: Build and push Docker image | ||
| id: build | ||
| uses: docker/build-push-action@v6 | ||
| with: | ||
| context: . | ||
| platforms: linux/amd64,linux/arm64 | ||
| push: true | ||
| tags: ${{ steps.meta.outputs.tags }} | ||
| labels: ${{ steps.meta.outputs.labels }} | ||
| cache-from: type=gha | ||
| cache-to: type=gha,mode=max | ||
| provenance: true | ||
| sbom: true | ||
|
|
||
| - name: Attest Build Provenance | ||
| uses: actions/attest-build-provenance@v2 | ||
| if: ${{ github.event_name == 'workflow_dispatch' && github.ref == 'refs/heads/main' || github.event.workflow_run.head_branch == 'main' }} | ||
| with: | ||
| subject-name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} | ||
| subject-digest: ${{ steps.build.outputs.digest }} | ||
| push-to-registry: true | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| name: Integration Test | ||
|
|
||
| on: | ||
zohayb23 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| workflow_run: | ||
| workflows: ["Docker Build and Push"] | ||
| types: [completed] | ||
| branches: [main] | ||
| workflow_dispatch: | ||
|
|
||
| permissions: | ||
| contents: read | ||
| packages: read | ||
|
|
||
| env: | ||
| REGISTRY: ghcr.io | ||
| IMAGE_NAME: ${{ github.repository }} | ||
|
|
||
| jobs: | ||
| integration-test: | ||
| name: Integration Test | ||
| runs-on: ubuntu-latest | ||
| if: ${{ github.event_name == 'workflow_dispatch' || (github.event.workflow_run.conclusion == 'success' && github.event.workflow_run.head_branch == 'main') }} | ||
| steps: | ||
| - name: Validate workflow_run security | ||
| if: github.event_name == 'workflow_run' | ||
| uses: actions/github-script@v7 | ||
| with: | ||
| script: | | ||
| const workflowRun = context.payload.workflow_run; | ||
|
|
||
| // Validate workflow_run is from the same repository (not a fork) | ||
| if (!workflowRun || workflowRun.repository.full_name !== context.repo.owner + '/' + context.repo.repo) { | ||
| core.setFailed('Security: workflow_run must be from the same repository'); | ||
| return; | ||
| } | ||
|
|
||
| // Validate head SHA format | ||
| const headSha = workflowRun.head_sha; | ||
| if (!headSha || !/^[a-f0-9]{40}$/i.test(headSha)) { | ||
| core.setFailed('Invalid head SHA format'); | ||
| return; | ||
| } | ||
|
|
||
| // Validate branch is allowed | ||
| const allowedBranches = ['main']; | ||
| if (!allowedBranches.includes(workflowRun.head_branch)) { | ||
| core.setFailed(`Security: workflow_run from branch '${workflowRun.head_branch}' is not allowed. Allowed branches: ${allowedBranches.join(', ')}`); | ||
| return; | ||
| } | ||
| - name: Checkout code | ||
| uses: actions/checkout@v5 | ||
| with: | ||
| persist-credentials: false | ||
| ref: ${{ github.event_name == 'workflow_dispatch' && github.sha || github.event.workflow_run.head_sha }} | ||
|
|
||
| - name: Create test config | ||
| env: | ||
| REPO_OWNER: ${{ github.repository_owner }} | ||
| REPO_NAME: ${{ github.event.repository.name }} | ||
| run: | | ||
| cat > test-config.yml << EOF | ||
| loglevel: info | ||
| write-directory: evaluation_results | ||
| write: true | ||
| output: yaml | ||
| services: | ||
| self-test: | ||
| plugin: github-repo | ||
| policy: | ||
| catalogs: | ||
| - OSPS_B | ||
| applicability: | ||
| - Maturity Level 1 | ||
| vars: | ||
| owner: ${REPO_OWNER} | ||
| repo: ${REPO_NAME} | ||
| token: \${{ secrets.GITHUB_TOKEN }} | ||
| EOF | ||
|
|
||
| - name: Run self-assessment using Docker image | ||
| run: | | ||
| docker run --rm \ | ||
| -v $(pwd)/test-config.yml:/.privateer/config.yml \ | ||
| -v $(pwd)/evaluation_results:/evaluation_results \ | ||
| ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest | ||
|
|
||
| - name: Upload evaluation results | ||
| uses: actions/upload-artifact@v4 | ||
| if: always() | ||
| with: | ||
| name: evaluation-results | ||
| path: evaluation_results/ | ||
| retention-days: 30 | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.