|
1 | | -name: Build Container and Push on GHCR |
2 | | - |
3 | | -on: |
4 | | - push: |
5 | | - branches: [ "main" ] |
6 | | - pull_request: |
7 | | - branches: [ "main" ] |
8 | | - workflow_dispatch: # Allows manual triggering of the workflow |
9 | | - |
10 | | -env: |
11 | | - # Use ghcr.io |
12 | | - REGISTRY: ghcr.io |
13 | | - # github.repository as <account>/<repo> |
14 | | - IMAGE_NAME: ${{ github.repository }} |
| 1 | +name: CI/CD |
15 | 2 |
|
| 3 | +on: [push] |
16 | 4 |
|
17 | 5 | jobs: |
18 | | - build: |
19 | | - |
| 6 | + build-and-test: |
20 | 7 | runs-on: ubuntu-latest |
21 | | - permissions: |
22 | | - contents: read |
23 | | - packages: write |
24 | | - |
25 | 8 | steps: |
26 | | - - name: Checkout repository |
27 | | - uses: actions/checkout@v4 |
28 | | - |
29 | | - - name: Extract version from version.py |
30 | | - run: | |
31 | | - version=$(grep 'version = ' version.py | sed -E "s/version = \"([^\"]+)\"/\1/") |
32 | | - echo "APPLICATION_VERSION=$version" >> $GITHUB_ENV |
33 | | - |
34 | | - - name: Log in to the Container registry |
35 | | - uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1 |
| 9 | + - uses: actions/checkout@v2 |
| 10 | + - name: Set up Node.js |
| 11 | + uses: actions/setup-node@v2 |
36 | 12 | with: |
37 | | - registry: ${{ env.REGISTRY }} |
38 | | - username: ${{ github.actor }} |
39 | | - password: ${{ secrets.GITHUB_TOKEN }} |
| 13 | + node-version: '16.x' |
| 14 | + - name: Install Dependencies |
| 15 | + run: npm install |
| 16 | + - name: Run Tests |
| 17 | + run: npm test |
40 | 18 |
|
41 | | - # Extract metadata (tags, labels) for Docker |
42 | | - # https://github.com/docker/metadata-action |
43 | | - - name: Extract metadata (tags, labels) for Docker |
44 | | - id: meta |
45 | | - uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7 |
46 | | - with: |
47 | | - images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
48 | | - tags: | |
49 | | - type=ref,event=branch |
50 | | - type=raw,value=${{ env.APPLICATION_VERSION }} |
51 | | -
|
52 | | - # Build and push Docker image with Buildx (don't push on PR) |
53 | | - # https://github.com/docker/build-push-action |
54 | | - - name: Build and push Docker image |
55 | | - id: build-and-push |
56 | | - uses: docker/build-push-action@ac9327eae2b366085ac7f6a2d02df8aa8ead720a |
57 | | - with: |
58 | | - context: . |
59 | | - push: true |
60 | | - tags: ${{ steps.meta.outputs.tags }} |
61 | | - labels: ${{ steps.meta.outputs.labels }} |
62 | | - |
63 | | - - name: Output image digest |
64 | | - id: output-digest |
| 19 | + deploy_to_production: |
| 20 | + runs-on: ubuntu-latest |
| 21 | + steps: |
| 22 | + - uses: actions/checkout@v2 |
| 23 | + - name: Configure AWS Credentials |
| 24 | + run: | |
| 25 | + echo "${{ secrets.AWS_ACCESS_KEY_ID }}" > aws_access_key_id |
| 26 | + echo "${{ secrets.AWS_SECRET_ACCESS_KEY }}" > aws_secret_access_key |
| 27 | + - name: Deploy to EKS |
65 | 28 | run: | |
66 | | - echo "digest: ${{ steps.build-and-push.outputs.digest }} size:" |
| 29 | + curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl" |
| 30 | + chmod +x ./kubectl |
| 31 | + sudo mv ./kubectl /usr/local/bin/kubectl |
| 32 | + pip install awscli --upgrade |
| 33 | + aws --version |
| 34 | + aws eks update-kubeconfig --name my-cluster |
| 35 | + LATEST_SHA=$(git rev-parse HEAD) |
| 36 | + docker build -t express-eks:$LATEST_SHA . |
| 37 | + docker tag express-eks:$LATEST_SHA 213561109591.dkr.ecr.us-east-1.amazonaws.com/express-eks:$LATEST_SHA |
| 38 | + docker push 213561109591.dkr.ecr.us-east-1.amazonaws.com/express-eks:$LATEST_SHA |
| 39 | + docker tag express-eks:$LATEST_SHA 213561109591.dkr.ecr.us-east-1.amazonaws.com/express-eks:latest |
| 40 | + docker push 213561109591.dkr.ecr.us-east-1.amazonaws.com/express-eks:latest |
| 41 | + kubectl set image deployment/myapp myapp=213561109591.dkr.ecr.us-east-1.amazonaws.com/express-eks:$LATEST_SHA -n default |
0 commit comments