Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Github Actions Substitute String before Deploying

I am trying to develop a Github Action to update the Version Number and DB credentials in my (mostly) php files before uploading to a FTP server.

Inside my main branch, I have a textfile called VERSION.md with the content 2.0.1.

The action should run on each push commit.

Before deploying to a FTP server, these substitutions are required (of course, the real repository files should not be changed, only the files which are uploaded to the server).

  • For each file, the string __VERSION__ should be replaced with the content of my VERSION.md file.
  • In addition, only for the file db_connection.php, the string __DB-PASSWORD__ should be replaced with the corresponding secret DB_PASSWORD.

This is my current workflow:

on: push
name: FTP Deploy
jobs:
  web-deploy:
    name: Ubuntu VM
    runs-on: ubuntu-latest
    steps:
    - name: Getting latest Code
      uses: actions/[email protected]
    
    # at this position I tried the string replacing

    - name: Sync Files
      uses: SamKirkland/[email protected]
      with:
        server: ${{ secrets.ftp_server }}
        username: ${{ secrets.ftp_username }}
        password: ${{ secrets.ftp_password }}
        server-dir: "public_html/"
        exclude: .git*
          - .git*/**
          - fonts/ttf/**
          - symbols/**
          - README.md
          - VERSION.md

As a first test, I have tried

- name: Set Tokens
      run: perl -pi -e 's/getString\(R\.string\.token\)/"$ENV{TOKEN}"/' 'index.php'
      env:
        TOKEN: ${{ secrets.TOKEN }}

to replace only for index.php the string TOKEN with a token stored in my secrets, but this fails. Everything looks ok, but there is no change a the server.

like image 245
michael Avatar asked May 28 '26 01:05

michael


1 Answers

You can use sed to do this

For example to replace 'foo' with 'bar' - sed -i 's/foo/bar/g' input_file

- name: Replace credentials
  run: |
    find . -name "*" -exec sed -i "s/__VERSION__/$(cat VERSION.md)/g" {} +
    sed -i 's/__DB-PASSWORD__/${{ secrets.DB_PASSWORD }}/g' db_connection.php
like image 131
Shivam Mathur Avatar answered Jun 01 '26 12:06

Shivam Mathur



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!