Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute shell scripts?

├── .github
│   └── workflows
│       ├── cd.yml
│       └── ci.yml
├── scripts
│   ├── createDevEBAppVersion.sh
│   └── createProdEBAppVersion.sh
├── src

Github Actions throws the following error:

/home/runner/work/_temp/9c99fa0d-20da-49ed-a0af-322f274c13ac.sh: line 1: [refs/heads/develop: No such file or directory

/home/runner/work/_temp/9c99fa0d-20da-49ed-a0af-322f274c13ac.sh: line 5: [[refs/heads/develop: No such file or directory

name: CD

env:
  PACKAGE_NAME          : "app_${GITHUB_SHA::8}.zip"
  ARTIFACT_ROOT         : "${GITHUB_REF#refs/*/}"
  APPLICATION_NAME      : "example-app-name"
  AWS_ACCESS_KEY_ID     : ${{ secrets.AWS_ACCESS_KEY }}
  AWS_SECRET_ACCESS_KEY : ${{ secrets.AWS_SECRET_KEY }}
  AWS_REGION            : ${{ secrets.AWS_REGION }}
  AWS_BUCKET            : ${{ secrets.AWS_S3_BUCKET_NAME }}

on:
  push:
    branches:
      - develop
    paths-ignore:
      - '**.json'
      - '**.md'
  release:
    tags:
      - 'v*'

jobs:
  deploy:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
    - name: Create ZIP package artifact
      run: git archive --format=zip HEAD -o ${{ env.PACKAGE_NAME }}

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

    - name: Copy package artifact to S3 bucket
      run: |
        aws s3 cp ${{ env.PACKAGE_NAME }} s3://${{ env.AWS_BUCKET }}/${{ env.APPLICATION_NAME }}/
        echo "✔ Artifact uploaded successfully to s3://${{ env.AWS_BUCKET }}/${{ env.APPLICATION_NAME }}/${{ env.PACKAGE_NAME }}"

    - name: Create new ElasticBeanstalk Application Version
      working-directory: scripts
      run : |
        if ["${{ github.ref }}" == "refs/heads/develop"]; then
          chmod +x ./createDevEBAppVersion.sh
          ./createDevEBAppVersion.sh
        fi
        if [["${{ github.ref }}" == *"refs/tags/v"*]]; then
          chmod +x ./createProdEBAppVersion.sh
          ./createProdEBAppVersion.sh
        fi

    ...

I've tried without having working-directory, but it didn't work.

#!/usr/bin/env bash

aws elasticbeanstalk create-application-version \
--application-name ${{ env.APPLICATION_NAME }} \
--source-bundle S3Bucket="${{ env.AWS_BUCKET }}",S3Key="${{ env.APPLICATION_NAME }}/${{ env.PACKAGE_NAME }}" \
--version-label "${{ env.PACKAGE_NAME }}" \
--description "Application version created from https://github.com/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID"
echo "✔ Deployment triggered successfully."

It seems like CI had problems finding a file. How can I execute createDevEBAppVersion.sh?

like image 650
JeffMinsungKim Avatar asked Mar 02 '23 10:03

JeffMinsungKim


1 Answers

Your error message is:

/home/runner/work/_temp/9c99fa0d-20da-49ed-a0af-322f274c13ac.sh: line 1: [refs/heads/develop: No such file or directory

It's trying to execute [refs/heads/develop and failing. The root cause is that you're missing a space between the bracket and the test. You want:

if [ "${{ github.ref }}" == "refs/heads/develop" ]; then

(And the same on line 5.)

That's because if in bourne shells actually runs a command that it evaluates. Although it looks like it's evaluating an expression, it's actually passing an expression to a command called [.

% ls -Flas /bin/\[
 8 -rwxr-xr-x  1 root  wheel  22704 May  4  2019 /bin/[*

This is often a symlink (or hardlink) to /bin/test, and in some shells, like bash and zsh, [ will actually be a shell built-in:

% which '['
[: shell built-in command

But it still behaves like a command, and syntactically, it needs the space. Because:

if [ true ]; then ...

Runs the [ command (or shell-builtin) to evaluate the /bin/true command. And

if [true ]; then ...

Tries to run the nonexistent [true command.

like image 52
Edward Thomson Avatar answered Mar 05 '23 18:03

Edward Thomson