Login into Portainer using Github Actions Pipeline

I'm facing an issue while deploying a docker image using portainer on EC2 instance. In my pipeline my last step is

  1. login into portainer
  2. update the image in the specific container.
name: Pipeline

on:
  push:  
    branches:
        master

permissions:
      id-token: write   
      contents: read  
jobs:
  build:
    runs-on: ubuntu-latest
    env:
      AWS_REGION: us-east-1 
      PORTAINER_URL: ${{ secrets.PORTAINER_URL }} # Portainer URL
      PORTAINER_API_KEY: ${{ secrets.PORTAINER_API_KEY }} # Portainer API key
      PORTAINER_STACK_ID: 21 # The Stack ID you want to update
      PORTAINER_PASSWORD: ${{ secrets.PASSWORD }} 
    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2
     
      - name: Print AWS Region (Debug)
        run: echo "AWS Region:"  ${{ env.AWS_REGION }}


      - name: Set up Docker Compose
        run: |
          sudo apt-get update
          sudo apt-get install -y docker-compose



      - name: Configure AWS credentials
        uses: aws-actions/configure-aws-credentials@v3
        with:
            aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
            aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
            aws-region: ${{ env.AWS_REGION }}

      - name: Setting Docker Image Docker
        run: |
          BRANCH_NAME="master"
          COMMIT_SHA=${GITHUB_SHA::7}
          IMAGE_TAG="${BRANCH_NAME}-${COMMIT_SHA}"
          echo "IMAGE_TAG=$IMAGE_TAG" >> $GITHUB_ENV
          echo "PORTAINER_URL=$PORTAINER_URL"


      - name: Build and push Docker images with Docker Compose
        run: |
         
          ECR_REPO_NAME= ${{REPO_NAME}}
          
          
          aws ecr get-login-password --region $AWS_REGION | docker login --username AWS --password-stdin $ECR_REPO_NAME

         
          docker-compose -f docker-compose.yml build

         
          docker tag sludoapi:latest $ECR_REPO_NAME:$IMAGE_TAG

        
          docker push $ECR_REPO_NAME:$IMAGE_TAG

      - name: Login to Portainer API
        run: |
            # Authenticate with Portainer API to get the JWT token
            RESPONSE=$(curl -s -X POST $PORTAINER_URL/api/auth \
              -H "Content-Type: application/json" \
              -d '{"Username": "admin", "Password":"'${{ secrets.PASSWORD }}'"}')
  
            PORTAINER_JWT=$(echo $RESPONSE | jq -r '.jwt')  


            # Getting the current stack
            
            CURRENT_STACK=$(curl -s -X GET "$PORTAINER_URL/api/stacks/21?endpointId=2" \
               -H "Authorization: Bearer $PORTAINER_JWT" \
               -H "Content-Type: application/json")

            CURRENT_STACK_CONTENT=$(echo "$CURRENT_STACK" | jq -r '.StackFileContent')

            # Update only the image section in the stack file content
            UPDATED_STACK=$(echo "$CURRENT_STACK_CONTENT" | sed "s|image:.*|image: 532597438699.dkr.ecr.us-east-1.amazonaws.com/skulls-api:$IMAGE_TAG|")

            UPDATED_STACK_JSON=$(echo "$UPDATED_STACK_CONTENT" | jq -Rs .)


            curl -s -X PUT "$PORTAINER_URL/api/stacks/21?endpointId=2" \
            -H "Authorization: Bearer $PORTAINER_JWT" \
            -H "Content-Type: application/json" \
            -d '{
              "StackFileContent": '"$UPDATED_STACK_JSON"'
              }'

The configuration for this is given above, all steps are working fine but not the last one, the logs I'm getting for this are

Run # Authenticate with Portainer API to get the JWT token
{"message":"failed to deploy a stack: parsing /data/compose/21/docker-compose.yml: Top-level object must be a mapping\n","details":"failed to deploy a stack: parsing /data/compose/21/docker-compose.yml: Top-level object must be a mapping\n"}

while the syntax of my stack in the portainer is fine for reference I can share too if needed. Please guide me how to solve this thing.

The content of my stack file is:

version: '3.9'
services:
  skullsapi:
    image: image-link
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      # - ASPNETCORE_HTTPS_PORTS=8081
      # - ASPNETCORE_HTTP_PORTS=8080
      - ASPNETCORE_URLS=http://+:8080
    restart: always
    ports:
      - "8886:8080"

networks:
  default:
    name: cloud
    external: true     

Upvotes: 0

Views: 30

Answers (0)

Related Questions