2

I have a static HTML website with the following structure:

.
├── index.html
└── images/
    ├── a.jpg
    ├── b.jpg
    └── c.jpg

I found the following configuration in a blog post from 2016:

pages:
  stage: deploy
  script:
  - mkdir .public
  - cp -r * .public
  - mv .public public
  artifacts:
    paths:
    - public
  only:
  - main

I attempted to simplify it to:

pages:
  stage: deploy
  script:
    - mkdir -p public
    - cp -r * public
  artifacts:
    paths:
      - public
  only:
    - main

Is this simplified version valid, functional, and reliable for deploying a plain HTML website with GitLab CI/CD? Will it remain robust and continue to correctly deploy the entire content, including any new files such as scripts, stylesheets, or additional HTML pages that I might add to the root directory in the future?

1
  • Whether that job is configured "valid" can be checked through Gitlabs own linters. Whether it is reliable or whether the other is "better" depends on what you want to achieve. Anything not working with them? Commented May 9, 2023 at 5:58

4 Answers 4

1

I don't see why the simplified version shouldn't work. To deploy to GitLab pages, you have to move files to the public folder and declare that folder as artifact. This has not changed, see the docs.

However, only is now deprecated and you should use rules. E.g.:

pages:
  # …
  rules:
    - if: $CI_COMMIT_BRANCH == "main"
Sign up to request clarification or add additional context in comments.

Comments

1

The difference between the original version and your simplified version is that the original version will also delete the previous contents in the public folder (if it exists). This would make more sense if the "public" folder was outside of your project - for example, on the server that is hosting the website.

If you don't have a folder named "public" in your project, there's nothing to worry about. Your simplified CI will copy all and any new files to the folder, and create an artifact with all new files, regardless of their type or extension.

Your version will work no matter how many files you add or delete from your project - be it inside your root directory or any other subdirectories. The -r argument in cp command will copy all files recursively, and the gitlab runner makes sure that the "working directory" is always clean and there are no old files inside the directory.

If you want, you can also rename your root folder to public - that way, you won't have to copy or move any files inside your CI, and you can just directly create an artifact from the folder:

Directory structure:

public
├── index.html
└── images/
    ├── a.jpg
    ├── b.jpg
    └── c.jpg

CI file:

pages:
  stage: deploy
  script:
    - ls .
  artifacts:
    paths:
      - public
  only:
    - main

2 Comments

"you can just directly create an artifact from the folder" I just put your CI file configuration in GitLb CI Lint. It throws an error message saying the syntax is incorrect: "This GitLab CI configuration is invalid: jobs pages config should implement a script: or a trigger: keyword."
I see, I didn't know that the "script" is absolutely needed by gitlab CI, my bad - I didn't test it out. I re-added a script to my answer, the only thing it does is it lists the current directory contents - you can change it to an "echo" command or anything like that.
0

This worked quite easily for me in (2023)

pages:
  stage: deploy
  script:
  - mkdir .public
  - cp -r * .public
  - mv .public public
  artifacts:
    paths:
    - public
  only:
  - master

Comments

0

The original version's intermediate step with .public is essentially an unnecessary complexity if you only need to create a public directory and copy files into it.

The simplified version you've come up with is indeed valid, functional, and reliable for deploying a plain HTML website with GitLab CI/CD. It performs the necessary steps to copy the website files into a public directory, which GitLab Pages uses for deployment. Therefore, you can confidently use the simplified version.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.