Claude
Skills
Sign in
Back

ci-cd-pipelines

Included with Lifetime
$97 forever

GitHub Actions, GitLab CI/CD, Jenkins, Azure DevOps, build and test strategies, and deployment patterns

Cloud & DevOps

What this skill does


# CI/CD Pipelines

## GitHub Actions Workflows

### Core Concepts
- **Workflows**: YAML files defining automation processes
- **Events**: Triggers for workflow execution (push, pull_request, schedule)
- **Jobs**: Collections of steps that run on the same runner
- **Steps**: Individual tasks within a job
- **Actions**: Reusable components for common tasks

### Best Practices
- **Workflow Organization**
  - Use separate workflows for different concerns (CI, CD, release)
  - Use workflow templates for consistency
  - Use composite actions for reusable steps
  - Use reusable workflows for shared pipeline logic
  - Use matrix strategy for testing across multiple configurations

- **Job Optimization**
  - Use caching for dependencies and build artifacts
  - Use parallel jobs for faster execution
  - Use job dependencies for sequential execution
  - Use artifacts for passing data between jobs
  - Use self-hosted runners for custom environments

- **Security**
  - Use secrets for sensitive data
  - Use environments for deployment protection
  - Use required reviews for critical deployments
  - Use OIDC for cloud provider authentication
  - Use branch protection rules

### GitHub Actions Examples
```yaml
name: CI/CD Pipeline

on:
  push:
    branches: [main, develop]
  pull_request:
    branches: [main]

jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [14.x, 16.x, 18.x]
    steps:
      - uses: actions/checkout@v3
      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node-version }}
          cache: 'npm'
      - run: npm ci
      - run: npm test
      - run: npm run build

  deploy:
    needs: test
    runs-on: ubuntu-latest
    if: github.ref == 'refs/heads/main'
    steps:
      - uses: actions/checkout@v3
      - name: Deploy to production
        run: |
          # Deployment commands
```

## GitLab CI/CD Pipelines

### Core Concepts
- **Pipelines**: Series of jobs executed in stages
- **Stages**: Logical grouping of jobs
- **Jobs**: Individual tasks that execute scripts
- **Runners**: Agents that execute jobs
- **Artifacts**: Files passed between jobs

### Best Practices
- **Pipeline Configuration**
  - Use `.gitlab-ci.yml` for pipeline definition
  - Use stages for logical job grouping
  - Use only/except rules for job execution control
  - Use rules for more complex conditions
  - Use needs for job dependencies

- **Job Optimization**
  - Use artifacts for passing data between jobs
  - Use cache for dependency caching
  - Use parallel jobs for faster execution
  - Use retry for transient failures
  - Use timeout for job duration limits

- **Security**
  - Use CI/CD variables for sensitive data
  - Use protected variables for protected branches
  - Use masked variables for hiding values
  - Use environments for deployment control
  - Use approval rules for critical deployments

### GitLab CI/CD Examples
```yaml
stages:
  - test
  - build
  - deploy

test:
  stage: test
  image: node:18
  script:
    - npm ci
    - npm test
  parallel:
    matrix:
      - NODE_VERSION: [14, 16, 18]
  cache:
    paths:
      - node_modules/

build:
  stage: build
  image: node:18
  script:
    - npm ci
    - npm run build
  artifacts:
    paths:
      - dist/

deploy:
  stage: deploy
  image: node:18
  script:
    - npm install -g serverless
    - serverless deploy
  only:
    - main
  when: manual
```

## Jenkins Pipelines

### Core Concepts
- **Pipelines**: Groovy scripts defining automation
- **Stages**: Logical grouping of steps
- **Steps**: Individual operations
- **Agents**: Nodes where pipeline runs
- **Credentials**: Secure storage for secrets

### Best Practices
- **Pipeline Design**
  - Use Declarative Pipeline syntax
  - Use shared libraries for reusable code
  - Use pipeline stages for logical organization
  - Use when conditions for conditional execution
  - Use post sections for cleanup and notifications

- **Agent Management**
  - Use labels for agent selection
  - Use Docker agents for consistent environments
  - Use Kubernetes agents for dynamic scaling
  - Use agent none for lightweight pipelines
  - Use stash/unstash for passing data

- **Security**
  - Use credentials binding for secrets
  - Use credential scopes for access control
  - Use approval steps for manual gates
  - Use input steps for user interaction
  - Use role-based strategy for permissions

### Jenkins Pipeline Examples
```groovy
pipeline {
    agent any
    
    stages {
        stage('Test') {
            parallel {
                stage('Node 14') {
                    agent { docker { image 'node:14' } }
                    steps {
                        sh 'npm ci'
                        sh 'npm test'
                    }
                }
                stage('Node 16') {
                    agent { docker { image 'node:16' } }
                    steps {
                        sh 'npm ci'
                        sh 'npm test'
                    }
                }
            }
        }
        
        stage('Build') {
            agent { docker { image 'node:18' } }
            steps {
                sh 'npm ci'
                sh 'npm run build'
                archiveArtifacts artifacts: 'dist/**'
            }
        }
        
        stage('Deploy') {
            when {
                branch 'main'
            }
            steps {
                input message: 'Deploy to production?', ok: 'Deploy'
                sh 'npm install -g serverless'
                sh 'serverless deploy'
            }
        }
    }
    
    post {
        always {
            cleanWs()
        }
        success {
            emailext subject: 'Build Success',
                body: 'The build completed successfully.',
                to: '[email protected]'
        }
        failure {
            emailext subject: 'Build Failed',
                body: 'The build failed.',
                to: '[email protected]'
        }
    }
}
```

## Azure DevOps Pipelines

### Core Concepts
- **Pipelines**: YAML or GUI-defined automation
- **Stages**: Major divisions in a pipeline
- **Jobs**: Units of work within a stage
- **Tasks**: Pre-built units of work
- **Agents**: Machines that run jobs

### Best Practices
- **Pipeline Configuration**
  - Use YAML pipelines for version control
  - Use templates for reusable pipeline logic
  - Use parameters for pipeline customization
  - Use variables for configuration values
  - Use stages for environment separation

- **Job Optimization**
  - Use parallel jobs for faster execution
  - Use artifacts for passing data between jobs
  - Use caching for dependency caching
  - Use matrix strategy for multiple configurations
  - Use demands for agent selection

- **Security**
  - Use secret variables for sensitive data
  - Use variable groups for organization
   - Use key vault for secret management
  - Use environment checks for deployment control
  - Use approval gates for manual intervention

### Azure DevOps Pipeline Examples
```yaml
trigger:
- main
- develop

pool:
  vmImage: 'ubuntu-latest'

stages:
- stage: Test
  jobs:
  - job: Test
    strategy:
      matrix:
        Node_14:
          node_version: 14.x
        Node_16:
          node_version: 16.x
        Node_18:
          node_version: 18.x
    steps:
    - task: UseNode@1
      inputs:
        versionSpec: $(node_version)
    - script: |
        npm ci
        npm test
      displayName: 'Install dependencies and run tests'

- stage: Build
  dependsOn: Test
  jobs:
  - job: Build
    steps:
    - task: UseNode@1
      inputs:
        versionSpec: '18.x'
    - script: |
        npm ci
        npm run build
      displayName: 'Build application'
    - publish: dist
      artifact: dist

- stage: Deploy
  dependsOn: Build
  condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/main'))
  jobs:
  - deployment: Deploy
    environment: 'production'
    stra

Related in Cloud & DevOps