2

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/checkout@v2.3.2
    
    # at this position I tried the string replacing

    - name: Sync Files
      uses: SamKirkland/FTP-Deploy-Action@4.0.0
      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.

michael
  • 167
  • 2
  • 8

1 Answers1

6

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
Shivam Mathur
  • 2,543
  • 19
  • 26