0

I have a Laravel project with a GitHub Action workflow, it triggers on code push, then logged into EC2, and execute following commands:

- name: SSH into EC2 instance
  uses: appleboy/ssh-action@master
  with:
    host: ${{secrets.EC2_HOST}}
    username: ${{secrets.EC2_USERNAME}}
    key: ${{ secrets.EC2_SECRET }}
    script: |
        cd /var/www/project
        **git pull origin test**
        cp .env.production .env
        cp docker-compose.test.yml docker-compose.yml
        docker-compose build
        docker-compose down
        docker-compose up -d

I am succesfully logged into server and when the following scripts runs.

Problem faced:

The command **git pull origin test** against the EC2 server is failing – due to merge conflict.

I don't want to execute further scripts and abort here. Or can undo changes made by any previous script.

Thanks in advance.

Ali Raza
  • 155
  • 7
  • You write you have searched through the internet. Please provide references with a short summary why you considered each resource in the first place and why it didn't match your expectations. Otherwise such a sentence would be no-saying, the internet is pretty huge, and most part of it is unrelated to your problem. It would therefore appear pretty useless to search the internet unless you add some more clarifications (e.g. what have you searched for, why doesn't it play well etc.). – hakre Aug 17 '23 at 22:03
  • And what do you _want_ to achieve? Abort (which it should already do) -or- do you want to ignore local changes? You have to tell, because both is possible but exclusive to each other. Have you tried fetching and then resetting to the remote test HEAD? What did prevent you to do it so far? – hakre Aug 17 '23 at 22:05
  • And as this is EC2, why not having a deployment pipeline on AWS? AFAIK they offer some kind of service for every problem. – hakre Aug 17 '23 at 22:09
  • @hakre thanks for correcting. My knowledge relative to Github actions or pipe lining are relative new. Maybe I couldn't extract the knowledge from the internet or couldn't understand. – Ali Raza Aug 18 '23 at 06:45
  • 1
    I want, if some script fails like pull request (although it can be checked out before pull ) at server then there is docker-compose up shouldn't be running then shouldn't be down I just want to abort there, which wasn't happening. – Ali Raza Aug 18 '23 at 06:48

1 Answers1

1

It is good practice in CI/CD to graceful exit to become fail-safe, typically done with any important command with such scripts.

In this context I see two ways how to do it.

  1. The Microsoft Github Action Marketplace SSH Action¹ seems to be missing the context it is used in and therefore has this awkward default behaviour and requires to override it with an input, always.

  2. The other is exit on error in the shell script (this is normally learned before getting allowance to do deployments on remote computers, talk to your tutor).

Both ways:

    ...
    script_stop: true # <1>
    script:
        |
        cd /var/www/project || exit # <2>
        **git pull origin test** || exit
        ...

<1> First way: Control the Micrsoft Github Action by its inputs

<2> Second way: Control the execution flow by doing error handling in the shell script


See the actions¹ and your shell² user manual for details.

¹ https://github.com/marketplace/actions/ssh-remote-commands

² Are there any man pages that summarize bash scripting?

hakre
  • 193,403
  • 52
  • 435
  • 836